Working Effectively with Legacy Code

Working with legacy code is a unique challenge.  If you ever find yourself in such a situation I recommend you take a look at the book by Michael Feathers Working Effectively with Legacy Code.  Working in Java development for over ten years I have worked with a lot of legacy code.  Each legacy application is setup differently.  Many times a legacy application has been neglected for a long time.  There can be secrets lurking in every file.

Michael Feathers book addresses  one of the big problems with legacy code is doesn’t have any tests.  So he talks about how we can create test harness to put around the code base.  This is not easy to do as legacy code wasn’t designed to be testable, think methods as long as novels.  The spaghetti code is difficult to isolate and test.  We could refactor the code to make it testable, but we need to unit test it before we refactor.  The catch-22 of legacy code is maddening.

The plan put forth by Michael Feathers is to make minimal changes to enable the code to be testable.  Once you make the code changes to get the tests running you now can make design better.  To make the code easier test Feathers recommends removing dependencies and add barriers in the code, for two main reasons.

  1. Sensing: to check and verify the effects of piece of code.
  2. Separation: to get the specific code into the test harness

Removing dependencies is very tough.  It is important not to get too extravagant with the changes.  You might have override some methods and perhaps replace a class with an stub class to make the test work.  Another important part of the book is the discussion of seams.  The code seam is bit of code that we can modify and it doesn’t change the code.  These code seams create separations in the code you which you want to test.  This also allows you to sense the action of the code under test.

No comments yet.

Leave a Reply

Powered by WordPress. Designed by Woo Themes