Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Legacy code

Легаси-код

Existing code becomes hard to maintain when people still rely on it but the team can’t confidently predict what a change will break. That’s what developers usually mean by legacy code; it isn’t simply old code.

Also known as: легаси

What makes code legacy?

A system can run reliably every day and still be risky to change. Its source code might be the only record of a billing rule, or another application might depend on an output that nobody thought of as part of the public interface. What makes the code hard to maintain is the gap between what users rely on and what the team can confidently verify.

Michael Feathers uses a narrower definition: legacy code is code without tests. That framing gives you something concrete to look for before making a change. A ten-year-old parser with a dependable test suite may be easier to maintain than last month's checkout code, and a large test suite is no guarantee if it misses the behavior you're about to touch.

How to make a safe change

Start at a boundary you can exercise, such as a public function, a command-line tool, or an API endpoint. Find out who calls it, which outputs and side effects matter, and what happens at the edges of the input range. If the code is difficult to test in isolation, a focused integration test may be a better starting point than a large refactor to make unit tests possible.

A characterization test records what the code does today. It gives you a baseline for refactoring, but it isn't evidence that the behavior is correct. Check surprising results with someone who knows the business rule or with the teams that consume the output. Keep behavior-preserving cleanup separate from intentional behavior changes so reviewers can tell which is which.

Example: changing invoice rounding

Suppose you're asked to change how tax is rounded on invoices. Before editing the formula, run the existing invoice generator with a multi-item invoice where rounding each line and rounding the total give different answers. Capture the current result at the generator's public interface. Include an older account if it follows a different rounding rule. Then add a test for the new result on the affected accounts and retain the checks for accounts that should stay on the old rule. You now have a concrete way to catch a change that reaches more customers than intended.

Where it comes up in interviews

You might be asked whether to refactor a module or replace it. Explain what you would verify first, rather than proposing a rewrite because the stack looks dated. If replacement is necessary, migrating one slice at a time can let you compare results and switch traffic back if they differ unexpectedly. Also distinguish legacy code from technical debt: legacy describes code you still have to work with and may struggle to change, while debt describes the ongoing cost of a design choice, such as updating the same rule in several places. New code can incur that cost too.