Edexcel International Advanced Level Computer Science, point 9.2.1
Clean code is code that is easy to read, understand and change. Consistency means doing the same thing the same way throughout the program, so that readers aren't distracted by random differences and can predict the layout. The specification refers to the Good Programming Practice Guide (GPPG). Python's official style guide, PEP 8, is a good model.
a. Use of white space
White space (spaces, blank lines and indentation) separates and organises code.
- Indent with 4 spaces per level, always the same amount (Python requires consistent indentation).
- Spaces around operators:
total = price * quantity, nottotal=price*quantity. - A space after commas:
print(a, b, c). - No spaces just inside brackets:
items[0], notitems[ 0 ]. - Blank lines to separate subprograms (two) and groups of related lines (one).
Inconsistent:
def area(w,h):
a=w*h
return a
x=area( 3,4 )
Consistent:
def area(width, height):
result = width * height
return result
x = area(3, 4)
b. Line breaks and layout
- One statement per line.
- Break long lines inside brackets, or at operators, aligned so that it is clear they continue.
- Put code in a standard order: imports, constants, subprograms, main program.
- Put a blank line between sections.
- Same style for every similar structure (all
ifstatements laid out alike).
# one statement per line
x = 1
y = 2
# a long call broken inside brackets
result = calculate_total(unit_price, quantity,
discount_rate, delivery_cost)
# a long condition broken inside brackets
if (age >= 18 and has_licence
and not banned):
allow = True
c. Naming conventions
A naming convention is a set of rules for how identifiers are written. Python's (PEP 8):
| Item | Convention | Example |
|---|---|---|
| Variables | lower_case_with_underscores |
total_price, student_name |
| Subprograms | lower_case_with_underscores, verb-like |
calculate_total, get_name |
| Constants | UPPER_CASE_WITH_UNDERSCORES |
MAX_ATTEMPTS, MAX_SEATS |
| Classes | CapitalisedWords |
StudentRecord |
| Modules (files) | short_lower_case |
grades.py |
| Booleans | Read like a question | is_valid, has_finished |
Mixed styles are confusing:
TotalPrice = 0 # looks like a class
student_Name = "Amy" # mixed
getname() # no word separation
Consistent:
total_price = 0
student_name = "Amy"
get_name()
Choose one convention and keep to it throughout, including plurals (names for a list, name for one item).
d. Fixed line length
Keep lines to a maximum length, 79 characters in PEP 8 (some teams use 88 or 100).
- Lines fit on screen and in side-by-side views without scrolling.
- Short lines are easier to read and to compare in version control (6.3.2).
- Break long lines as in (b).
A statement written on one line of about 130 characters would run off the screen. Broken inside brackets, it fits:
message = ("Dear " + customer_name + ", your order " + str(order_number)
+ " has been dispatched and will arrive on " + delivery_date)
Why consistency matters
- Easier to read: the eye learns the pattern.
- Easier to maintain: new people can follow the code.
- Fewer mistakes: differences stand out because everything else is uniform.
- Teamwork: everyone follows the same rules (a team style guide).
- Tools (linters and formatters, such as flake8 and black) check or fix style automatically.
Worked example
Task: identify the consistency problems in this code, and rewrite it.
MaxScore=100
def calc(Name,s):
if s>MaxScore: return "Invalid"
else:
return Name+" scored "+str(s)
Problems:
- White space: no spaces around
=,>,+or after the comma. - Layout:
ifandreturnon one line, and indentation differs (2 spaces, then 8). - Naming:
MaxScore(looks like a class, but is a constant),Name(capital for a variable),calc(not descriptive),s(unclear). - No blank lines around the function.
Rewritten:
MAX_SCORE = 100
def describe_score(name, score):
if score > MAX_SCORE:
return "Invalid"
return name + " scored " + str(score)
Exam tip: consistency is about applying the same convention throughout. Give specific points: 4-space indentation, spaces around operators, snake_case for variables, UPPER_CASE for constants, lines under 79 characters. In the practical exam, keep your own code neat.
Check yourself
What is the Python convention for naming constants?
Upper case with underscores, such as MAX_SCORE.
Give two ways white space improves readability
Any two of: consistent indentation; spaces around operators and after commas; blank lines to separate subprograms and groups of lines.
Why use a fixed maximum line length?
Lines fit on screen and in side-by-side views without scrolling, and short lines are easier to read and compare.
Why does consistency make code easier to maintain?
Readers learn the pattern, so they follow it quickly, and any difference stands out as a possible mistake.