TDD Guarantees Good Code

Address 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’s quite reasonable to assume that using TDD is all we need to guarantee good code. Unfortunately, that’s 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’s 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’re 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’s 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 1400+ tech skills courses.