On this page
- What is legacy code?
- Why legacy code is hard to change
- The first five steps
- 1. Build the start list
- 2. Pin current behavior with characterization tests
- 3. Break dependencies at a seam
- 4. Make the change under the tests
- 5. Refactor and delete, in that order
- Why a short start list works
- Tools that help at each step
- From an AI agent
- FAQ
Legacy code is code without tests: that is Michael Feathers' definition in Working Effectively with Legacy Code (Prentice Hall, 2004), and it is the one that helps you act. To work with it, rank the files that change often, have many dependents and little coverage, pin their current behavior with tests, then change them in small steps.
Part of the refactoring and debt cluster.
What is legacy code?
Feathers defines legacy code as "code without tests", because without tests you cannot tell whether a change preserved behavior. His argument is that code without tests is bad code however well designed it looks, and that with tests you can change behavior quickly and verify the result.
The usual meaning is looser. People say legacy when they mean old, inherited, written in a retired framework or owned by someone who left. Those are real problems, but they describe how a codebase feels. Feathers' version describes what you can do with it. A ten-year-old service with a solid suite is easy to change. Last quarter's module with no tests is legacy already.
That makes the definition measurable. You can count which files have tests, which a test run exercises and which change every week. See what is technical debt for how that debt builds up and the code smell glossary entry for the structures that make untested code hard to test.
Why legacy code is hard to change
Legacy code resists change because it is tangled and unverified at the same time. To add a test you need to instantiate the class, which needs a database, a clock, a network call and six collaborators. To break those dependencies you need to change the code. To change the code safely you need tests.
Feathers calls the way out a seam: a place where you can alter behavior without editing the code in that spot. An object seam lets you pass a substitute through a parameter or a subclass, a link seam swaps a library at link time, and a preprocessing seam is a macro in C or C++. Each gives a test a way in.
The first five steps
Begin with a start list, pin behavior, create seams, make one small change and clean up under the tests. Steps 2 to 5 follow Feathers' legacy code change algorithm (identify change points, find test points, break dependencies, write tests, make changes and refactor). Step 1 is the addition: choosing where to begin.
1. Build the start list
You cannot test a whole legacy codebase, so pick the files where the missing tests cost the most. A good start list has three properties: the file changes often, many other files depend on it and tests barely reach it. Those are untested hotspots, covered in depth in which important code has no tests.
By hand, three commands give you the inputs:
# Most-edited files in the last 90 days
git log --since="90 days ago" --name-only --pretty=format: | sort | uniq -c | sort -rn | head -30
# Line coverage per file (any runner that writes lcov)
pytest --cov --cov-report=lcov:coverage.lcov
# Who imports a module (replace the name)
grep -rn "import billing_core" src | wc -l
Intersect the lists: high edit count, high importer count, low coverage. With repowise the same ranking is a lookup:
uv tool install repowise
repowise init
repowise coverage add coverage.lcov
repowise health
repowise health flags an untested_hotspot when a file is a git hotspot, has at least 4 dependents and has line coverage below 40%, with severity CRITICAL at 15% coverage or less and 10 or more dependents (test intelligence layer). Without a coverage report it flags a file only when nothing shows a test reaching it, by name or through the dependency graph. The top of that list is where you start. For a reading-first pass on a repo you have just inherited, use how to read a codebase you didn't write.
2. Pin current behavior with characterization tests
A characterization test records what the code does today, not what it should do. Call the function with realistic inputs, capture the output and assert on it. If the output is surprising, the test still passes: you are building a net, not judging the design.
def test_invoice_total_today():
invoice = build_invoice(items=[("widget", 3, 9.99)], region="EU")
# Run once, copy the real value into the assertion.
assert calculate_total(invoice) == 35.67
Feathers' method is to write an assertion you know will fail, run it, and let the failure show the actual behavior. Snapshot and approval testing tools automate the same loop for large outputs. Keep these tests narrow: the goal is to detect an unintended change around the file you are about to touch, not to cover every branch.
3. Break dependencies at a seam
Once a test can reach the code, the next obstacle is what the code reaches. Use the smallest move that gets the class under test: extract an interface for the database client, pass the clock in as a parameter, wrap a static call in a method you can override. Feathers catalogs dependency-breaking techniques by name, and a few of them (parameterize constructor, extract and override call, extract interface) cover most cases.
These edits are the risky part, because the tests do not exist yet. Keep each one mechanical, use automated refactorings from your IDE where they exist, and make no behavior changes in the same commit.
4. Make the change under the tests
Now do the work you came for: the bug fix or the feature. Two patterns keep new code clean when the surrounding code stays messy. A sprout method (or sprout class) puts new logic in a new, tested unit and calls it from the old code with one line. A wrap method renames the old method and puts a new one in front that calls it plus the new behavior. Both leave the legacy body untouched, so the characterization tests from step 2 keep passing.
5. Refactor and delete, in that order
With the tests green, tidy the file you just touched, using the clean code principles as the target. What is refactoring covers the named techniques. Before spending time on a function, check whether anything calls it. Dead code is the cheapest legacy to remove, because code that nothing runs needs no test, and repowise dead-code lists unreachable files and unused exports with a confidence score for each. See how to find dead code in a repository.
Then go back to step 1. The start list is a rolling worklist, not a one-time audit: each file you cover drops off it, and the next one moves up.
Why a short start list works
Change concentrates in a small share of files, so a start list of a few dozen files covers most of the risk. We analysed 195 of the most-starred public repositories indexed on repowise.dev (1,000+ GitHub stars each, 253,688 production source files, November 2026). Production code only, measured per repository:
| Finding (median repo) | Value |
|---|---|
| Share of 90-day line churn in the busiest 10% of production files | 68% |
| Share of 90-day line churn in the busiest 5% of production files | 52% |
| Code health, all production files (0 to 10) | 7.1 |
| Code health, hotspot files only (0 to 10) | 4.9 |
| Share of bug-fixed files in the least-healthy 20% of files (173 repos) | 60% |
| Production files with a bus factor of 1 | 60% |
Scroll the table sideways to see every column.
Two things follow for legacy work. The files you change most are the hardest to change safely: in 174 of 186 repositories (94%) the hotspot files scored below the repository average, by a median of 2.0 points. And the files with the weakest health held 60% of the files that needed a bug fix in the last 180 days, an association on history, not a forecast. Testing and cleaning those files first goes where the defects have been.
Knowledge matters too. Across the cohort, 34% of 20,941 hotspot files had a bus factor of 1, meaning one author wrote 80% or more of the commits. A characterization test is a way to write down what that person knew. See what is bus factor.
Tools that help at each step
| Step | What you need | Options |
|---|---|---|
| Start list | Churn, dependents and coverage in one ranking | repowise health, CodeScene, a git log script |
| Characterization tests | Record current output | pytest, Jest snapshots, ApprovalTests |
| Dependency breaking | Safe mechanical edits | IDE refactorings, OpenRewrite, ts-morph |
| Understanding | Map what depends on what | repowise graph, Sourcegraph, IDE find-usages |
| Dead code | Find what nothing calls | repowise dead-code, Knip, vulture |
Scroll the table sideways to see every column.
For a side-by-side of the tools that help you map an unfamiliar system, see best tools to understand a legacy codebase.
From an AI agent
Give the agent the start list before the task. An agent editing a legacy file without tests has the same problem you do: nothing tells it when it broke something. repowise exposes the ranking to agents over MCP: get_risk on a file returns its risk signals before the edit, and get_change_risk scores the diff afterwards. Ask the agent to write characterization tests first, then make the change, in the order above. The same approach holds for mainframe code, where the missing context is copybooks and JCL: see can Claude understand COBOL.
FAQ
- What is legacy code?
- Legacy code, by Michael Feathers' definition in Working Effectively with Legacy Code, is code without tests. Without tests you cannot verify that a change kept behavior, so the code is risky to modify whatever its age, language or author.
- What is the difference between legacy code and technical debt?
- Technical debt is the cost of past shortcuts that makes future change slower. Legacy code is the subset where you also lack the tests to change it safely. Most legacy code carries debt, but well-tested old code can carry little of either.
- How do you start refactoring legacy code?
- Rank files by churn, dependents and coverage, write characterization tests for the top of the list, break dependencies at a seam, then refactor under the tests. Do not refactor a file before it has tests, and do not rewrite what you cannot yet pin down.
- What is a characterization test?
- A characterization test asserts what the code currently does, not what it should do. You run the code, capture the real output and encode it as the expected value, so any later change in behavior fails the test.
- Should you rewrite legacy code or refactor it?
- Refactor in place by default. A rewrite throws away behavior that nobody documented, and the new system arrives without tests as well. Rewrite a module only when you can run old and new side by side against the same inputs.
- How do you find which legacy code to test first?
- Start with files that change often, are imported by many other files and have low coverage. repowise flags these as untested hotspots (a git hotspot with at least 4 dependents and line coverage below 40%). By hand, intersect
git logedit counts, an lcov report and an importer count. - Is Working Effectively with Legacy Code still worth reading?
- Yes. The book is from 2004 and its examples are in Java, C++ and C#, but the central ideas (seams, characterization tests, sprout and wrap) apply in any language. Read the dependency-breaking chapters when you hit a specific obstacle.



