“Programs must be written for people to read, and only incidentally for machines to execute."
- Harold Abelson, Structure and Interpretation of Computer Programs
top_result = df.sort_values('age', ascending=True).groupby('city').head(int(top_item.limit))
top_result = (df
.sort_values('age', ascending=True)
.groupby('city')
.head(int(top_item.limit))
)
if (company.name == 'Orcablue' and company.domain == 'tech' and session is not None and cache is not None):
# do something
if ((company.name == 'Orcablue') and
(company.domain == 'tech') and
(session is not None) and
(cache is not None)):
# do something
Split up expressions into multiple lines
def adder(a, b):
return a + b
net_pay = adder(90 if employee.designation == 'Manager' else 100, employee.bonus if employee.bonus and employee.bonus > 0 else 0)
def adder(a, b):
return a + b
salary = 90 if employee.designation == 'Manager' else 100
bonus = employee.bonus if employee.bonus and employee.bonus > 0 else 0
net_pay = adder(salary, bonus)
Do not pass expressions as parameters
def check_if_measure(parse_node, tree_nodes):
result = False
ops = None
parse_node_i = None
for i, node in enumerate(tree_nodes):
if node == parse_node:
parse_node_i = i
if parse_node_i and parse_node_i > 0:
if tree_nodes[parse_node_i - 1].c_tag == '0'
result = True
ops = parse_node.parent.t_tag
return result,ops
def check_if_measure(parse_node, tree_nodes):
result = False
ops = None
parse_node_i = None
for i, node in enumerate(tree_nodes):
if node == parse_node:
parse_node_i = i
if parse_node_i and parse_node_i > 0:
if tree_nodes[parse_node_i - 1].c_tag == '0'
result = True
ops = parse_node.parent.t_tag
return result,ops
Split logic into visual contexts
class Person:
def __init__(self, field1, field2, field3, field3):
self.field1 = field1
self.field2 = field2
self.field3 = field3
self.field4 = field4
class Employee:
def __init__(self, name, age, designation, salary):
self.name = name
self.age = age
self.designation = designation
self.salary = salary
class Akira:
def __init__(self, org_name, org_domain, created):
self.org_name = org_name
self.org_domain = org_domain
self.created = created
self.employees = {}
self.engagements = {}
def add_employee(self, p):
self.employees[p.field1] = p
def new_engagement(self, name, c, emp):
eng_status = 'BEGIN'
self.engagements[name] = (c, e, eng_status)
def engagement_over(self, name):
eng_status = 'TERMINATED'
c, e, _ = self.engagements[name]
self.engagements[engagement_name] = (c, e, eng_status)
def expenditure(self):
temp = 0
for e in self.employees:
temp += e.field4
return temp
def unassigned_employees(self):
unassigned = []
for e in self.employees:
for p_name, p in self.projects.items():
if p[1].field1 == e.field1:
unassigned.append(e)
return unassigned
class Organization:
def __init__(self, name, domain, incorporated):
self.name = name
self.domain = domain
self.incorporated = incorporated
self.employees = {}
self.projects = {}
def hire(self, employee):
self.employees[employee.name]] = employee
def start_project(self, project_name, client, employee):
status = 'ONGOING'
self.projects[project_name] = (client, employee, status)
def finish_project(self, project_name):
status = 'FINISHED'
client, employee, _ = self.projects[project_name]
self.projects[project_name] = (client, employee, status)
def calculate_expenditure(self):
sum_of_salaries = 0
for employee in self.employees:
sum_of_salaries = employee.salary
return sum_of_salaries
def get_unassigned_employees(self):
assigned_employees = []
for (_, employee, _) in self.projects.values():
assigned_employees.append(employee)
unassigned_employees = (
set(self.employees) - set(assigned_employees))
return unassigned_employees
“I like my code to be elegant and efficient."
- Bjarne Stroustrup, creator of C++
# On master branch
git checkout -b new_feature
...
# On new_feature branch
git add ...
git commit -m "Adds skeleton for new class"
git add ...
git commit -m "Adds methods for conncting to database"
git add ...
git commit -m "Fixes issue #71. None return value needed to be handled."
git checkout master
# On master branch
git merge new_feature
git branch -d new_feature
git push