6.1.4 – Make programs readable

6.1.4 – Make programs readable

Use layout, indentation, names, comments and white space to make code easier to understand and maintain while preserving its behaviour.

Make the purpose easy to find

Readability helps a person understand code; maintainability helps them change it safely later. Correct output and clear presentation are different qualities. A readability change should preserve behaviour unless a new requirement is explicitly requested.

Python
q=6
p=2
t=q*p
print(t)
Python
ticket_count = 6
ticket_price = 2
total_cost = ticket_count * ticket_price

print(total_cost)

Both print 12. The second version identifies what each number means, so a maintainer changing the ticket price can locate the correct value without reconstructing the whole calculation.

The specification names five techniques: layout, indentation, comments, meaningful identifiers and white space. They work together. A long comment cannot rescue misleading names or a statement placed inside the wrong block.

Show the program structure

Layout groups related setup, processing and output. Indentation determines Python's nested blocks; it is part of program meaning. White space within expressions and between sections helps the reader scan them.

Python
scores = [4, 7, 3]
pass_mark = 5

for score in scores:
    if score >= pass_mark:
        print("Pass")
    else:
        print("Review")
    print("Checked")

The final print is inside the loop but outside the selection, so every score produces Checked. Moving it left to the margin would make it execute once after all scores. Four spaces per level is a clear convention; what Python requires is consistent, valid block structure.

Use spaces around operators and after commas. Separate logical sections with blank lines when that reveals structure. There is no need to double-space the program or insert exactly one blank line after every statement.

Long expressions can continue inside parentheses or square brackets:

Python
delivery_charge = 3
cost = (ticket_count * ticket_price
        + delivery_charge)

The indentation here helps the eye follow one expression; it does not create an if or loop block. Python also supports explicit backslash continuation under stricter placement rules, but grouping an expression in parentheses is often clearer. The expression and its evaluation order must stay unchanged.

Name roles and explain non-obvious rules

A meaningful identifier says what a value or subprogram does in this problem. maximum_score communicates more than number2. A long name is not necessarily useful: choose a clear role rather than padding it with words.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account