Legacy is a term commonly used with a positive meaning, if I’m talking about property law I may have inherited a wonderful house, or a big pile of money. In Italy the legacy of the roman empire are really beautiful buildings. But at work when I say I work on legacy code, everyone knows why I’m sad.
Nobody expect me to mean that I’m working on the Colosseum of code. A beautiful, well crafted piece of art, a bit aged, but wonderful to look at. No, they know I’m probably working on a big pile of mud. With no clear design, no documentation, and no tests.
So what is legacy code?
There are many definitions of it. It depends on who you ask, but in my opinion there are two main factor that indicate that you are working on legacy code:
- It is code that is in production
- It is code that you are afraid to change
If this piece of code is not in production, nobody uses it, it can’t be called legacy code, it’s just old code, retired. Nobody cares about it. One important thing to remember is that legacy code is code that is working, and the users need it to continue working, either because they need it directly or because some other system depends on it.
The second point is the reason that we hate legacy code. We hate it because we are scared of it. This code is fragile, is complicated, is a mess. Legacy code is code that resists change, it takes an extraordinary effort to change something small and simple. This is because we probably didn’t write it, so we don’t know all its little secrets, or we forgot most of them. A small change in a component might break one or two other components that shouldn’t depend on each other.
Legacy code is code without tests
This quote comes up a lot when talking about legacy code, and many people criticize it. They said essentially that it isn’t true, you can have code that is not legacy without unit test.
What I think they are missing is that given enough time, code without unit tests will become legacy code (if not retired). The amount of work required to keep a testless codebase sane, is orders of magnitude higher than a properly unit tested codebase.
That’s why you can say that code without unit test is legacy code. Because maybe not today, maybe not tomorrow but quite soon it will become legacy code. Sooner or later the business won’t keep pouring money at it, developers will have just enough time to make quick changes and the code will rot.