Code Maintenance Examples: 45 Problems with Answers
Worked examples of Code Maintenance to try before you open each solution, then practice problems with answers.
Worked Examples
Example 1
easyShow solution
Answer
First step
Full solution
- 2. Step 2: Fix: define a named constant: CONST TAX_RATE = 0.2. Use TAX_RATE everywhere instead of 0.2.
- 3. Step 3: Now a rate change requires editing only one line. This is the 'magic number' anti-pattern — unexplained numbers scattered through code.
Example 2
mediumExample 3
mediumExample 4
mediumExample 5
hardExample 6
hardExample 7
challengePractice Problems
Try each one, then open the solution to compare your method.
Example 1
mediumExample 2
hardExample 3
easyExample 4
easyExample 5
easyExample 6
easyExample 7
easyExample 8
easyExample 9
easyExample 10
easyExample 11
mediumExample 12
mediumExample 13
mediumExample 14
mediumExample 15
mediumExample 16
mediumExample 17
mediumExample 18
mediumExample 19
mediumExample 20
challengeExample 21
challengeExample 22
challengeExample 23
easyExample 24
easyExample 25
easyExample 26
easyExample 27
easyExample 28
mediumExample 29
mediumExample 30
mediumExample 31
mediumExample 32
mediumExample 33
mediumExample 34
mediumExample 35
hardExample 36
hardExample 37
hardExample 38
challengeWhat to focus on
Core idea: Maintenance typically consumes 60-80% of software's total lifetime cost. Writing maintainable code from the start saves enormous effort later.
Common stuck point: Maintenance isn't just fixing bugs — it includes adapting to new requirements, improving performance, and preventing future problems.
Ask yourself: Is software that people already use being changed, to fix a fault, keep up with new conditions, improve or tidy it?
Concept recap
The ongoing process of updating, fixing, and improving software after its initial release to correct bugs, adapt to new requirements, improve performance, and keep dependencies current. Maintenance includes four types: corrective (fixing bugs), adaptive (adapting to new environments), perfective (improving functionality), and preventive (reducing future problems).
Maintaining code is like looking after a house people live in: fix leaks, follow new building rules, add a room, clear the gutters before they overflow, all without moving the family out. You do not knock it down because one room is untidy. Where the comparison falls short: around software, the neighbourhood (operating systems, libraries) keeps changing on its own.
Read the full lesson on Code MaintenanceRelated concepts
Background knowledge
These ideas may be useful before you work through the harder examples.