Skip to content

Putting the Three Rules of TDD Into Practice

Robert Greiner
Robert Greiner
2 min read

I've been thinking alot lately about how to practice better at software development. One of the things I want to get really good at is unit testing. I think unit testing is vital to the success of a software project. Good unit tests can reduce your overall debug time and can help give you confidence that your code changes aren't breaking anything. However, as anyone who has written more than four lines of code can attest to, writing unit tests is similar to eating vegetables or working out. You know it's good for you and can pay huge dividends in the future, yet we still don't do it.

So, the obvious question here is: if unit tests are so good, what is stopping us from writing them? Assuming that we can go ahead and agree to the assertion (see what I did there?) that unit tests are indeed helpful and worth writing (which is a controversial statement in itself), I think the main reason developers neglect writing tests is due to a lack of standardization. Developers just don't know where to get started sometimes. When do I write tests? How many tests do I write? When do I run my tests? What if I know a change works, do I write a test for it anyway?

This is where Uncle Bob's article can really help us out. Here, Uncle Bob outlines three very specific rules that can clear up any of the questions you may have been struggling with.

The Three Rules of TDD

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

At first, trying to follow these rules is going to be pretty tough. Very tough actually. I'm still trying to get into the test-first mindset and it's going to be difficult to break away from all those years of doing development a specific way. However, I know that this is an area of development that I am weak in, and anything I can do to improve how well I write unit tests is welcome.

Ready to get started? Uncle Bob suggests checking out The Bowling Game Kata. Check it out and let me know what you think.

Please consider subscribing, it's free.
Technology

Robert Greiner Twitter

Professional optimist. I write a weekly newsletter for humans at the intersection of business, technology, leadership, and career growth.


Related Posts

Members Public

AI Rule #1 - Customer First

In 1985, Warren Buffett wisely said, "The first rule of investment is don't lose. And the second rule of investment is don't forget the first rule, and that's all the rules there are." Similarly, the first rule of AI investment is to

AI Rule #1 - Customer First
Members Public

Navigating the Upside Down as a Technology Leader

Ideas for leading technology organizations with confidence and creativity.

Navigating the Upside Down as a Technology Leader
Members Public

Artist for a Day

The future of creative output is multiplied by AI.

Artist for a Day