In my last post What the Heck is a Unit Test I mentioned the term Legacy Code and some might be wondering what the heck that is.
Legacy Code in short is any piece of code not under test.
You could be writing Legacy Code right now.
But wait isn’t legacy code old? How can I be writing legacy code now, my code is new.
Well there is some wibbily wobbably timey wimey going on here. Imagine that you’ve just been hired on at a company to write a new GUI application as part of their core business. Your job is to write a GUI, the business logic already exists and was written by a developer no longer with the company and no one else knows anything about it except that it works for the most part so don’t touch it.
Your code is new, sleek, modern, and you know it line for line. To you the business logic is just grandfathered in black-boxed code that is old and scary. You don’t want to touch it because you don’t know if you’ll break something which would be hard to find and hard to fix. In other words to you its Legacy Code compared to you new modern code.
Lets fast forward 2 years and you have won the lotto and are now living on your own private island. The company hired on a new developer to replace you. They are asked to develop some new features into the application. To them their new features are modern, sleek, and sexy. The existing undocumented code, both the business logic and the GUI, are just some old unfamiliar code written by some faceless developers they never met. To them your sleek and modern GUI is a scary black box they don’t want to change because they don’t know if they will break anything. In other words Legacy Code.
So you see because we don’t write code in isolation and its inevitable that someone else will eventually take over the sustaining of code we write it does not matter if the code was written 2 years ago or 2 years from now if its not under test its legacy code. Remember the developer who wrote that business logic probably thought their code was modern, sleek, and sexy too once upon a time.
The theme above was that the legacy code is the code whose behaviour is undocumented and has the it an’t broke so don’t fix it stigma on it because of a fear that any changes to it will introduce unexpected bugs due to a lack of familiarity the current development team has with it.
Sound familiar? A fear of change and unknown code behaviour?
How can we resolve these issues so we don’t have to fear change in the legacy code?
That’s right say it with me, Unit Tests.
Once the Legacy Code is under tests we wont have to fear change since the codes behaviour will be documented by the tests and any unexpected changes to that behaviour will be detected and located before we commit by the unit tests. Now that the code is under test it no longer satisfies the definition of Legacy Code and will now be code any developer past, present, and future wont be afraid to change.
So that is what the heck Legacy Code is. Code that isn’t under test so its behaviour is unknown and changes to the code is feared as its likely unexpected bugs will get introduced.
Until next time think imaginatively and design creatively