Another .NET Developers Blog


Get on the TDD bandwagon!
March 23, 2008, 11:38 am
Filed under: programming | Tags: ,

Earlier this month me and a few guys at work decided to delve head first into test driven development. We had always done things the old fashioned way and decided it was time to catch up with more robust development techniques.

We knew it would be painful at the beginning, but the rewards of TDD are too good to ignore. And painful it was… At first development was extremely slow! Thinking about what to test was difficult. Thinking about how to test was also hard! But we persevered, moving slowly from one test to the next, and testing each new class and method as we invented them.

I think it was about one week in, that this experiment started to bear real fruit. We started to notice that, in the process of developing the tests, we were designing our program from the outside in. This reminded me of something Jean-Paul Boodhoo has repeated over and over in various screencasts: In writing the tests for our programs we are actually driving out the design of our programs!

The most obvious benefit of the TDD approach is adherance to the YAGNI agile principle. It is often very tempting to write some code which solves a problem that you have anticipated. The problem with this is that you may well NOT need this code! TDD overcomes this anticipation by dictating that you write only the code needed to satisfy your tests. At the start this can feel restrictive, but as you move on this becomes liberating.

The second obvious benefit of TDD is that you tend to write functional, loosely coupled code. By starting from class names, then filling in the gaps with the methods you need, I have found that I am creating more useful classes with less bloat. When you specify a method you are forced to think carefully what parameters you should be passing to it and what object, if any, it should return.

The approach of designing classes from the outside is totally different than designing them from the inside. I’d say you get a birds eye view over your software when using TDD. This is far better than forever being bogged down by tiny details…

All in all I’d say I’m now a huge fan of TDD!! I’m still taking baby steps, but the more I use agile development techniques the more I see the huge benefits!

Recommendations:
Agile Principles, Patterns, and Practices in C# by Robert C. Martin.
dnrTV! – Check out the episodes with JP Boodhoo and Venkat Subramaniam!
DotNetRocks! – A twice weekly .NET podcast.


No Comments Yet so far
Leave a comment



Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>