My favorite definition of a “legacy system” is “a system without automated unit tests”. After spending my entire working life (nearly 25 years) in software development, and having worked for over 3 years in the IT division of a huge organization on one-million-line projects that have virtually no automated unit tests, I can confidently say with no doubts that automated unit tests are an absolute, total, unqualified, 100% must-have for the medium- to long-term viability of a large project. There are many reasons for this, but the most important one is this: I have seen many developers in this organization almost tremble in fear at the thought of having to make a change to production code, lest it result in an expensive and high-profile production outage. The lack of unit tests to catch functional regression is so fearsome that teams have written totally new systems rather than try to work with existing ones. Of course, said teams write legacy code right from the start because they still don’t create unit tests, so their successors will throw away their code, too.
I cannot even begin to estimate the massive waste of resources, the frustration, the stress, the long hours - all totally unnecessary - as a result of not having automated, properly designed and executed unit tests. I’m not going to go into what constitutes a proper unit test - there’s not enough time for that and there are plenty of resources out there.
What many people don’t realize is that the mere fact of having thorough unit test coverage ensures that you have testable code. Testable code is in general better designed and more flexible than the alternative. It has to be by the very nature of testability, which requires a great degree of decoupling and reusability (namely, the code needs to be “reused” by the test cases).
In addition, unit tests help to answer many questions about what the code is supposed to do. The reality of many large IT divisions is that there is never adequate documentation, and there are literally millions of lines of code that people simply don’t understand and are afraid to touch. A decent unit test suite would go very far towards compensating for the lack of other information about what all that code does.
However, from what I have seen, the likelihood that the corporate IT developer will create anything approaching good (or any!) unit tests approaches zero. Sorry to be negative, but I have just seen too much (or, perhaps I should say, too little). Quite frankly, I have come to believe in Sturgeon’s Law, which I will paraphrase: 90% of software is crap, because 90% of everything is crap. Have a great day.