I have been programming Ruby on Rails for about 3 months now, and have been following a fairly strict Test Driven Development (TDD) methodology using rspec for the duration of that time. Using TDD has been a great experience, and now that I’ve been doing it for a while I never want to go back to my pre-TDD ways. Here’s why:
I can describe my software in English before I write any code. An rspec test describes in English some particular behavior of a component such as “it returns the empty string if no record is found”. As you can imagine an entire suite of tests at this level of detail pretty well covers all of the behaviors of the component. Describing each behavior in English first frees my brain to focus only on implementation details when I’m writing the code.
I can focus on the details that matter and ignore the ones that don’t. When my brain is in coding mode it wants to account for every logical possibility and my code can easily become overly complex. Or I can overlook important edge conditions and end up with unexpected bugs. A good set of tests clearly defines the expected behavior and tells me when I’ve written just enough code to satisfy the requirements.
It is easier to read test descriptions than it is to read code. The test descriptions help me quickly identify the interesting code without having to follow method calls or conditional logic. The tests themselves are full of examples for how to use the component, so reading tests is an excellent way to quickly learn the details of a third-party API. Spelunking through code that has thorough test coverage can actually be a pleasant experience!
And of course there are fewer bugs. According to my project management tool, on my current project there are on average 3.7 bugs for every 11.7 features. With so few bugs I don’t have to go through a tedious as stressful bug fixing cycle at the end of the release!
In conclusion, TDD is awesome. It makes programming more efficient and enjoyable. If you are a software developer and you have not tried TDD I highly recommend you give it a chance.
One Trackback
[...] Test Driven Development: Why it’s awesome [...]