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

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?

Answer

Replace the magic number with a named constant (TAX_RATE = 0.2). Changes then require editing only one line.

First step

1
Step 1: If the tax rate changes, you must find and update all 15 occurrences. Missing even one creates a bug.

Full solution

  1. 2
    Step 2: Fix: define a named constant: CONST TAX_RATE = 0.2. Use TAX_RATE everywhere instead of 0.2.
  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.
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.

Example 3

medium
A 200-line function does input parsing, business logic, and database writes. List two maintenance problems this causes.

Example 4

medium
A function getCustomerById is renamed. List one risk and one mitigation.

Example 5

hard
A legacy module has no tests and produces correct output. Outline the safe order of work to add a new feature inside it.

Example 6

hard
Two engineers fix the same bug by editing the same lines on separate branches. What is the standard term for the conflict, and what is the resolution discipline?

Example 7

challenge
A senior dev says 'comments lie, but tests don't'. Explain in one paragraph what this means for maintenance.

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?

Example 3

easy
Fixing a reported bug in released software is which type of maintenance?

Example 4

easy
Updating software to run on a new operating system is which type of maintenance?

Example 5

easy
Adding a new feature or improving existing functionality is which type of maintenance?

Example 6

easy
Refactoring to reduce future bugs, before any problem occurs, is which type of maintenance?

Example 7

easy
Roughly what share of software's total lifetime cost does maintenance typically consume?

Example 8

easy
Why update dependencies regularly? (best reason)

Example 9

easy
True or false: rewriting working code from scratch is usually safer and cheaper than improving it incrementally.

Example 10

easy
Before changing unfamiliar code, what should you do first to avoid introducing bugs?

Example 11

medium
A library you depend on has a critical security patch released. Which maintenance type and action?

Example 12

medium
While fixing bug A, a developer changes shared code and breaks feature B. What practice would have prevented this?

Example 13

medium
Classify: the OS vendor releases a new version and your app must be updated to keep running. Maintenance type?

Example 14

medium
Classify: users are happy and there are no bugs, but the team refactors a tangled module to ease future changes. Type?

Example 15

medium
Classify: a customer requests a new export-to-PDF feature. Which maintenance type?

Example 16

medium
A team faces a messy module. Option A: incremental refactor with tests. Option B: full rewrite. Which is generally safer?

Example 17

medium
Why is writing maintainable code from the START economically smart, given the 60-80% rule?

Example 18

medium
Classify: the team rewrites a slow report so it runs 10x faster, with no behavior change. Maintenance type?

Example 19

medium
Classify: a payment regulation changes and the app must adjust how it stores tax data to stay compliant. Type?

Example 20

challenge
A 5-year-old app uses an unpatched library with a known exploit, and the vendor's fix is in a new major version with breaking changes. Outline the disciplined maintenance path.

Example 21

challenge
Maintenance backlog has: (1) a data-loss bug, (2) a nice-to-have feature, (3) an unpatched critical CVE. Order by priority and justify the top item.

Example 22

challenge
A developer 'fixes' a bug by commenting out the failing check, and the symptom disappears. Why is this poor maintenance, and what is correct?

Example 23

easy
Name the four types of software maintenance.

Example 24

easy
Why are meaningful variable names a maintenance practice?

Example 25

easy
What is a 'magic number' and why is it a maintenance risk?

Example 26

easy
True or false: comments are useful only when they describe what the next line does.

Example 27

easy
Roughly what fraction of total software cost is maintenance?

Example 28

medium
Classify: a tax rate changes from 0.20 to 0.22 nationally and the app must update. Which maintenance type?

Example 29

medium
A small fix is shipped without tests. Six months later a similar bug recurs. Name the maintenance practice that was skipped.

Example 30

medium
Why is duplicated code a maintenance hazard?

Example 31

medium
A dependency releases a major version with breaking changes. The team decides to upgrade. What test discipline should accompany the upgrade?

Example 32

medium
Version control records who changed what and when. How does this support maintenance?

Example 33

medium
Why are small, frequent commits easier to maintain than one huge end-of-week commit?

Example 34

medium
Classify: the team removes an unused helper file. Which maintenance type?

Example 35

hard
Why is 'we'll fix it later' an anti-pattern in maintenance?

Example 36

hard
A backlog has three items: (1) critical CVE, (2) flaky test in CI, (3) new dashboard. Justify the top two by maintenance principles.

Example 37

hard
A pull request changes 40 unrelated files. Why should reviewers ask for it to be split?

Example 38

challenge
A team is asked: 'rewrite or refactor this 10-year-old monolith?'. State the principle that biases the answer.

Background Knowledge

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

documentationsoftware development life cycle