Cambridge IGCSE · Topic 8.1
IGCSE Computer Science 0478 / Topic 8 / IGCSE

Code for the next reader.

Programs are read far more often than they are written. Someone, often you, will have to fix or update your code months later. Learn the techniques the syllabus expects: meaningful identifiers, comments, constants, and procedures and functions.

The key idea

A maintainable program is easy for another programmer to understand, correct and update. Use meaningful identifiers for variables, constants, arrays, procedures and functions, so each name says what it holds or does. Add comments (starting with // in pseudocode) to explain the purpose of sections of code; the computer ignores them. Use constants for fixed values and procedures and functions to split the program into named, reusable parts.

hard to maintain maintainable X ← 0FOR I ← 1 TO 30INPUT Y X ← X + YNEXT IOUTPUT X / 30 // average mark for one class CONSTANT ClassSize ← 30 TotalMark ← 0 FOR Student ← 1 TO ClassSize INPUT Mark TotalMark ← TotalMark + Mark NEXT Student OUTPUT TotalMark / ClassSize same algorithm: meaningful names, a comment and a constant
Fig. 1: both programs do the same job, but only one explains itself
Section 01

Meaningful identifiers.

Compare the two versions of the same algorithm. The second needs no explanation: the names tell you what each value is.

X ← 0
FOR I ← 1 TO 30
    INPUT Y
    X ← X + Y
NEXT I
OUTPUT X / 30
// Works out the average mark for one class
CONSTANT ClassSize ← 30
DECLARE TotalMark : INTEGER
DECLARE Mark : INTEGER
DECLARE Student : INTEGER
TotalMark ← 0
FOR Student ← 1 TO ClassSize
    INPUT Mark
    TotalMark ← TotalMark + Mark
NEXT Student
OUTPUT "Average mark: ", TotalMark / ClassSize
Examiner trap

"Use good variable names" is too vague for a mark. Say meaningful identifiers and explain the benefit: another programmer can see what each variable stores, so the code is easier to understand and update.

Section 02

Comments.

A comment is a note for human readers. In pseudocode it starts with two forward slashes and continues to the end of the line. It usually goes on its own line above the code it describes.

// Validate the age: it must be from 11 to 18
REPEAT
    INPUT Age
UNTIL Age >= 11 AND Age <= 18
Discount ← Price * 0.1    // 10% discount for students

The same idea in Python (for comparison only: the exam uses pseudocode)

# Python comments start with a hash
discount = price * 0.1    # 10% discount for students
Worked example Useful or not?
Which comment is more useful above the line Count ← Count + 1 inside a validation loop: (A) // add 1 to Count, or (B) // count the number of failed attempts?
  1. Comment A only repeats what the code already says.
  2. Comment B explains the purpose: what the count is for.
Answer: B: comments should explain why, not repeat the code
Examiner trap

Comments are ignored by the translator, so they do not make the program run faster or slower, and they do not fix errors. They help people.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which identifier is the most meaningful for the number of lives a player has left?

Quick check+10 XP

How does a comment begin in 0478 pseudocode?

Quick check+10 XP

Why use a constant for a VAT rate rather than typing 0.2 throughout the program?

Quick check+10 XP

Which is a valid 0478 pseudocode identifier?

Section 03

Constants and subroutines.

Each program in the lab has one mistake. As you hunt for it, notice how meaningful names and a clear structure make the faulty line easier to find.

Worked example Improve this code
A program contains the line P ← C * 1.2 in five places, where 1.2 adds 20% tax. Suggest three improvements to make it maintainable.
  1. Rename the variables, e.g. PriceWithTax and CostPrice.
  2. Declare CONSTANT TaxMultiplier ← 1.2 and use it instead of 1.2, so a tax change is one edit.
  3. Put the calculation in a function, e.g. AddTax(CostPrice), called in all five places, with a comment explaining it.
Answer: Meaningful identifiers, a constant and a function (with a comment)
Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Which TWO techniques make a program more maintainable?

Exam style+20 XP

What is the purpose of comments in a program?

Exam style+20 XP

Which identifier is NOT allowed in 0478 pseudocode?

Exam style+20 XP

How does using procedures and functions make a program easier to maintain?

Skill unlocked

Maintainable programs, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Meaningful identifiers, comments, constants and subroutines: explaining and applying them.