Paul asks:
Suppose one had a relatively large program (say 900k SLOC in C#), all commented/documented thoroughly, well organized and working well. The entire code base was written by a single senior developer who no longer with the company. All the code is testable as is and IoC is used throughout—except for some strange reason they did not write any unit tests. Now, your company wants to branch the code and wants unit tests added to detect when changes break the core functionality.
Is adding tests a good idea? If so, how would one even start on something like this?
See the original question here.
Don’t unit test just to unit test
Karl Bielefeldt answers (14 votes):
Not all unit tests have equal benefit. The benefit of a unit test comes when it fails. The less likely it is to fail, the less beneficial it is. New or recently changed code is more likely to contain bugs than rarely changed code that is well tested in production. Therefore, unit tests on new or recently changed code are more likely to be more beneficial.
Not all unit tests have equal cost, either. It’s much easier to unit test trivial code you designed yourself today than complex code someone else designed a long time ago. Also, testing during development usually saves development time. On legacy code that cost savings is no longer available.
In an ideal world, you’d have all the time you need to unit test legacy code, but in the real world, at some point it stands to reason that the costs of adding unit tests to legacy code will outweigh the benefits. The trick is to identify that point. Your version control can help by showing you the most recently changed and most frequently changed code, and you can start by putting those under unit test. Also, when you make changes going forward, put those changes and closely related code under unit test.
Following that method, eventually you will have pretty good coverage in the most beneficial areas. If you instead spend months getting unit tests in place before resuming revenue-generating activities, that might be a desirable software maintenance decision, but it’s a lousy business decision.
Loading comments...