TDD and Legacy Code

TDD is a powerful way to develop new code. However, most organizations have significant existing code that was not developed this way. This “legacy code” is often difficult to test because it was not designed to be testable in the first place.

In his excellent book, Working Effectively with Legacy Code, Michael Feathers outlines techniques for dealing with this kind of code, with an eye toward making it more testable. This book is an essential resource for anyone that has significant legacy code.

But how do we incorporate these activities into TDD? Organizations can’t afford to simply stop and fix all their legacy code.

Focus on occasions when legacy code must be changed. We begin by using Feathers’ techniques to get the portion of the code that will need to change under some kind of test. This may not be a “good” test in the TDD sense. But once such a test exists it will reflect the current behavior of the system. It is then changed to reflect the new requirement and allowed to run and fail. The system is then updated to make it pass.

If changes to legacy code are always accomplished this way, then the system will always be continuously improving, and gradually can be brought under the TDD umbrella. This will be incremental, but also very realistic.

This is Scott Bain. Visit us at

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.