I love writing code using test driven development. But this will not be a post about how to test drive your code. There are plenty of very good articles available and, of course, the Kent Beck book. I will reference the obligatory Wikipedia definition, though.

Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards.

This will not be a post about how TDD improves code quality and design. These topics have been well covered. This post is much more personal.

Let’s walk through a typical TDD session: I write a test. I watch the test fail. I implement code. I watch the test pass.

At this point, I’m engaged and excited about moving on to the next test. I know that I’m on the right path and it feels good. I repeat the cycle in short intervals, each one leaving me increasingly more satisfied with what I’ve accomplished. Ballooning test counts give me a sense of how far I’m progressing. I often get into a state of flow and can lose myself in the code.

These micro goals and feedback loops have a fascinating way of reinforcing our behavior. Video game designers have been exploiting the power of positive reinforcement for years. Gamers keep playing in order to get that next win or next reward. With TDD, each passing test is a “win” and I want to keep going. I’m doubly motivated if I implement some code and the test is still failing. This is Albert Bandura’s self-efficacy – “the measure of one’s own ability to complete tasks and reach goals” – in action.

Research on game theory helps us understand why this works:

The flow we experience when playing a great game is a prime example of how we can condition other parts of our lives. Because of this, flow has become central to game theory. Good games that are responsive to player ability and game difficulty are framed as microcosms of optimal experience. They give the player a sense that their skills and abilities are adequate for coping with the challenges presented, and are based around “a goal- directed, rule-bound action system that provides clear clues as to how well one is performing”

TDD truly is about so much more than testing. The feedback loop inherent in TDD feeds us with positive reinforcement that our minds crave. The quality and design improvements that TDD lead to are nice, as well 🙂