Managing technical debt
Find the debt that really slows your team, measure its interest, and pay it down in small steps the business agrees to.
Updated since you finished it. We changed this lesson after you marked it done. Your progress is kept.
What broke
This story is made up, but it is common. Ravi works on the pricing service of a food delivery app called FoodNow. Three years ago, the team copied the discount logic into three places to hit a festival launch. Everyone agreed to clean it up the next month.
Next month never came. Each new offer now needs changes in three places. Engineers forget one of them about once a quarter. Last Diwali, customers saw one price in the cart and another at checkout. Support got 4,000 tickets in one evening.
The team was frustrated. A senior engineer proposed rewriting the whole pricing service in a new language. "Six weeks," he said. The rewrite started. Six months later it was still not finished. Meanwhile, the old service still needed every offer, so the team was maintaining two systems.
The problem was real. The fix was wrong. Nobody had measured where the debt hurt most, and nobody paid it down in small steps.
The idea in simple words
Ward Cunningham first used the debt metaphor in 1992. He wrote: "Shipping first time code is like going into debt." 1 He added that a little debt speeds development "so long as it is paid back promptly with a rewrite." 1 He did not say that messy code is fine. The danger, he wrote, comes when the debt is not repaid. Then "every minute spent on not-quite-right code counts as interest on that debt." 1
Martin Fowler explains the parts. The messy code, which he calls cruft, is the debt. The extra effort it adds to each new feature is the interest. Cleaning up the cruft is paying back the principal. 2
Debt is not always bad. Fowler sorts it on two axes: reckless or prudent, and deliberate or inadvertent. 3 Each kind has a typical quote:
- Reckless and deliberate: "We don't have time for design".
- Prudent and deliberate: "We must ship now and deal with consequences".
- Reckless and inadvertent: "What's Layering?"
- Prudent and inadvertent: "Now we know how we should have done it".
Fowler writes that "the useful distinction isn't between debt or non-debt, but between prudent and reckless debt." 3 He also says the prudent and inadvertent kind is "inevitable" for teams that are excellent designers. They learn the right design while they build. 3
The key idea. Debt only costs you when you touch the code. Pay down the debt in the code you change often, in small steps, and only when the interest is larger than the cost of fixing it.
How it works
Step through one debt item, from the day someone notices it to the day its interest drops. Use Next and Back, or the ← and → keys.
- An engineer notices that the discount logic is copied in three places. She adds one line to the team's debt register. Nothing is fixed yet.
- The team measures the interest. Each offer change takes 3 days instead of 1. The code changed 40 times last year and caused 2 incidents. It is a hotspot.
- The team compares the item with planned features. Eight offer changes are coming, so the cleanup pays back soon. The PM agrees to 2 days of cleanup per sprint.
- The debt is paid in small steps that each ship alone. First tests, then one shared module, then deleting the old copies. Features keep shipping.
- The team measures again. Offer changes now take 1 day, and there are no pricing incidents. The register item is closed.
1. Write the debt down
Keep a short debt register: a list in your tracker, one line per item. Each line says where the debt is, what it slows down, and who noticed it. A register turns "the code is bad" into items you can compare.
2. Measure the interest
Interest is what the debt costs you now. Look for three signals:
- Time. How many extra days does each change take? Ask the engineers who made the last few changes.
- Change frequency. How often does this code change? Your version control history tells you.
- Incidents and bugs. How many incidents or bug fixes touched this code?
Fowler notes that you only pay interest when you work in that part of the code. So "crufty but stable areas of code can be left alone." 2 But "areas of high activity need a zero-tolerance attitude to cruft." 2
This is the idea behind a code hotspot: code that changes often and is hard to work with. CodeScene, a tool for this analysis, says most development activity happens in relatively few modules. 4 Its advice: low code health in a hotspot is expensive, so improve it first. Low health in stable code has lower priority. 4 In one example in its docs, the hotspots were 1.2% of the code but held 45% of the bugs found and fixed. 4
Will Larson also starts with hot spots when he lists ways to improve quality. First fix the hot spots that cause problems now. Larger programs come later. He advises starting with the lightest solution that works. 5
3. Decide what to pay down
Debt work competes with features, so compare them in the same units. Cost of delay helps here. It combines how valuable a piece of work is with how urgent it is. 6 A related rule, CD3, divides cost of delay by duration. It favours work that is both valuable and quick. 6
For debt, ask: what does each month of waiting cost us in slower changes, bugs and incidents? Compare that with the features you would delay.
4. Pay in small steps
There are four common ways to pay:
- The Boy Scout rule. Robert C. Martin's version is "Always check a module in cleaner than when you checked it out." 7 Rename one variable or split one long function. 7
- Fix it inside the feature. Fowler calls this opportunistic refactoring: fix unclear code when you see it. He says a team that refactors well "should hardly ever need to plan refactoring." 8 So clean up the pricing code inside the next pricing feature, and include that time in its estimate.
- Dedicated time. For larger items, agree on a fixed share of each sprint with your PM. This is the lesson's advice, not a rule from a source. The share matters less than doing it every sprint.
- A rewrite. This is the riskiest choice. Joel Spolsky called rewriting code from scratch "the single worst strategic mistake that any software company can make." 9 His reason: "Old code has been used. It has been tested. Lots of bugs have been found, and they've been fixed." 9 He used Netscape as the example. When he wrote, its last major release had come out almost three years earlier. 9 Treat this as one strong opinion. Some rewrites are needed. But replace the system piece by piece, behind the same interface, and ship each piece.
5. Make the case in business terms
Talk about interest, not code style. "Each pricing change takes 3 days instead of 1. We plan 8 this quarter. Two days of cleanup per sprint pays back by March." Bring the incident count as well. See working with PMs and the business for how to frame options and costs.
Trade-offs
| Way to pay | Risk | Cost to the roadmap | Use it when |
|---|---|---|---|
| Boy Scout rule | Low. Small, reviewed changes. | Almost none. | Always, in the code you touch. |
| Fix inside a feature | Low. Tests from the feature cover it. | The feature takes a bit longer. | The next feature works in the same code. |
| Dedicated time each sprint | Low to medium. | A fixed, visible share. | Debt in hotspots that no feature will touch soon. |
| Big cleanup project | Medium. It can grow quietly. | Weeks with fewer features. | One measured hotspot, with a clear end. |
| Full rewrite | High. Lost bug fixes, two systems for a long time. | Months with little new value. | Rarely. Prefer replacing piece by piece. |
| Leave it alone | Low, while the code stays stable. | None. | The code is stable and rarely changes. 2 |
Simple rule: pay down debt where you change code often, in steps small enough to ship every week.
At work: how a senior uses this
- Keep a debt register. Review it once a month with your PM. Remove items nobody has touched.
- For each item, write the interest in days, bugs or incidents, not in adjectives.
- Before you propose cleanup, check the change history. If the code rarely changes, leave it.
- When you take on deliberate debt, write the ticket and the payback date in the same pull request.
- Include cleanup time in feature estimates for messy code. Do not hide it, and do not ask for it separately each time.
- When someone proposes a rewrite, ask: "Can we replace one piece first and ship it?"
- After paying down an item, measure again. Show the PM that the change time dropped.
With AI
What to ask the agent
"Here is the git log for the last 12 months as a list of files and change counts. Rank the 10 files that changed most, and for each one, list the size and the number of commits that mention 'fix' or 'bug'."
"Read this module. List the duplicated logic and the riskiest parts to change. Suggest three refactoring steps, each small enough to ship alone with its own tests."
"Turn this debt item into a short note for my PM: what it costs us each month, the fix in steps, and when it pays back."
What to check yourself
- The ranking uses real history, not guesses. Run the git command yourself and compare.
- Each refactoring step keeps behaviour the same. Tests must pass before and after every step.
- The agent did not "clean up" odd code that is really a bug fix. Check the commit history of strange lines before you delete them.
- The cost numbers in the PM note come from your team, not from the agent.
- An agent can rewrite a module fast. That does not make a rewrite less risky. Review it like any other large change.
Check yourself
Your progress is saved only in this browser. Export it to keep it safe.
You have finished 5 lessons. Your progress lives only in this browser. Export a copy so you don’t lose it.
Go deeper
Show 9 sources and related lessons
- Ward Cunningham, “The WyCash Portfolio Management System”, OOPSLA '92 Experience Report (1992) (opens in a new tab)
The short report where the debt metaphor first appears, with "interest" on not-quite-right code. - Martin Fowler, “Technical Debt” (bliki, 2019) (opens in a new tab)
Cruft, principal and interest, and why stable messy code can be left alone while busy code cannot. - Martin Fowler, “Technical Debt Quadrant” (bliki, 2009) (opens in a new tab)
Reckless or prudent, deliberate or inadvertent, with one quote for each of the four kinds of debt. - CodeScene documentation, “Technical Debt” (hotspots guide) (opens in a new tab)
How a hotspot analysis combines change frequency with code health to rank debt, with a worked example. - Will Larson, “Manage technical quality”, StaffEng (opens in a new tab)
Seven ways to improve quality, from fixing hot spots up to a quality program, and why to start light. - Black Swan Farming, “Cost of Delay” (opens in a new tab)
Cost of delay as value plus urgency, and CD3 for ranking work. - Robert C. Martin, “The Boy Scout Rule”, in 97 Things Every Programmer Should Know (O'Reilly, 2010) (opens in a new tab)
Check a module in a little cleaner than you found it, with small examples. - Martin Fowler, “Opportunistic Refactoring” (bliki, 2011) (opens in a new tab)
Fixing unclear code as you meet it, and why good teams rarely need to plan refactoring. - Joel Spolsky, “Things You Should Never Do, Part I”, Joel on Software (2000) (opens in a new tab)
The classic argument against rewriting working code from scratch, with Netscape as the example.
Related: Working with PMs and the business · Refactoring and legacy code (coming soon) · Making architecture trade-offs · Running large migrations (coming soon) · Estimation and planning under uncertainty