Tuesday, September 18, 2007

The TDD, Test-Driven-Development

I've been reading about and trying out the tdd. It's of course real great. Especially if you are as errorprone as I am :) . The idea is that you write the tests first and then the code. You first specify a function you want, make sure that the test fails for the right reasons and then implement it good enough to satisfy the test. It's real easy, and powerful.

First I like to say something about testing in general. Some people seems to think it's awkward and go: First I write the code and then I write the same code to verify it's right? Then what's the point? I myself sort of thought something like this and, well, there is no point. Or it wouldn't be, if that was the way you wrote tests. The thing is that you (as you may recall from that algorithms course) can't algorithmically prove, in a general case, that a program works. The thing you do is that you take certain cases. Usally two extremes (empty & infinity) and one perfectly normal testcase and implement it and see that it passes. I sort of did that before anyways but now I do it in a more organized way. A more hands on tutorial on this can be found in the nice python book: Dive Into Python.

I tried out tdd and found to my surprise that it actually increased the speed of my development. One of the reasons for this is that I didn't even had to fire up my browser to test things. Then typos and others are discovered right there on the spot so you save time there. And then you can always lean on your test when your brain seems to have gone on vacation.

The unittests is great but sort of old and don't really fit with OO. I mean, it goes nice and you could really do it well, but the thing about software development, as far as I know, is that it's all about how powerful your abstraction is. Therefore I recommend the behavior driven pattern. I actually had nothing to do with the decision to start using it but now I got the hang of it, I love it. A real great article about this can be found here. (Most things go for unittesting as well...). If I was to give a short intro to behavior driven, I would say that it's all about describing objects and what you should be able to do with them.

No comments: