Thursday, July 15, 2004
Make that bar green!

At work, we have done quite a bit of unit testing so far.  We have been using nunit religiously, and have it integrated into everything we do.  My manager is a java guy, so he is a very firm believer in unit tests.  I was very fortunate in these aspects.

So far though, we have done a lot of test later code, in other words write the unit tests after the code is written.  We all know that we should be writing the tests first, but it's not the easiest concept to grasp.  The arguments are all over on why TDD should be this way, but still, in practice, it's tough to start doing.  In the end, also, I would maintain that the unit tests have the same value if written before or after the code.  Especially when tied into a build system.

This week, though, I had my first test first development, and I have to say, I love it.  One of our developers was heading off for a couple days, and we had been designing something together.  Before he left, we decided the best way to make sure we both agreed with the design, and more importantly each other, was to write the tests first.  Our thoughts were if we agreed on the interfaces, and they matched the logical design, we were golden. 

This of course was a task on it's own, and brought up some very interesting bits that we had overlooked.  So after a few more conversations at the white board, we had stubs for all of our tests and components, a few half written unit tests, and a whole lot of NotImplementedExceptions.  So off he went, and on I went to code.

I first polished off the unit tests to make sure they were all 100%.  This obviously took some time, but since all the interfaces were there it was a pretty easy task.  Didn't even have to turn to the use cases.  So I run my tests, all red.  Fantastic.  Its about the end of the day, so I take an inventory, and check everything in.

Come in today, and start implementing methods.  One by one I see green.  After about 5, it was like I got hit in the head with a boulder.  Holy shit.  I don't think I have ever had such an easy time coding.  Just by writing the tests first, and approaching code one green line at a time, my whole mindset changed.  In the past, half of coding was just deciding what needs to be done next.  Now nunit tells me.  I also see real progress with every green bar. 

Another side effect is the "do the simplest thing possible" statement.  There's no thinking about what it could do, how it could be implemented, should there be another object over here, should there be a kitchen sink in this object over here, none of that.  Just green.  Beautiful green.  Do the smallest amount of work to make green.  I'll be dreaming in green bars tonight.

Well, just after these past few days, I'm sold.  Maybe it's all in my head, god knows the voices are loud, but it seems striving for the green changes how I think about my code. 

Of course one word of warning to anyone thinking about this, your green bar is only as good as your tests.  At this point I have mostly green, and in the end I am estimating I will have spent about 2-3 hours more on the tests then the code.  So, crappy tests, crappy code. 

Thats it for now.  Time to turn the rest green.  One by one.  Just like I like it, small, manageable units of work.  Thanks nunit.