TDD Pro-Tip: Before I write the first test in a new context, I usually design until I get to a state of “Nervous Optimism”.
A couple of days ago, I was party to some drinking geekery with my colleagues at a hookah bar restaurant dance club. It was the end of a long day, and we were unwinding and being pretty silly. My three partners in crime mobbed on “Evil Hangman”, a fun little game. And I sat off to the sidelines and took potshots and enjoyed my scotch, all on my new twitch channel. It was really just some decompression, but it did trigger something for me.
See, by the time we got the twitch channel alive, after ten minutes of silly fumbling, they stormed in and one of them, Nayan I think, just wrote a first test. When I say first test, here, I mean first test ever, in a project we created on the spot.
Reader, it bugged the shit outta me. Now, I wasn’t part of the mob, so it’s not like I had anything but snark rights, and in any case, we were basically “drinking with keyboard”, but to me, it felt like a crazy-wild rush into the abyss.
I bring this up because I want to tell you how I come into TDD’ing a brand new thing. (Also by way of apology for the severe dip in liquor supply up in Northern Virginia the last couple of days.)
There are folks out there who seem to suggest that TDD is best when you don’t think about things before you write the first test at all. That is not how I do TDD.
When I start TDDing in some brave new context, I don’t start by typing in the text of a test. I start by doing something we may as well call “design”.
By that word, “design”, I mean to designate basically thinking about where I’m going to go without yet going there.
I am also going to use the word “sketch”, but I want to be sure you understand how I’m using it. There are markings some people make, on some kind of paper, using some kind of stylus, that are called sketches. That is not what I mean, or at least, not necessarily.
When I say “sketch”, I mean to emphasize not the medium of any expression about where I’m thinking of going, but the – well – the sloppy rapid slapdashed not-really-as-good-as-I-could-do-ness of it all. These sketches I’m talking about might actually be markings on a surface with a stylus. I certainly do sometimes draw ugly little pictures, and those would qualify. But they include several other mediums of expression. I choose the one I want based on god-knows-what.
And I do this all a lot. In fact, I only occasionally write a test I don’t have some idea of how I’m going to pass. I do do that, but usually only when I’m working in code I already know and am performing some relatively minor tweak on.
So if you’re okay calling those episodes sketches and calling that process design, then an immediate pair of questions occurs: How do I know when to start doing that? How do I know when to stop doing that and go type something?
I have an answer that seems incredibly rock-solid to me, but may take some folks aback.
I start designing when my stomach hurts.
I stop designing when my stomach doesn’t hurt but is still a little fluttery.
If I’m driving in the car, and I need to go somewhere, and I don’t even know what direction to face, my stomach hurts. I am an anxious boy. If I don’t know whether it’s a 3 miles away or a thousand, my stomach hurts. If I don’t know any nearby landmarks, my stomach hurts.
On the other hand, I drive places I’ve never been before all the time. And when I know it’s roughly west, maybe 3 or 4 miles, right past that gunstore with the big plaster bear on top, my stomach doesn’t hurt. It’s just a little fluttery now and then.
There are other ways to say this, but I like the “driving somewhere new” metaphor. I need clues. I’m not joking when I say that I’m an anxious person. If I don’t have clues, my stomach hurts literally.
I call this feeling “nervous optimism”. And I am fascinated by the fact that it is “just a feeling” and at the same time so integral to my way of working.
I write a first test when I think I can probably pass it, but I don’t know I can definitely pass it. (I write other tests at times when I do “know” “definitely”, but hardly ever first tests.
I can’t tell you when to design and when not to, or how much of it to do. All I can really tell you is what I do. What I do is design until I am nervous but optimistic, and then write a microtest.
Possibly later tonight, or more likely Sunday afternoon, I’m going to roll an Evil Hangman solution on the twitch stream. I will announce it here a half-hour before I start. When I do, I’ll walk you through my designing activities as I go. Join me then, if you like.
And in any case, I hope you have a lovely spring Saturday evening!