Why I love TDD

There are quite a few reasons to try out Test Driven Design aka TDD.  Personally I find that:

  • It usually leads to a cleaner design.  This is because your problem is broken up into smaller pieces.  I find it much easier to deal with the nice small concrete steps that I find naturally occur when doing TDD.
  • It results in good unit testing coverage of all new code.  I personally find that when I write tests after the code is written I have a much harder time writing good tests.  It's definitely do-able but it ends up taking a lot more effort.
  • I write code quicker.  Okay now there is definitely going to be some disagreement on this.  If you're writing the test then obviously you're writing more code but this misses a fairly important point.  As long as you have half decent typing skills (if you don't I recommend that as the first skill to work on) then the actual writing of code is the easiest, fastest thing I do in my daily job.  The thought process that goes behind that code and allows me to try and improve my skills and processes everyday takes much more time.  By doing things in a TDD manner I find that my thoughts are less likely to wander into the unimportant details that surround every problem.  I can focus on each detail one at a time.

But for all those reasons there is one even more important reason to me.  I'd like to think I'm not the only developer who can look back at a time where they watched a demo of the software they were working on with their fingers crossed.  That gut wrenching feeling when an unexpected error pops up in front of a client.  Or just the shame that comes with missing something that should have been obvious.  And this is where my most important reason comes into play.

I've found over the last few years that when I properly TDD something those moments are greatly reduced.  In fact a lot of times they don't even come up.  When I am writing code in a TDD manner I naturally find points where I feel a tingling of the spiddy senses.  When this happens I know that something is wrong.. either my design is going in the wrong direction or maybe I'm not testing a situation that needs to be tested.

I will admit that there have been times in the past while where due to schedule constraints I allowed myself to fall off the TDD wagon.  And each and every time I've done that I've ended up regretting it.  It always ends up taking more time to write code that has more defects then it should and I always feel bad about it after the fact.

So if you've ever sat there with your fingers crossed and you haven't given TDD a try.  I say try it.. preferably with the mentoring of someone who has already done it but even if your only source of information is books and what you can dig up online it's still worth it.  You may have a rocky start (I sure did) but in the end it'll be well worth the time and effort.