Kevlin Henney asked once: Why do cars have brakes?
Imagine if cars didn’t have brakes, how fast would you go? You’ll be driving really slow to not hit anything. But by giving the capability to slow down, brakes actually give us the ability to go faster.
TDD is like a good car
People new to unit testing and TDD complain that their development speed goes down.
Speed of development, of course, isn’t the point. A solution that solves the right problem made out of code that enables future changes is the point. We can deliver code that doesn’t work very quickly, or that solves half the problem, or both. We’d rather do the right thing.
With TDD we need to think in slices. Every test we pass is a slice. And after each slice, there’s a break.
These breaks allow for two things that help us. The first is enabling us to think about the current test. Focusing on a small problem helps us define and model it (our test) and solve it (the simplest code that works).
Refactoring is very helpful in reflection about the capabilities of the code, now with the added code. We might think of cases we didn’t originally thought of, and add them to our task list. We might refactor to confirm (or deny) the design that we had in our heads. Reviewing the tests (as part of the refactoring process) tells us how much of the solution we have, or if we can stop.
The key thing here is the process gives us space to think. Almost requires us to think. And when we do, it’s about small problems. Things that are our small minds are able to process.
TDD acts as a flow regulator. It allows us to solve the problems in small chunks, by lowering the speed (of typing) and giving us time to think. Since we’re solving small problems quickly, the overall development speed actually goes up.
And at every break, we know the code works. Which has a much better feeling to it, than just releasing without testing.
TDD isn’t just Red-Green-Refactor. It’s a holistic process. It fits to how we humans think and work. It results in high speed, and high quality.
Just what we want from a good car.