Friday, March 24, 2006

Why should you write tests first?

(this is a response I posted to the question below regarding test-driven development)

Saulius said: "All this sounds nicely, but what about those situations when you need something done yesterday?"

This is one of those things that is difficult to explain if you haven't done it. I get this question a lot, and it is hard to convince someone to test first.

I think that perhaps the only way to answer the question is to maybe ask one of you.

You do test your code, don't you? Eventually?

With test-driven development you are just writing the tests first instead of last (evn if testing means running the app in the browser). The side-effect of testing first, which has been proven over and over, is that your code becomes cleaner, is easier to maintain, and has less defects. This last side effect is where you save all of your time.

The trick of course is that you need to learn HOW to test your code. This is the time consuming part, but it only needs to be done once. You need to learn how to use the testing tools for the language you work in. Once you have done this, the actual writing of tests takes very little time.

No comments: