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.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Consistency (spec 9.2.1)

Readability (spec 9.2.2)

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.333333333333333

Clean 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'

  1. Name the values: score and result
  2. Use a named constant for the pass mark: PASS_MARK = 40, MAX_MARK = 100
  3. Add spaces and a line break: if score >= PASS_MARK and score <= MAX_MARK:
  4. Then on an indented line: result = "Pass"

Answer: PASS_MARK = 40 / MAX_MARK = 100 / if score >= PASS_MARK and score <= MAX_MARK: / result = "Pass"

Key idea: Consistent spacing, line breaks, naming and line length; meaningful identifiers, informative comments, named constants and well-named subprograms.

Check yourself

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.