I Don’t Have Time for Unit Tests
I’ve helped several organisations adopt Test Driven Development (TDD). The initial worry that almost everyone has, is that it will hurt productivity. It seems intuitively correct, because of course, the developer has to write the unit tests as well as the production code. However when you actually look at how developers spend their time, actually writing code is a small part of it. Check out this study by Peter Hallam from the Visual Studio team at Microsoft:
http://blogs.msdn.com/b/peterhal/archive/2006/01/04/509302.aspx
According to Peter, developers actually spend their days like this (while not reading Code Rant of course :):
- Writing new code 5%
- Modifying existing code 25%
- Understanding Code 70%
If some technique allowed you to half the modifying/understanding parts of the job by doubling the new-code part, you’d now be taking only ~60% of the time you previously took to deliver a feature, almost doubling your productivity.
TDD is that technique. In my experience the productivity gains come from:
- Allows safe modifications. If you break something when you modify some code, the unit tests fail.
- Shortening the iteration cycle between writing/running code. No more need to step through your application to part where your new code gets exercised.
- Mistakes in your code are shallow and obvious. No more need to step through code in the debugger, wondering which part of your application is broken.
- Code is self-documenting. The unit tests explicitly show how the author expected the code to be used.
- Code is decoupled. You can’t do TDD without decoupling. This alone makes the code easier to understand as a unit and much safer to modify.
Note that I’m just talking about feature-delivery productivity here. I haven’t mentioned the huge gains in stability and drop in bug-counts that you also get.
Now I don’t deny that TDD is a fundamentally different way of working that takes time to learn. Undoubtedly productivity may drop while a developer is getting up to speed. But in my experience the argument that a developer doing TDD is slower than a developer who doesn’t do it is simply not true.
0 comments