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
easySolution
- 1 Step 1: If the tax rate changes, you must find and update all 15 occurrences. Missing even one creates a bug.
- 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.
Answer
Example 2
mediumPractice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
mediumExample 2
hardRelated Concepts
Background Knowledge
These ideas may be useful before you work through the harder examples.