Saturday, July 11, 2020

Legacy Code

In his book Working Effectively With Legacy Code, Michael Feathers gives a clear definition of what Legacy Code means to him:

To me, legacy code is simply code without tests.

Without tests, it’s usually very hard to know everything a code can do. If you need to understand what the code is doing, you need to read it carefully, play the computer in your head and envision all the possible scenarios. You can also test it manually to see what it does. Generally, code without tests is tricky to change without introducing a regression somewhere.

This is my [Nicolas Carlo] definition of Legacy Code.

Legacy Code is valuable code you’re afraid to change.

You need to realize a few things:
  • Unfamiliarity with the code plays a lot. We overestimate the complexity of unfamiliar code. This is why you think this code you didn’t write is Legacy Code. And, yes, your past self often does silly mistakes that result in unfamiliar code for future you.
  • Good tests make you comfortable changing unfamiliar code. Hence Feathers’ definition. But poor tests won’t.
  • It gets better after a few months. Keep that in mind if you started on working on a legacy project and you’re struggling. I’m not saying the code is great, but you’ll get used to it and understand its quirks and specificities better.
  • Most of the code is terrible because it’s the result of many people working on it, over a long period of time, with conflicting requirements, under time pressure. Knowledge is imperfect and shortcuts are taken to meet the deadlines. That’s VERY common. Eventually, you’ll reach a state where every move introduces a bug and any feature takes forever to be implemented.
The following definition states something people fail to realize: Legacy Code is a personal point of view.

Legacy Code is the code you need to change and you struggle to understand.

It depends on your understanding of the code. And your feeling about changing it.

Learning to understand Legacy Code is essential to be productive. You can try to avoid it and feel bad when you’re stuck with it… Or you can see this as an opportunity to develop valuable skills that will make you stand out as a great developer.

Original article, from which this is excerpted, was written by Nicolas Carlo who lives and works in Montreal, Canada 🍁 He founded the Software Crafters Montreal community which cares about building maintainable softwares.


For me [Doug], legacy code is:
  • written without best current practices, for example, tests
  • authored by someone else (or me long enough ago I've forgotten it)
  • has stood the test of time and is part of a successful product.
As Nicolas Carlo stated, legacy code is valuable.


No comments:

Post a Comment