The Key Points of Working Effectively with Legacy Code
5 days ago
- #Legacy Code
- #Refactoring
- #Software Testing
- Legacy Code is defined as code without tests, a concept from Michael Feathers' book 'Working Effectively with Legacy Code'.
- The primary approach to working with legacy code is to first add tests to ensure behavior preservation before making changes.
- A key challenge is the 'Legacy Code dilemma': needing to change code to add tests, but needing tests to safely change code.
- Identify 'Seams' in the code—places where behavior can be altered without changing the source code—to break dependencies for testing.
- Unit tests should be fast (<100ms per test) and not interact with infrastructure to be reliable.
- Characterization tests capture the current behavior of code to create a safety net for refactoring.
- Use 'Sprout' and 'Wrap' techniques to add new code to legacy systems with minimal risk when time is limited.
- Scratch refactoring helps familiarize with opaque code by making temporary changes that are reverted after understanding is gained.
- Avoid direct dependencies on library implementations to prevent lock-in and ease future upgrades.
- Despite being written in Java/C++, the book's principles are language-agnostic and highly applicable to modern development.