Saturday, October 14, 2006

Test-driven development -- valuable practice or just a waste of time?

I'm sure lots of us have all heard about how we should be writing unit tests for our code, but haven't quite got round to witting them because the features of our project were a more immediate issue?

Well very recently I started a new project, Boogaloo (see previous entry). Although it was a diversion from my main project, I decided to go all out this time and write unit tests, or more specifically, write the tests before I wrote any of the features. This practice is called test-driven development, i.e your unit tests drive the evolution of your application.

Before I come to the punch, I must first tell you that recently I have been battling with what I identified as one of my major programming weaknesses -- complexity. In the past, I'd start out with trying to envision how I wanted a project to be in n months and begin writting code to accommodate all the different eventualities. I'd even add complexity at class and method level in an attempt to make the application as flexible and diverse as possible. A university friend commented the other day that "Ian's code doesn't look like anyone else's" -- he was talking about the needless complexity.

So, I've been consciously trying to avoid complexity where I can, but it doesn't always work and it's easy to want to slip in an extra little feature hear and there. Test-driven development has changed that though. As great as unit-tests are, I don't particularly enjoy writing them -- and that's actually a good thing in this case! Because I write the tests first, I don't bother with adding tests for any extra little features, just the essential functionality. Once the functionality is in place and the test starts passing, I don't want to add any extra complexity because that means I'll have to write new tests and probably re-factor the existing one, problem solved!

The end result is a simple application that does exactly what you wanted it to, plus you know for sure it actually works, and will continue to work as you make changes. This is a plus for the users too as they don't have to wade through your cruft to get at what they want.

At some point the project may approach the stage of a little more than 'simple', but at least it's still accessible, and you've only added the complexity where the users wanted it.

I'm definitely won over by test-driven development, it suits my development style and forces me to walk to begin with so that I can run later on.

I'm still discovering the wonders of testing, so now doubt I'll be blogging more about it very soon.

"Simple software is a lot harder to write than complex software" -- me

Boogaloo Simple Cache Server

I've just made the first release of a new project named Boogaloo.

It's a simple cache server that provides persistent and temporary caching. You can also use it to host your own custom services.

Give it a look! http://boogaloo.rubyforge.org/