I've had some interesting experiences on TDD Island over the last month or so, and I thought it would be a good idea to share what I think so far. Evan Hoff asked me in a comment on my last post:
SOO..one of the big things with Agile is having a feedback loop. What's your feedback on TDD so far? Did you find the green lights slightly addicting?
I've been meaning to put fingers to keys on this for a few days now, if not just to look back on this post in a year and see the road I've travelled...
I Don't Need TDD
That's one thing I think I won't change my mind on. I've been doing quite well over the last few years, thank you, and the "Sudden Ray of Light" won't present itself in TDD I'm afraid.
I also don't need swim laps at the gym and take my stand-up paddle board out at sunset to blow off steam. I don't need to eat organic for that matter, and I certainly don't need to recycle anything and continue using that stinky mulch pile in my back yard.
The use of TDD doesn't resolve down to a need - it's a choice. Some may think it's the ONLY choice (more on that later) but like every other choice we have in life - there are pros and cons to it.
It comes down to what you want out of your geek life.
Pro #1: I Assume Way Too Much
One of the things that didn't make it into Part 3 was my major blunder with the TestCatalogRepository. I want to lie and say "oops, I forgot" but the truth is, I was embarrassed of this one :). Long story short - my routine for creating each product didn't gaurantee a unique ID - I had a nested loop and I mistakenly used the wrong loop variable to set the Product ID.
How did I catch this? Well, as I was sort of grumbling to myself writing what appeared to be a simple test for the products in the repository - making sure that the repository would only return one record if I asked it to - LO AND BEHOLD it failed. I felt very stupid.
I fixed the error, but when I did, 4 other tests failed. In my fix I managed to screw up the way the category was being set for each product. 10 minutes later and with a very sheepish feeling - I fixed everything and was now certain that I had things working in good order - thanks to the tests!
This is what I realized:
Over 90% of the bugs I introduce are stupid.
I say it every time - "damn that was a STUPID bug". The reason it happens? Haste. Assumptions.
The Pro here is actually a double-goody. One failed test found the bug - 4 failed tests told me I screwed up the fix. I like this a lot.
Pro #2: I Love YAGNI
This really should be #1, but the assumption thing... grrr. YAGNI (You Aint Gonna Need It) is wonderful - I am so very guilty of violating this principle! I stub out code that just sits there, unused, waiting like a puppy with a leash in its mouth that I have to feed and clean up its poop. I don't want a puppy.
How many times have you heard this:
"... So where is this method being used?"
I've heard it a lot.
The refactoring process of pruning out code is really quite fun (not sure why it's so fun to scrape out code). It's also fun being sure that everything you've written is needed, and you can see proof of it with a bunch of green icons.
Con #1: I'll Never Do This Right
TDD is a design process - not a coding process. Anything with the words "design" and "process" unavoidably are spawned from a theory, and invariably there are those who the theory works for who have the "Sudden Ray of Light" and think that you should have this too. And as with every theory there are the workers who kick some of the theory to the curb in favor (in their eyes) getting some work done.
I'll summarize my point by saying that TDD is discussed far more than it is applied. There's no problem in that in general - but what it points out is the uncertainty that everyone has as they search for the Right Way and their own Sudden Ray of Light. Although a lot has been written about the process.
In other words theory abounds on this topic, and in these seas There Be Dragons.
Summary
I'm happy to say I'm really digging TDD in general. The results are very positive, my code can leave the door open at night, feeling safe from my assumptions.
There's no greater Con to me then the way the community has divided a bit, with judgements going both ways (no I'm not singling out this week's discussions). It would be nice if this series allowed for positivity (I've already seen it)!
