Legacy Code
Inherited code that is difficult to change safely due to lack of tests, documentation, or understanding.
Also known as: Heritage Code, Inherited Code
Category: Software Development
Tags: software-development, code-quality, maintenance, refactoring, testing
Explanation
Legacy Code, as defined by Michael Feathers, is simply 'code without tests.' More broadly, it refers to any codebase that developers are afraid to modify because they cannot confidently predict the impact of changes. Legacy code is not necessarily old or bad - it's code that has become difficult to work with safely.
Characteristics of legacy code:
- **No automated tests** or inadequate test coverage
- **Missing or outdated documentation**
- **Original developers unavailable** to explain design decisions
- **Tightly coupled components** that resist modification
- **Implicit knowledge** required to understand behavior
- **Fear of change** among current maintainers
Why legacy code accumulates:
1. Time pressure prioritizes features over maintainability
2. Team turnover loses institutional knowledge
3. Technologies evolve, making old code feel alien
4. Technical debt compounds over time
5. 'Working code' is left alone even when problematic
Strategies for dealing with legacy code:
- **Characterization tests**: Write tests that document current behavior before changing
- **Strangler fig pattern**: Gradually replace old code with new
- **Seams**: Find safe places to insert tests and make changes
- **Boy Scout Rule**: Leave code cleaner than you found it
- **Incremental refactoring**: Small, safe improvements over time
The key insight is that legacy code isn't a permanent condition - with disciplined effort, any codebase can be brought under test and made maintainable again.
Related Concepts
← Back to all concepts