All subjects · Computer Science · WCP02 Practical Programming and Problem-solving
Clean code: consistency and readability
Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving
Examiners award marks for clean code: code that another programmer can read and maintain. The specification lists what that means.
Consistency (spec 9.2.1)
- White space: one space each side of = and operators, a blank line between subprograms, consistent 4-space indentation.
- Line breaks: one statement per line; break long statements inside brackets.
- Naming conventions: snake_case for variables and subprograms, UPPER_CASE for constants, used the same way throughout.
- Fixed line length: choose a maximum line length and keep to it, so code can be read without scrolling.
Readability (spec 9.2.2)
- Meaningful identifiers: total_cost, not tc or x.
- Informative comments: explain why, or what a block does, not what one obvious line does. Start with a comment describing the program.
- Named constants: MAX_WIDTH instead of a bare 20.
- Subprograms: give a block of code a name that says what it does.
Before and after
Both versions give the same output, but only one can be understood at a glance:
a=[3,9,4]
t=0
for i in a:t=t+i
print(t/len(a))Output:
5.333333333333333# Display the mean of a list of quiz scores
def mean(values):
total = 0
for value in values:
total = total + value
return total / len(values)
quiz_scores = [3, 9, 4]
print(mean(quiz_scores))Output:
5.333333333333333Clean code in the mark scheme
In the sample paper's longest question, marks were given for informative comments, sensible white space, consistent notation, meaningful identifiers, using the supplied constants instead of literals, and parameter names that make scope clear (re-using a global name as a parameter was not accepted).
Worked example
Improve this line: if s>=40 and s<=100: r='P'
- Name the values: score and result
- Use a named constant for the pass mark: PASS_MARK = 40, MAX_MARK = 100
- Add spaces and a line break: if score >= PASS_MARK and score <= MAX_MARK:
- Then on an indented line: result = "Pass"
Answer: PASS_MARK = 40 / MAX_MARK = 100 / if score >= PASS_MARK and score <= MAX_MARK: / result = "Pass"
Check yourself
- Which identifier is the best choice for the number of seats left on a bus?
- s
- SeatsRemaining2
- seats_remaining
- x1
Show answer
seats_remaining. It says exactly what the value is, in the snake_case convention.
- Which line follows the convention for a constant?
- MAX_ATTEMPTS = 3
- max_attempts = 3
- MaxAttempts = 3
- maxattempts = 3
Show answer
MAX_ATTEMPTS = 3. Constants are written in UPPER_CASE with underscores.
- Which comment is most informative?
- # Loop
- # Add 1 to count
- # Retry until the user enters a mark from 0 to 100
- # This is a while loop
Show answer
# Retry until the user enters a mark from 0 to 100. It explains the purpose of the block, which the code alone does not say.
- A program uses 20 in several places as the number of items per row. What is the best improvement?
- Write 20 as twenty in a comment each time
- Assign ITEMS_PER_ROW = 20 once and use the name
- Replace 20 with 10 + 10
- Store 20 in a different variable in each subprogram
Show answer
Assign ITEMS_PER_ROW = 20 once and use the name. A named constant explains the value and means it changes in one place.
- What is displayed when this code runs?
```python
# Count words longer than a set length
MIN_LENGTH = 5
def count_long_words(sentence):
count = 0
for word in sentence.split():
if len(word) > MIN_LENGTH:
count = count + 1
return count
print(count_long_words("clean code reads like well written prose"))
```
Show answer
1. Words longer than 5 characters: written: 1. Clean code is easier to trace like this.
- A global list is called readings. Which parameter name is best for a function that averages any list passed to it?
- readings
- values
- global_readings
- r
Show answer
values. A distinct parameter name makes it clear the function works on its argument, not on the global; the sample mark scheme did not accept a global's name as a parameter name.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification (XCP01/YCP01), first teaching 2026
- Pearson IAL Computer Science Unit 2 Programming Language Subset (PLS), Python 3
- Pearson IAL Computer Science IAS sample assessment materials (Unit 2 paper and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.