You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers how to write maintainable programs as required by OCR J277 Section 2.4. Maintainability means writing code that other programmers (or your future self) can easily read, understand, and modify. Good maintainability practices are essential for professional programming and are frequently tested in the OCR exam.
A maintainable program is one that can be easily:
Programs rarely remain unchanged after they are first written. They need to be updated, fixed, and improved over time — often by programmers who did not write the original code. Making code maintainable saves time and reduces errors.
Comments are notes within the code that explain what the code does and why. They are ignored by the computer but are invaluable for human readers.
OCR Pseudocode:
// Calculate the final price including VAT
const VAT_RATE = 0.20
price = real(input("Enter price: "))
total = price + (price * VAT_RATE) // Add 20% VAT
print("Total: £" + str(total))
Python:
# Calculate the final price including VAT
VAT_RATE = 0.20
price = float(input("Enter price: "))
total = price + (price * VAT_RATE) # Add 20% VAT
print(f"Total: £{total:.2f}")
| Do | Don't |
|---|---|
| Explain why the code does something | State the obvious (e.g. x = 5 // set x to 5) |
| Describe the purpose of functions | Comment every single line |
| Note any assumptions or limitations | Write comments longer than the code itself |
| Keep comments up to date | Leave outdated comments that no longer match the code |
OCR Exam Tip: When asked "how would you make this program more maintainable?", commenting is usually worth 1 mark. But be specific — say "add comments to explain the purpose of each function" rather than just "add comments."
Identifiers are the names given to variables, constants, functions, and procedures. Using meaningful names makes code self-documenting.
| Poor Name | Good Name | Why Better |
|---|---|---|
x | totalScore | Clearly describes what the variable stores |
a | studentAge | Immediately understandable |
fn | calculateAverage | Describes what the function does |
temp | temperatureCelsius | Specific and unambiguous |
lst | studentNames | Describes the contents of the list |
| Convention | Example | Usage |
|---|---|---|
| camelCase | totalScore, firstName | Variables and functions |
| UPPER_CASE | VAT_RATE, MAX_SIZE | Constants |
| snake_case | total_score, first_name | Python convention for variables |
Indentation (using spaces or tabs to offset code blocks) shows the structure of the program, making it clear which code belongs to which block (loops, if statements, functions).
Well-indented code:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.