On a panel several years ago, I was asked what was the greatest benefit that Agile had delivered to me personally. It took me no time to respond "unit testing." (While this answer is not historically accurate — unit testing precedes the Agile movement — it's clear that the Agile exponents made it a widespread practice. In large part, because of Kent Beck's lapidary JUnit implementation, which has been widely copied to most major languages.)
The specific benefit I — and many other developers — have enjoyed is quite simply less time spent in the debugger. Today (and for years now), I write code and then I write unit tests that exercise the edge cases and one or two main cases. Right away, I can tell if I missed something obvious or if my implementation has a slight burble that mishandles cases I expected to flow through easily.
This doesn't mean I can assert that I now feel comfortable my code is right. But what I do know is that it's better because it's been partially tested. The result of this is that the long sessions in the debugger trying to find why, for example, some data packets have an unexpected zero in the last byte, are pretty much done with. An error like that — invalid values or an improper format — will be caught right away by my tests. Previously, I'd have to wait for the bug to surface, then trap it, back up through code, examine and test it, and keep following the data up the call chain only to discover that at the very start when I created a substring, I miscounted by one the number of bytes to extract. Those days of 3 AM debugging are gone. Most of my debugger sessions today are about logic errors that transcend the unit level. I am happier and more productive.
I also enjoy another considerable advantage that I did not have prior to unit tests. As the collection of tests grows, the tests themselves become monitors of sorts that watch over my code to make sure it has not shifted unexpectedly beneath me. Because my code coverage is in the 75%-80% range, I have a lot of these monitors watching out for me. I run them all as a regression sequence any time I make a change that might have more than local impact. When I make a big change, I can tell right away if I have unhooked anything. For example, the other day, I changed the name of a major package. There were references to it throughout my code base. I had the IDE do the refactoring. When the compilation worked the first time, I felt a first measure of comfort. When all the unit tests passed, I had a high level of confidence that the change had been implemented non-disruptively. Without unit tests, I would never have made the change, because of the risk that it would cause problems for me far down the chain.
These benefits are not unique to me. Many Agilists view tests as enabling flexible development. In fact, the edgier subset among the Agilists won't code at all unless they have tests written beforehand. This approach is called Test-Driven Development (TDD), which they swear by, but which does not appeal to me. Regardless, the fundamental recognition that by writing unit tests, you assure yourself of the ability to make major changes easily is indeed a universal recognition.
However, the recognition is not universally embraced. A reader recently wrote to me, grousing about the current vogue that so values unit tests. His view was that the test are a waste of time. I've heard this view before. Unit testing has not been a waste of time for me, because the time it costs to write the tests is recovered from the shortened debugging cycle. And I gain the ability to make changes to the code with confidence. But the classic complaints often point to exactly such large changes. Namely, that if — in the Agile spirit — you throw away a bunch of code because of a shifting requirement, you also have to throw out a bunch of unit tests. Because unit tests typically represent 50%-75% of the size of the code base, you're chucking 1.6x code, say, for every 1x that needs to be changed. This is true, of course, but there's no way out of that bind. I could write untested code that I dreaded to debug and then I could feel better about having to dump it. But that's not exactly a happy place either.
One place where the complaint rings true is psychologically. The more code you've written, the more attached you become to it and the less you're willing to throw it away. And tests only add to this resistance. But the reality is that code is rarely stripped off and dumped en masse. Rather, some pieces are replaced selectively and others are modified. In this process, tests once again help assure that the code works as expected.
Most organizations today use unit tests in varying degrees. In a recent talk, Jeff Atwood of Coding Horror fame said he uses tests selectively — mostly when he's testing package interfaces. Others, as I pointed out, are more committed to their use throughout a project. If you're among the hold-outs who don't use unit tests at all, you're missing an excellent opportunity to make your code better and to improve your coding experience. As I said earlier, of all the practices the Agile revolution has brought to the mainstream, this is probably the one that will save you the most time and aggravation.