Yesterday I wrote a function that takes a variable argument, like
printf(). That function decomposes the list arguments, and drops
the whole mess onto a function pointer. The pointer points to a
function on either the console message sink object or a kernel-side
memory buffer message sink object. (This is just basic inheritance,
but it’s all gooky because I’m writing it in C.)
Anyway, in the past I would expect a problem of that complexity to
stall me for an indefinite amount of time while I tried to debug
all the bizarre and fascinating things that can go wrong with a
setup like that.
It took me less than an hour to write the test and the code using
test-first.
My test was pretty simple, but coming up with it was probably the
hardest part of the whole process. I finally decided that if my
function returned the correct number of characters written (same as
printf), that I would infer that the function was working.
With the test in place, I had an incredible amount of focus. I knew
what I had to make the code do, and there was no need to wander
around aimlessly in the code trying to support every possible case.
No, it was just “get this test to run”. When I had the test
running, I was surprised to realize that I was indeed finished.
There wasn’t anything extra to add; I was actually done!
I usually cut 350-400 lines of production-grade code on a good day.
Yesterday I didn’t feel like I had a particularly good day, but I
cut 184 test LOC and 529 production LOC, pLOC that I
know works, because the tests tell me so, pLOC
that includes one of the top-10 trickiest things I’ve ever done in
C (that went from “no idea” to “fully functional” in under 60
minutes).
Wow. I’m sold.
Test infection. Give it a warm, damp place to start, and it’ll do
the rest….
David Brady