thorns and gold

The Thorns And Gold Of TDD

I want to carry on developing our ShoppingCart class using Test-Driven Development (TDD). In my last post on TDD, we forced the creation of an Add() method. Add() is required so we can put items into ShoppingCart.

However, before we go further, it is time to introduce the concept of ‘The Thorns And Gold’.

With TDD, before we can reach for The Gold, we need to cut down The Thorns.

What are The Gold and The Thorns?

‘The Gold’ is the Happy Path scenario, where everything works as expected. For example, with our ShoppingCart’s Add() method, the happy path is being able to successfully add 3 Apples to the cart. Or 7 Ferraris, or 1 iPhone.

In TDD done right, we shouldn’t be writing tests for The Gold first. 

Why? 

Because we’ll get snagged by The Thorns. We need to cut The Thorns down first before we can go for The Gold.

What are The Thorns?

The Thorns are problem scenarios – things we know can reasonably go wrong – we get invalid data; an object is null. For our current ShoppingCart.Add() method, thorns that we should write tests for so that we can manage them appropriately are

  • the Product is missing/null
  • the Quantity is zero
  • the Quantity is negative

If we cut down thorns, our systems become stable and deterministic. I believe this makes intuitive sense: Thinking of what could go wrong and then writing code to manage these problem scenarios creates expected behaviour. It’s a case of ‘Don’t worry – we thought of it and it’s under control.’ 

On the other hand, willfully turning a blind eye to possible errors and hoping they don’t occur is prone to leave a system in an unknown, unstable, indeterministic state. Right here we have the cause of so many NullReferenceExceptions, supposedly the ‘Billion Dollar Bug

Not cutting down thorns is a case of crossing our fingers and hoping for the best – ‘She’ll be right’. Unfortunately, she won’t be. When we leave an abundance of thorns in place sooner or later, we are going to get snagged.

There is more to Thorns than exceptions. We’ll look into that next time.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply