Code Maintenance Examples in CS Thinking
Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Code Maintenance.
This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.
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).
Software is never 'done.' Like a garden, it needs constant tending โ fixing bugs, updating dependencies, and adapting to changing needs.
Read the full concept explanation โHow to Use These Examples
- Read the first worked example with the solution open so the structure is clear.
- Try the practice problems before revealing each solution.
- Use the related concepts and background knowledge badges if you feel stuck.
What 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.
Sense of Study hint: When maintaining code, start by understanding what the existing code does before changing it. Write tests for the current behavior so you can verify your changes do not break anything. Make small, focused changes and test after each one rather than attempting large rewrites.
Worked Examples
Example 1
easyAnswer
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 these problems on your own first, 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
challengeRelated Concepts
Background Knowledge
These ideas may be useful before you work through the harder examples.