Programming Practice · Lesson 2 of 2
Code Quality
Readable programs use purposeful names, clear blocks and focused subprograms. Comments explain reasons and assumptions rather than restating every obvious operation. Reduce repetition while keeping the flow easy to follow.
By the end, you can
- Use meaningful identifiers.
- Use comments appropriately.
- Avoid unnecessary repetition.
- Use procedures and functions to structure code.
- Keep logic clear and readable.
Maintainability
Code quality is judged on whether someone else can read and change the program safely. The syllabus asks for maintainable programs, and Paper 2, which is a written exam, can ask you to make a program more maintainable or to explain how one has been made so.
Names carry most of the meaning. A variable called TotalMark, a function called CalculateAverage and a constant called MaxAttempts make a line of code readable on its own; T, Calc and 3 do not. Use a consistent convention throughout rather than mixing styles, and avoid single letters except for a loop counter.
Comment the why. A header on each procedure saying what it does, what it expects and what it returns is always worth writing; a note explaining an unobvious decision — why the index is offset by one, why this value is rejected — saves the next reader real time. A comment restating the line above it adds nothing and goes stale as soon as the line changes.
Repetition is the enemy. The same few lines appearing in three places is three places to fix, and one will be missed; move them into a procedure or a function and call it. If two blocks are nearly the same, the difference is usually a parameter.
Structure and layout finish the job: small single-purpose procedures, consistent indentation showing what belongs to which block, one statement per line, and blank lines between sections. Deeply nested conditions can usually be flattened, and a procedure that will not fit on a screen is generally doing more than one thing.
Worth remembering
- Meaningful, consistent names do more for readability than comments.
- Comment why, not what; document each procedure at its top.
- Move repeated code into a procedure, and keep each one to a single purpose.
Worked example
A reusable IsValidMark function can centralise a rule used in several parts of a program.
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 change to the rule can be made and tested in one place, reducing inconsistent copies.
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.