Benefits of TDD: Problem Solving 18 Jul 2012

Sometimes those of us who advocate Test Driven Development have trouble clearly enumerating the reasons we find it valuable. “Because it gives us quick feedback,” we’ll say, or “because it helps us organize our code better.” We can usually come up with a couple of good reasons, but a compelling or comprehensive description can be elusive. The source of this trouble may lie in the interacting and cumulative nature of the many benefits of TDD. To put this another way, I practice TDD not for one or two specific reasons, but for many subtle positive effects, some of which may come into play to varying degrees in different situations. I think it’s helpful to be aware of the benefits that come from TDD. In particular, this article by Dan North (by its omission of this aspect) made me think about one of the benefits I find from TDD: support for breaking down a problem into smaller parts.

If you think back to your grade school math classes, you may remember trying to solve word problems. As the complexity of the problems increased, you might have broken each problem down into smaller problems, solving each directly, then combined the parts into a complete solution. Fast forward to professional software development, which often involves solving complex and deeply interwoven problems. TDD can provide the structure for just this type of breakdown.

A characteristic often found among the smart and talented folk who occupy the field of programming is an ability to hold many parts of a complex problem in your head, reconciling them through sheer intellect. This is a wonderful characteristic to possess, but programs that depend on it can be difficult to understand and maintain. For those of us with more modest mental faculties, who enjoy simpler expressions of complex problems, or who desire to make code accesible to others without the context built up to develop the code, it can be helpful to break problems down into constituents of the whole.

This is where TDD comes in. By fixing the solution to each sub-problem with a test, you free your mind to focus on other parts of the problem. If you make a change that subtly invalidates one of the conditions you’ve established, the test you’ve written will alert you without your having to run through all the edge cases in your head. Once you have determined the correct conditions for part of your problem, you can rely on the computer to ensure those conditions remain valid and devote your mental effort fully to address other parts of your program or larger issues of composing the solution. In essence, this is applying the core value of computing to the task of applying the core value of computing. While we leverage computing power to reduce work for general problems, shouldn’t we harness the same power for our own tasks?

What’s your favorite benefit of TDD?

comments powered by Disqus