Working Effectively With Legacy Code

One summary

“Working Effectively with Legacy Code” by Michael Feathers is a comprehensive guide for software developers to effectively maintain and update legacy code. The author defines legacy code as code without tests, which makes it difficult to understand and modify without introducing new bugs. He emphasizes that understanding the existing code is the key to making changes to it effectively.

The book covers a range of techniques and strategies for dealing with complex and untested code. One of the main approaches is refactoring, where the author suggests gradually improving the code rather than trying to rewrite everything from scratch. He also stresses the importance of creating tests for the code to increase confidence in changes made to it. The author provides techniques for separating code into testable units and creating test harnesses.

In addition to refactoring, the author covers other strategies for working with legacy code, including dependency injection, which allows developers to minimize the impact of changes by isolating parts of the code that are difficult to test. He also covers the concept of “seams” which are places in the code where developers can introduce hooks to allow for testability and controlled modification.

Throughout the book, the author provides real-world examples and case studies to illustrate the concepts and techniques he covers. He also offers advice for navigating organizational challenges, such as resistance to change, and provides tips for gaining support for refactoring efforts.

Three actions

  1. Refactor Gradually: Refactor code incrementally, making small, controlled changes to the existing code rather than trying to rewrite everything from scratch. This helps to minimize the risk of introducing new bugs and makes it easier to understand the code.
  2. Create Tests: Create tests for the code to increase confidence in changes made to it. Tests also help to identify when changes have broken existing functionality and allow developers to make changes more safely.
  3. Isolate Code with Dependency Injection: Use dependency injection to minimize the impact of changes by isolating parts of the code that are difficult to test. This makes it easier to modify the code and reduces the risk of introducing new bugs.

Five quotes

  1. “Legacy code is simply code without tests.”
  2. “The only way to change a system that has no tests is to add tests to it.”
  3. “The first step in working with legacy code is to understand it.”
  4. “Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.”
  5. “When we change software, we want to be confident that the change we made was correct. Confidence comes from tests.”