“Legacy Code is code without tests” If you’ve come across that definition, it’s from Michael Feathers’ book: Working Effectively with Legacy Code. While I have a slightly extended definition, this is a very valid and useful one! Feathers’ book is from 2004. Yet, its content doesn’t get outdated. There is a reason for that and this CommitStrip puts it best: This book is a reference. Probably THE reference. When there’s a thread about Legacy Code, it doesn’t take long for someone to drop a comment suggesting you read it. I didn’t read it. I’ve seen it’s recommended. But what are the key points of that book? If that’s you, I got your back! Here’s my summary of the salient points of the book and how they can help you deal with your existing codebase. First, add tests, then do your changes The challenge with changing existing code is to preserve the existing behavior. When code is not tested, how do you know you didn’t break anything? You need feedback. Automated feedback is the best. Thus, this is the first thing you need to do: write the tests. Only then you’ll be safe to change the code and refactor. Your goal is to get there. The point of the book is to show you how you can get there when you have to deal with an impossibly convoluted codebase. Which leads us to the next point… Adding tests: the Legacy Code dilemma Before you change code, you should have tests in place. But to put tests in place, you have to change code. This is the paradox of Legacy Code! So, how do you go about it? Are you doomed? You’re not. But you should be extra careful until you got tests in place. You should perform minimal, safe refactorings. Change as little code as possible to get tests in place. The recipe is: Identify change points (Seams) Break dependencies Write the tests Make your changes Refactor Once you get to the tests, you know how to proceed. The first two points are the difficult ones. Identify Seams to break your code dependencies Adding tests on the existing code can be chall...
First seen: 2025-09-08 18:47
Last seen: 2025-09-09 13:57