Earlier today I stumbled across a TDD/test-first analogy that's worth sharing.
My dishwasher broke a while ago, and for various reasons, we've been slow in replacing it. This means that my sink often has a few glasses and plates, waiting to hand-washed. Of course, that's gross and makes things look dirty, especially in a kitchen as small as mine. For a while I tried to always wash my dishes immediately after using them, but that proved beyond my discipline. I tried fighting instincts, but after I'm finished preparing and then consuming a meal, I just want to relax -- not spend 15 minutes scrubbing pots and pans. There's not much incentive to wash dishes right then.
But that means that the mess grows and after a few meals, I have to spend an hour or so washing dishes. Washing dishes isn't so awful by itself, but it has a way of permeating the rest of the your activities in the kitchen. For example, I cook a healthy meal at home much less often if there are any dishes waiting to be cleaned. Instead, I'll just have a bowl of canned soup, some crappy microwave dinner, or takeout. Altogether I was pretty unhappy with the situation and looking to expedite the process of getting a dishwasher installed.
So, I developed a new tactic. When I decide to cook a meal, I will force myself to wash an amount of dishes equivalent to the amount I'm about to use, i.e., if I'm eating a bowl of cereal, I'll at least wash a bowl/plate/glass and a utensil beforehand. It's been working fairly well.
In general, with wash-first I'm finding:
- The dishes actually get washed. Putting the incentive after the effort works much better.
- I often wash dishes immediately after using them, because I'm already in that washing mindset.
- As I plan out a meal, I try to minize the number of dishes I'll dirty; I keep washability in mind.
- I end up spending less time cleaning overall.
- I'm cooking more at home.
- My kitchen is cleaner.
So, let's tie this back into software. This past Thursday I went to the Meet and Code dinner in Richmond (now Richmond Software Craftsmanship Group). It was a very fun meeting with lots of good discussion about the challenges of the testing legacy code and just software development in general. With that experience lingering in my mind, I woke up this morning in the mood for pancakes. Oh, but that would dirty so many dishes! I set about washing a pan and so on, and I started ruminating that this wash-first methodology was working fairly well. I suddenly realized that I was applying TDD strategies to dishwashing!
In general, with test-first I've found:
- Tests actually get written.
- My code is designed with testability in mind and thus more testable.
- I end up spending less time writing tests overall.
- I write more tests.
- My code is cleaner.
I'm not sure if this is a cool cross-domain application of development thought processes or just an amazingly geeky idea -- like my friend using Scrum to plan her wedding (ahem). Regardless, I found it as a nice affirmation that the logic behind TDD is founded in basic human psychology.