Thursday, January 20, 2005

Test Driven Development !!!

Test Driven development .. ya ya .. ThoughtWorks does Things the agile way and TDD is one of the hallowed practices that we try and follow. I have heard tons of stuff about TDD .. but only recenty did i have an AhA moment with TDD ..

I was working with this rather difficult piece of code which had lotsa calculations in it and I just was not able to get it right .. I went thru the cycle .. try to fix .. build .. start up application and see if my fix worked .. but for some strange reason it kept failing ..

I thought of adding a test for this, but being the lazy ass that i am .. i decided to just try it the non-test way once more .. and the "once more" kept getting pushed.

Finally my friend Suresh (fed up of seeing me waste my time like this) told me to write a test for this. So I did that .. and trust me in 15 mins I had solved the problem !!! .. 15 mins ..

Going the Test First way I noticed many things

- the cycle of fixing the issue is much faster, as a test is well just a test. U dont have to compile and build the whole app and start it and go thru the GUI to get to the issue. Using the test U get right to the core of the issue. So it makes a huge difference.
- this way I have a test in place. So even if Im damn lazy .. i have tests surrounding my code.
- no way some one can break my code as the tests will prevent that. For example last week I was fixing a bug in some obscure part of the code. The fix was simple ( which kind of triggered alarms ) and I checked in, and promptly broke the build. I had broken a ton of unit tests. When I looked at the tests I figured out that this 'bug' was not a 'bug'. It was a piece of intended funtionality. So there !!
- Cleaner code and better design of my code. This is one thing that I have consistently seen when I use TDD.
- I can express my intent to a certain extent thru the tests.
- I always like the part when i run tests on the code and see the green bar, Gives me a sence of accomplishment and confidence.
- and doing things the TDD way is real fun. No doubt its a lil more difficult. But that only the first test that takes so much time. After that its just adding test .. adding functionality .. adding test .. adding functionality. some where along this process .. u dont like the look of the code .. go on a refactoring spree .. change every god damn thing .. and just make sure u get a green bar at the end of it and we're safe.
Beautiful !!!!! :-)


Post a Comment

<< Home