Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Software Design & Development · Grade 9-12

Code Maintenance

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.


Quick Answer

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. It fills most of a program's working life: an app released once may be patched hundreds of times. The four kinds are corrective (fix a bug), adaptive (keep working when the phone system, a law or a library changes), perfective (add or improve features) and preventive (tidy the code so later changes are safer). Each change starts by understanding the existing code and ends by rerunning its tests.

Why This Matters

Software decays even when nobody touches it: a phone update retires a feature the app used, a library gets a security hole, a tax rate changes. Studies of real projects put maintenance at roughly 60-80% of total cost, so habits that make code easy to change, like clear names, tests and small modules, pay back more than writing it fast. Fixes made without understanding the code are a common source of new bugs.

Intuitive Explanation

Think of a bike you ride every day. You fix a flat tyre (a repair), fit winter tyres (adapting to new conditions), add a basket (an improvement) and oil the chain before it rusts (prevention). Software maintenance has the same four kinds. A quiz app that crashes on a blank answer needs corrective maintenance. The same app needs adaptive maintenance when a new phone system stops supporting its old notification method. Adding a timer is perfective; splitting a 900-line file into modules so the next change is easier is preventive.

The hard part is that you are changing code that already works for real people, often code someone else wrote. A safe change follows a loop: reproduce the problem, read enough code to understand why it happens, make the smallest change that fixes it, rerun the tests, and add a test for the case that broke. Skipping the 'understand' step is how one fix creates two new bugs.

Maintenance is not rewriting. Throwing away a messy but working program and starting again feels clean, but the old code holds years of fixes for odd cases nobody wrote down, and the new version has to rediscover them one complaint at a time. Maintenance is also different from building version 1: before release there are no users, so a change cannot break anyone's work. Once the software is released and people rely on it, every change is maintenance.

Think of it like this. 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.

Core idea. Maintenance typically consumes 60-80% of software's total lifetime cost. Writing maintainable code from the start saves enormous effort later.


When to Use

Code maintenance is the frame when software is already released and something must change: a bug report, a new phone or browser version, an outdated library, a feature request, or code that has become hard to change. Classify the change as corrective, adaptive, perfective or preventive. Before any release, it is still development within the SDLC; if the question is how to record and undo changes, use Version Control; if it is about explaining code, use Documentation.

Tip. Ask: Is software that people already use being changed, to fix a fault, keep up with new conditions, improve or tidy it?

How to Recognize It

Check whether the software has been released yet, and then what kind of change is being made to it.

  1. Is the software already released and in use?

    Yes means changes to it are maintenance. No means the work is still part of building it, an earlier phase of the Software Development Life Cycle.

  2. Is a reported fault being located and repaired?

    Yes means corrective maintenance, and the finding part is Debugging. The fix still has to be tested and released before users get it.

  3. Did something outside the program change, such as a new phone operating system, a new law or a retired library?

    Yes means adaptive maintenance: the code worked, but the world around it moved. A new feature users asked for is perfective maintenance instead.

  4. Is the question about keeping track of the changes, who made them and how to undo one?

    Yes means it is about Version Control, the tool that records each change; maintenance is the work of making the changes.

Code Maintenance vs Debugging vs Software Development Life Cycle vs Version Control

Fixing a bug in released software involves debugging, happens in the last phase of the life cycle and is recorded with version control, so all four turn up in one story. Ask whether you mean the ongoing work, finding one fault, the project's phases, or the change history.

Code Maintenance

Meaning
Code maintenance is all the work of changing software after release: fixing, adapting, improving and tidying it.
Key test
Software already in use is being changed, for any of four reasons.
Example
An app update adds dark mode, supports a new screen size and fixes a login crash.

Debugging

Meaning
Debugging is finding and correcting the cause of one specific error, before or after release.
Key test
One fault is being tracked down, at any stage of the project.
Example
Stepping through code to find that a loop starts at 1 instead of 0 is debugging.

Software Development Life Cycle

Meaning
The software development life cycle is the whole series of project phases, with maintenance as the last and longest.
Key test
About where maintenance sits among all the phases.
Example
Requirements, design, coding, testing and release, followed by years of maintenance.

Version Control

Meaning
Version control records every change to a project's files so that changes can be compared, shared or undone.
Key test
About tracking and undoing changes, not making them.
Example
git revert undoes last week's update that broke printing, without losing the work done since.

Formula & Notation

Worked Examples

Example 1 — Classify four maintenance tasks

Easy

A school's lunch-ordering app has been live for a year. Classify each task: (a) totals are wrong when an order has more than 9 items; (b) the next Android release removes a feature the app uses for notifications; (c) parents ask for a 'repeat last week's order' button; (d) the same price calculation is copied in 4 files, so the team merges it into one function.
Show the steps and the answer
  1. (a) is corrective.

    The app produces a wrong result; the change repairs existing behaviour.

  2. (b) is adaptive.

    Nothing in the app is wrong today; its environment is changing, and the app must change to keep working.

  3. (c) is perfective.

    It adds something users want that the app never did.

  4. (d) is preventive.

    Users see no difference, but with one copy of the calculation a future price change is made once instead of 4 times, with no copy forgotten.

Answer: (a) corrective, (b) adaptive, (c) perfective, (d) preventive.

Takeaway: Classify by the reason for the change: broken behaviour, a changed environment, a new want, or lowering future risk.

Example 2 — Rewrite from scratch or improve in steps?

Standard

A 4-year-old grade calculator has 3,000 lines, works for every teacher, and is messy. A developer proposes rewriting it from scratch in 8 weeks. While reviewing it, the team finds 37 special-case rules in the code, such as 'round 89.5 up to 90 only for final grades', that appear in no spec. Should they rewrite it or maintain it?
Show the steps and the answer
  1. Count what a rewrite would have to rediscover: 37 rules that exist only in the code.

    Each one was added to fix a real complaint; a rewrite that misses one brings back a bug that was fixed years ago.

  2. Compare risk: the current code works for every teacher, while a rewrite works for nobody until it is finished and fully tested.

    During those 8 weeks teachers still need fixes to the old version, so the team would be maintaining two programs at once.

  3. Choose step-by-step preventive maintenance: write tests that capture today's results, including all 37 rules, then clean up one module at a time and rerun the tests after each change.

    The tests turn hidden rules into checks, so the code can improve without losing any behaviour.

Answer: Maintain it: pin down current behaviour with tests (including the 37 special cases), then refactor one module at a time instead of rewriting from scratch.

Takeaway: Messy but working code holds knowledge; improve it in small tested steps rather than throwing it away.

Example 3 — Find the mistake: a fix that breaks the rule

Application

A shop app should charge no delivery fee on orders of 50 or more and a fee of 5 otherwise. A customer reports that an order of exactly 50 was charged 5. Ana finds the line 'if total > 50: fee = 0' and replaces it with 'fee = 0' to make sure nobody is overcharged. Find the mistake and correct it.
Show the steps and the answer
  1. Test Ana's version with an order of 20: the fee is now 0.

    She removed the condition, so every order ships free. The report was about one boundary value, not the whole rule.

  2. Find the real cause: total > 50 is false when total is 50, so an order of exactly 50 pays 5.

    The rule says 50 or more; the code says more than 50. The boundary is off by one.

  3. Make the smallest fix, 'if total >= 50: fee = 0', and test totals 49, 50 and 51: fees 5, 0 and 0.

    Values on both sides of 50 show the boundary is fixed and nothing else changed.

  4. Add 'an order of 50 has fee 0' to the test suite.

    If someone edits the line again, the test catches the old bug coming back.

Answer: Change 'total > 50' to 'total >= 50' instead of deleting the condition. Fees: 49 gives 5, 50 gives 0, 51 gives 0.

Takeaway: Find why the code fails before changing it, make the smallest fix, and keep a test for the case that broke.

Where You'll See It

  • A phone app that keeps getting updates

    Your favorite game app pushes updates that fix crashes (corrective), support a new phone screen size (adaptive), add a feature (perfective), and clean up old code (preventive) — that ongoing work is code maintenance.

  • Fixing a bug a friend found in your project

    When a classmate reports that your quiz app crashes on a blank answer, you read the existing code, find the cause, and patch it without rewriting the whole thing — that's corrective maintenance.

  • Updating an outdated library

    A library your project depends on releases a security fix; bumping to the new version to close the hole is adaptive/preventive maintenance, and skipping it leaves your app vulnerable.

Common Mistakes

  1. The slip: Once software is released, it is finished and needs no more work.

    Why it's tempting: Shipping feels like the finish line, and a working program seems 'done.'

    Instead: Maintenance typically consumes the majority (often 60-80%) of a program's total lifetime cost; software needs continual fixing, updating, and adapting to stay useful and secure.

  2. The slip: When old code is messy or buggy, the best move is to rewrite it from scratch.

    Why it's tempting: A clean rewrite feels easier than untangling someone else's confusing code.

    Instead: Rewriting throws away years of accumulated bug fixes and hidden requirements; incremental improvement is almost always less risky and cheaper than a full rewrite.

  3. The slip: All maintenance is just fixing bugs.

    Why it's tempting: Bug-fixing is the most visible kind of maintenance.

    Instead: Maintenance has four kinds: corrective (bugs), adaptive (new environments), perfective (improvements), and preventive (reducing future problems).


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. Classify each change as corrective, adaptive, perfective or preventive: (a) fix a crash on names with an apostrophe; (b) update the app for a new phone operating system; (c) add the dark mode users asked for; (d) split a 300-line function into small tested ones.

    Hint: Is it fixing, adapting to a new environment, improving, or heading off future problems?

  2. Before changing calc_shipping, you record that calc_shipping(2) returns 5 and calc_shipping(10) returns 12. After the change you get 5 and 15. What does this tell you?

    Hint: Compare the old and new results for each input.

  3. Your app uses a library at version 2.3.0. A security fix is released as 2.3.4. Using major.minor.patch numbering, what kind of update is this, and should you apply it?

    Hint: Which of the three numbers changed?

  4. You must change 5 functions. Plan A: change all 5, then test once. Plan B: change one, test, and repeat. If a test fails, how many functions might hold the bug in each plan?

    Hint: In Plan B, what changed since the last passing test?

  5. Today's release broke the login page, so the team rolls the app back to Tuesday's commit. Is the rollback code maintenance or version control?

    Hint: Is anyone changing the code, or restoring an earlier snapshot?

  6. A developer says: 'The old billing code is messy, so I'll delete it and rewrite it all this week. We don't need tests first.' What is the safer plan?

    Hint: What does the old code know that a rewrite might forget?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

What is technical debt?

Technical debt is the extra future work created by taking shortcuts now, such as copying code instead of writing a function, or skipping tests to hit a deadline. Like money debt, it collects interest: every later change takes longer because the shortcuts must be worked around. Preventive maintenance is how teams pay it back, by tidying the code before it slows everything down. Some debt is a sensible trade, as long as the team knows it is there.

What is refactoring?

Refactoring means changing the inside of code to make it clearer or easier to change, without changing what it does from the outside. Renaming a variable, splitting a long function into three, or removing duplicated code are all refactoring. It is a form of preventive maintenance, and it is only safe when tests exist: you run them before and after, and they must give the same results.

Why do some old programs keep running for decades?

Because replacing them is risky and expensive. Many banks and government offices still run programs written in COBOL, a language from 1959, and those programs work, having been fixed and adapted for years. A rewrite would have to rediscover every rule hidden in that code. In 2020, some US unemployment systems built on COBOL struggled with a surge of claims, and states had to look for programmers who still knew the language.

Learning Path

Code Maintenance

You are here

Next →

Version Control
You should know the Software Development Life Cycle, where maintenance is the last and longest phase, and Documentation, because you cannot safely change code you do not understand. Code maintenance adds the four kinds of change and a routine for making each one safely. Next, Version Control records every maintenance change so it can be reviewed, compared and undone if it breaks something.

See Also