In the last year or two, I’ve been pushing myself to limit the length of my TDD cycles. In particular, I’ve been trying to spend no more than five minutes writing a test, making it pass, or refactoring from one passing state to another. I went in skeptical, despite having heard this advice in one form or another from several people I respect, but I’ve been astonished by how much it’s helped me improve my work.
By limiting the time to make a test pass, I’m forced to work in fine-grained steps. Rather than testing a big feature, I think about the smallest thing I know I need next. This helps me build only what I actually need, and avoid scope creep.
If I understand the problem, it’s easy to break up, but if I don’t, I’ll have a hard time finding a small step to work on. This means I have more immediate feedback on whether I’m getting lost. If I don’t have a solution after five minutes, I need to back up and find something smaller to figure out.
Limited red time during refactoring helps me work with greater fluency. If my code is broken for three hours while I refactor, I can’t see how the design works in practice until all that work is done. But if I do the refactoring gradually in tiny steps, I’m constantly in a position to change directions as the design evolves. I can also stop at any time and still be able to leave the code better than I found it.
Finally, the time limitations force me to focus on one thing at a time. With just a few minutes to make a test pass, I can’t waste time tidying up my code or improving my algorithm. Once I have working code, I can step back and improve it – again, one step at a time.
If an example would be helpful, here’s a code kata I did last year, where I followed three rules: (1) Commit on every new failing test, passing test, or passing step of refactoring; (2) Try to have something to commit every five minutes; (3) If you don’t have something to commit, revert any work you have out.
If you’d like more background on what made me try this out in the first place, a good place to start would be Joshua Kerievsky’s presentation “The Limited Red Society”. If you try this yourself and find it useful, please do let me know!