When I started recovering from perfectionism, I noticed I’d been trying to carry around an “effectively infinite” set of thoughts to make sure to think. Trying and failing, that is, and wasting cognitive energy in the attempt. When TDD began to poke through my thick skull, I observed the existence of at least one context in which I didn’t have to carry that inventory. Run the tests, know where I am; write a red test, know where I am; make it green, know where I am.

Being able to instantly know where I was obviated my need to think all the thoughts, at least during the act of writing code. I had become test-infected. It was a first step toward a better life.

Test-Driven Development, as opposed to test-first, means we drop the premise that we can be pretty sure where we’re going to end up, and replace it with the premise that we can be pretty sure we’ll figure out how to get there. We keep the part where we write tests before doing things. We drop the part where we write a whole bunch of tests before doing a whole bunch of things. Instead, we write one test, do one thing, and listen for hints about what needs to happen next.

Doing one thing at a time requires, as you might imagine, the ability to delay some gratification. I happen to think a frequent green bar more than makes up for it. Even so, patience can be hard to come by when my brain, without my invitation, has begun loading up the problem and throwing off sparks about what all might be important later. And when it occurs to me that some of those ideas might well be right, and I might not think of them again, it’s hard to let them go. Hence our first hack.

The Test List Trick

The goal of this hack is to:

  1. Quickly record those ideas somewhere I know they won’t get lost, and thereby
  2. Restore my brain to a patience-capable state.

Here’s the spec:

Feature: Test-drive efficiently while being a talented worrier

  Scenario: Initialize focused brain
    Given I'm working on code with tests
    And I'm trying to test-drive
    When my head is full of all the things to make sure to worry about
    Then empty it into a block comment in the tests

  Scenario: Maintain focused brain
    Given I'm test-driving
    When my head invents a new thing to worry about later
    Then append it to the block comment in the tests

  Scenario: Convert good ideas to tests
    Given I'm test-driving
    When the right thing to do next is one of my commented ideas
    Then convert the idea to a test

  Scenario: Discard leftovers
    Given I'm test-driving
    When we're green and there's nothing left to do
    And the block comment still has ideas in it
    Then delete the block comment before committing

The Red-Test Breadcrumb Trick

In case the rationale for the previous hack didn’t make it clear, I’m forgetful. (I had to go back and check that I wrote something that implies forgetfulness.) I’m also a recovering procrastinator, in addition to the perfectionist bit. Really just a lovely person with fine character traits. I confess these things because while context switches are known to be expensive for everyone, I’d be willing to bet they’re extra expensive for me. So I evolved another hack, which is only barely anything to do with TDD, but only works if — and might not even make sense until — test-driving is part of you.

The goal of this hack is to:

  1. Quickly, before I forget, record my next action somewhere I know I’ll find it, and thereby
  2. Restore my brain to its previous context when I return.

The spec:

Feature: Come back efficiently from a context switch

  Scenario: Save mental state
    Given I'm trying to test-drive
    And I can write simple tests very quickly
    When I need to step away for any reason
    Then first write a test that says fail("yo, do X next")

  Scenario: Restore mental state
    Given I'm trying to test-drive
    And I test-drive as a matter of course
    When I'm trying to remember what the hell was going on here
    Then run the tests and be reminded

  Scenario: Get rolling again
    Given I'm trying to test-drive
    When I have a failing test
    Then I'm on solid ground
    And I know exactly what to do next

Got hacks?

I rely on a whole bunch of tricks to be effective. I’m quite sure I’ve got more than two, and I’m quite sure I’m not the first to introduce these two to the world, but as I said, I’m forgetful. (Hey, this time I didn’t have to check!) When I remember some more of my survival tactics, I’ll write another post.

What do you do to arrange yourself to get the right things done right?