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, and improve performance.

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.

Worked Examples

Example 1

easy
A program uses the number 0.2 (tax rate) in 15 different places. Why is this a maintenance problem, and how should it be fixed?

Solution

  1. 1
    Step 1: If the tax rate changes, you must find and update all 15 occurrences. Missing even one creates a bug.
  2. 2
    Step 2: Fix: define a named constant: CONST TAX_RATE = 0.2. Use TAX_RATE everywhere instead of 0.2.
  3. 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

Replace the magic number with a named constant (TAX_RATE = 0.2). Changes then require editing only one line.
Magic numbers make code hard to maintain and understand. Named constants make the meaning clear and changes easy. This is one of the simplest and most impactful code quality improvements.

Example 2

medium
List four practices that make code easier to maintain, and explain why each helps.

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

medium
Identify three maintenance problems in this code: x = INPUT(). y = x * 0.175. z = x + y. OUTPUT z. Rewrite it with better practices.

Example 2

hard
Explain the term 'technical debt' and give two examples of how it accumulates. How does it affect long-term code maintenance?

Background Knowledge

These ideas may be useful before you work through the harder examples.

documentationsoftware development life cycle