Whose code is this?
It is your first week at a company. You open a file written by somebody who left. One part is 200 lines long. The names are a, b and x2. There are no comments.
You must fix a bug in it by Friday. How do you feel?
People read code many more times than they write it. So write code for the next reader.
Key words
| Word | What it means | Example |
|---|---|---|
| Modular | Made of small parts, each with one job. | line_total, receipt_line |
| Readability | How easy code is to read and understand. | Clear names |
| Convention | A rule most programmers in a team or language follow. | snake_case names in Python |
| camelCase | Names with no spaces, each new word starting with a capital. | totalPrice |
| Comment | A note in the code for people, which the computer ignores. | # Add the delivery fee |
| Change log | A list of what changed in each version. | 2.0.1 Fixed the login bug |
| Documentation | Written help about the code and the product. | A README file |
| Refactoring | Making code cleaner without changing what it does. | Split one long function into three |
| Function | A named piece of code that does one job. | line_total(price, qty) |
The cells students fill in are left blank in this preview.
Five habits make code easy to share
| Habit | What it means | Example |
|---|---|---|
| Separate the code (modularity) | Small parts, each with one job | Do not put the price calculation and the printing in one function |
| Make it readable | Names and layout that explain themselves | total_price, not tp |
| Follow conventions | Do what the language and the team do | snake_case in Python, one style in the team |
| Comment to explain why | Say why, not what | # Free delivery starts at 20 because of the offer |
| Update the change log and documents | Record every change | Write a line in the change log with each fix |
The cells students fill in are left blank in this preview.
Try it: the same code, two ways
# Version 1
def f(a, b):
return a * b * 0.8
print(f(10, 3))
# Version 2
def discounted_price(price, quantity):
# Members get 20 out of every 100 off, so we keep 80 out of 100 (0.8).
member_share = 0.8
return price * quantity * member_share
print(discounted_price(10, 3))
# Both print 24.0. Which would you rather maintain?
Make a free account to save your work and track your progress.
Match each example to its habit
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Conventions are about agreement
Tip
Python programmers write names in snake_case: small letters with underscores, like total_price. Java programmers use camelCase: new words start with a capital letter and there is no space, like totalPrice. Neither is better. What matters is that the whole team does the same.
Your turn: split the long function
The receipt function does everything in one place. Split it into small functions. line_total gives price times quantity. basket_total adds all the lines. receipt_line gives text like Tea x2 = 3.00 (that is 2 teas at 1.50 each). Then rewrite receipt so it uses the small functions and gives the same text as before. The hidden tests check that receipt really calls them. Press Run Tests.
# This function works, but it does everything in one place.
# Split it into small functions. Each one does ONE job.
def line_total(price, qty):
# price times qty
return 0
def basket_total(items):
# items is a list of (name, price, qty). In one item, item[0] is the name,
# item[1] is the price and item[2] is the quantity. Give back the total of all the lines.
return 0
def receipt_line(name, price, qty):
# Give back text like: Tea x2 = 3.00 (x2 means 2 teas. The total has 2 decimal places)
return ""
def receipt(items):
# This one is the old long version. Rewrite it so it USES the three functions above.
text = ""
total = 0
for item in items:
cost = item[1] * item[2]
text = text + item[0] + " x" + str(item[2]) + " = " + format(cost, ".2f") + "\n"
total = total + cost
text = text + "Total = " + format(total, ".2f")
return text
Make a free account to save your work, or press Start lab to work through the whole lesson.
Good comments say why
A comment is a note for people. The best comments say why. The code already says what.
Bad: # multiply price by quantity. Good: # Round up, because the shop never sells half a unit.
A wrong comment is worse than no comment. When you change the code, change the comment. Notes in code are also called annotations.
Try it: a note inside a function
def line_total(price, quantity):
"""Give back the price times the quantity."""
return price * quantity
print(line_total(2.5, 2))
print(line_total.__doc__)
# The text in triple quotes is a docstring: a note that stays with the function.
# Change the words inside the triple quotes, then press Run again.
Make a free account to save your work and track your progress.
Check: comments and names
Have a go
Check: comments and names
4 questionsA change log records every change
Teams record every change. A change log is a short list: version, date, what changed.
It helps the next person see what happened and why. It also helps support staff answer the question: "When did this change?"
Keep other documents up to date too. A README is a file that explains the project and how to run it.
Write the change log
Write two functions for a change log. log_line makes one line like 2.0.1 (2026-10-02) [Fixed] Login accepts spaces. change_log joins the lines of many entries, newest day first. The function newest_first is finished for you: it puts the entries in order. Press Run Tests.
# A change log is a short list of what changed in each version, newest first.
# One entry is a dictionary: {"version": "2.1.0", "day": "2026-10-17", "kind": "Fixed", "text": "Login accepts spaces"}
# kind is one of: Added, Changed, Fixed.
def log_line(entry):
# Give back one line of text like:
# 2.1.0 (2026-10-17) [Fixed] Login accepts spaces
return ""
def newest_first(entries):
# Finished for you. Gives back a new list, newest day first.
# The days look like 2026-10-17, so text order is date order.
ordered = []
for entry in entries:
place = len(ordered)
for i in range(len(ordered)):
if entry["day"] > ordered[i]["day"]:
place = i
break
ordered.insert(place, entry)
return ordered
def change_log(entries):
# Give back one text with a log_line for each entry, newest day first.
# Put a new line ("\n") between two lines, and none at the end.
# Hint: loop over newest_first(entries). Start with text = "".
# Before each line, add "\n" if text is not empty yet.
return ""
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Final check
Have a go