Procedures and Functions · Cambridge syllabus 8.1 · Lesson 3 of 3
Maintainable Programs
Maintainable programs use meaningful names, consistent structure, short coherent subprograms and comments explaining non-obvious intent. Avoid duplicated logic and test changes against requirements. Readability makes defects easier to identify.
By the end, you can
- Use meaningful identifiers.
- Use appropriate comments.
- Break complex programs into procedures and functions.
- Structure programs for readability and maintenance.
Program quality
A program is read far more often than it is written, and most of the reading is done by someone trying to change it — often the author, months later. Maintainability is what makes that possible, and it is worth marks in its own right.
Meaningful identifiers do the most work, for variables, constants, arrays, procedures and functions alike. TotalScore, PupilCount, IsValid and CalculateAverage explain themselves; T, N, Flag and DoStuff require the reader to reconstruct the intent from the code. Constants for fixed values are part of the same idea: MaxAttempts says what 3 means.
Comments use the language’s own commenting feature: // in pseudocode and Java, # in Python and an apostrophe in Visual Basic. Comments should explain why, not what. A comment saying add one to the counter above a line that adds one to the counter is noise; a comment saying the file starts at record 1, so the index is offset by one is the sentence that saves an hour. A block comment at the top of each procedure saying what it does, what it expects and what it returns is worth writing every time.
Structure comes from decomposition. Breaking a program into procedures and functions that each do one nameable thing keeps any single piece small enough to hold in mind, allows each to be tested alone, and removes repetition — the same logic in several places is the commonest reason a fixed bug comes back.
Layout is not decoration. Consistent indentation shows which statements belong to which block, and a badly indented nested loop is genuinely hard to read correctly. Blank lines between sections, one statement per line, and consistent naming conventions all reduce the effort of reading.
The honest test is to ask whether someone else could change this program safely without asking you a question. If not, the thing missing is usually a name or a comment rather than a cleverer algorithm.
Worth remembering
- Meaningful identifiers and named constants explain the code without comments.
- Comment why, not what; document each procedure at the top.
- Decompose into small single-purpose procedures, and indent consistently.
Worked example
CalculateAverage explains a purpose more clearly than DoStuff, while a comment can explain why empty input is rejected.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
A rule change may be applied in one copy but missed in another, causing inconsistent behaviour.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.