TDD Guarantees Good Code

Let's examine the misconception that TDD guarantees good code and explore its implications.

Just as there are often overly pessimistic objections to TDD, here is an opposite view:

TDD guarantees good code.

As TDD is a process, and it claims to improve code, it is quite reasonable to assume that using TDD is all we need to guarantee good code. Unfortunately, that is not at all correct. TDD helps developers write good code and it helps as feedback to show us where we have made mistakes in design and logic. It cannot guarantee good code, however.

Understanding problem-inflated expectations

The issue here is a misunderstanding. TDD is not a set of techniques that directly affect your design decisions. It is a set of techniques that help us specify what we expect a piece of code to do, when, under what conditions, and given a particular design.

Freedom in design

It leaves us free to choose that design, what we expect it to do, and how we are going to implement that code.

TDD has no suggestions regarding choosing a long variable name over a short one. It does not tell us whether we should choose an interface or an abstract class. Should we choose to split a feature over two classes or five? TDD has no advice there. Should we eliminate duplicated code? Invert a dependency? Connect to a database? Only we can decide. TDD offers no advice. It is not intelligent. It cannot replace us and our expertise. It is a simple process, enabling us to validate our assumptions and ideas.

Get hands-on with 1200+ tech skills courses.