Why Does That Exist?

I was puzzled. Reading raw POST requests across a network socket returned headers, but never body content. When I mentioned it to Colin and showed him the method I suspected was misbehaving, he asked me a question: “Why does that exist? Which test brought that line of code into being?”

In a perfect TDD world, this question always has a good answer. In my case, it didn’t: this was a complicated line that was twice removed from the test that “created” it. But the path forward was clear right away: write a test for this line, in isolation, the way I should have done from the start.

Good test-driven development requires a lot of restraint and self-discipline, and lines like my faulty byte reader are guaranteed to sneak in if I break one of the laws of TDD—even if it’s only a couple lines of support code that aren’t written just to pass a test, or a test that looks comprehensive really trying to cover too much at once. This question is a great way to hunt them down and fix them: ask every line of code you write to justify its existence.

Comments