Thursday, December 02, 2004

Test Driven Development(TDD) or Test First Development(TFD)

Suresh and I have always been wondering about what is the most practical way of development: TDD or TFD ? We both agree that TDD is the best way theoretically. But in practice how would one go about writing tests for all possible cases, including the negative ones and then write code. The amount of tests written would be phenomenal. Then we took an evolutionary step and agreed that maybe all possible tests need not be written, but only those steps which are relevant to the class under consideration and which we feel are important need to be concentrated on. This leads to the problem of missing out crucial tests which were not apparent to the developers at that time. Therein lies the crux of the matter. How practical should one go before one turns out wrong and how theoretical should one go before one turns unacceptably inefficient.

We did pair programming on a not-so-complicated problem and started out with TDD. Over time with the deadline drawing near we deviated from strict TDD and ended up doing TFD. But at one point, because we had not written the test for a particular side-effect of a method our code passed all tests but failed to give the correct response. That was the time I was truly convinced about the truth behind TDD (Suresh was wise enough to be a believer from the beginning). But I still don't know how practical will TDD be in real life.

Now through Suresh I have heard of a discussion he had with a colleague of his where they concluded that beyond a point when the complexity of the code prevents one from writing proper tests, code should be refactored. This is basically TDD. I totally agree with this thought. This means that we end up molding the code for passing tests rather than developing code and writing tests to check the code. Since this is the basis of TDD (at least as I understand it), I guess what we had an discussion about was for projects which had not undergone several iterations of development. Once the process of development matures TDD is the way to go.

I have become a total convert. Long live TDD !!


At 10:29 PM, Blogger suresh said...

To add to the above post, i read somewhere about number of tests discussion. The author says that the number of tests that you write depends on your comfort level. On any sitting, initially you start with lots of tests. And as time progresses, you get comfortable and skip a few tests that you think dont matter. But anytime you see a red bar, you backup and start writing more tests.


Post a Comment

<< Home