Other reasons for TDD: Baby steps, ROI, Evil Manager Syndrome and Watching the watchmen
Tags: testing, tdd, unittesting, productivity
Published: 2019-02-27 01:29:05

---
published: true
title: Other reasons for TDD: Baby steps, ROI, Evil Manager Syndrome and Watching the watchmen
tags: testing,tdd,unittesting,productivity
description: Describing some reasons to do Test Driven Development that are rarely mentioned.
cover_image: https://cl.ly/c6fef6eb3a2d/download/Image%202019-02-27%20at%203.44.41%20PM.png
canonical_url: https://danlebrero.com/2019/02/18/other-reasons-for-test-driven-development-baby-steps-roi-evil-manager-watching-the-watchmen/
series: Efficient Development
---

Perhaps the world does not need another “X reasons for TDD”, but I find most people miss these four very important reasons for following a test-first approach:

1. Baby steps
1. ROI
1. Evil Manager Syndrome
1. Watching the watchmen

## Baby steps 

The TDD (Test-driven Development) workflow encourages you to make progress in small baby steps. With this workflow, you feel like a small meerkat that every few steps stands up to check its environment.

Maybe not the fastest way to reach your destination but, without a doubt, the safest. But as we do not run the risk of being eaten, what does it mean for us to be safer? 

After each small change, we check that no bug was introduced. And if a bug is introduced, it is obvious when and where the bug was introduced: in the last tiny step. 

Finding the bug becomes trivial. And bugs are the second worst time wasters in software development:

1. First, you spend time writing it.
1. Then somebody has to review the bug.
1. Then you have to release it to production.
1. Your client has to report it.
1. You triage the impact.
1. Your manager has to prioritize it.
1. You have to fill up some JIRA issue.
1. You explain it to your QA.
1. You have to debug the code, code that by now is probably completely alien to you as probably it has been days or weeks since you wrote it.
1. Then you fix it.
1. Somebody reviews the fix.
1. And at last, you release the fix to production.

So, paradoxically, doing baby steps and being safer can also mean going faster, depending on how many bugs you write.
 
This principle is in fact exactly the same as the [“Small batches” principle](https://continuousdelivery.com/principles/#work-in-small-batches) in Continuous Delivery.

*Moral:* if you don't write bugs, do not waste your time writing tests.

### How small should the baby steps be? 

As small as possible, but its lower limit it going to depend on how fast your test suite is:

![Changes between runs](https://danlebrero.com/images/blog/why-tdd/changes-between-runs.jpg "Changes between runs")

Even if you try to be very disciplined about running your tests after every small change, if your test suite is slow, you will end up making your change batch size bigger, just to avoid the pain of waiting. 

Hence, if you want to influence how often the tests are run, you need to make sure that they run very fast.

## Return on Investment (ROI)

A test suite is an investment and, as with any investment, you want to have a high positive return on investment (ROI). ROI can be calculated as:

*ROI = (Gain from Investment - Cost of Investment) / Cost of Investment*

The costs of a test suite are pretty obvious: time to build and maintain it. There is also the ongoing cost of running it (another reason why faster test suites are better).

There are several benefits to a test suite, but we can sum them all up as time saved while making changes.

So the ROI of a test suite can be calculated as:

*ROI = (time saved - time spent) / time spent*

If we plot the ROI (in red) with a test-last approach when building a new feature, we can imagine something like:

![ROI test last](https://danlebrero.com/images/blog/why-tdd/roi-test-last.jpg "ROI test last")

If you never ever touch that part of the codebase again, the tests are the worst of your investments, as it will not start to pay off until you start making changes again in that particular area of the codebase. 

Let’s compare it with a test-first approach:

![ROI test first](https://danlebrero.com/images/blog/why-tdd/roi-test-first.jpg "ROI test first")

Tests start to pay off immediately, as you are actively changing the codebase covered by those tests. 

*Moral:* **if** you write a test, write it first. Note the highlighted **if** in the previous statement.

## Evil Manager Syndrome

"I’m not given time to write tests" is probably one of our favorites excuses when we are questioned about why our code coverage is less than ideal. It is not your fault, it is your evil manager’s fault:

&nbsp;&nbsp;&nbsp;Manager: “We have to deliver yesterday!”<br/>
&nbsp;&nbsp;&nbsp;Team: “But we haven’t finished yet!!”<br/>
&nbsp;&nbsp;&nbsp;Manager: “Let cut some corners!!!”<br/>
&nbsp;&nbsp;&nbsp;Team: “Let's skip writing the tests until we have time!!!!”<br/>
&nbsp;&nbsp;&nbsp;Manager: “Awesome! You are a fantastic team!!!!!”<br/>

And this is how typically the spiral to the crying rewrite starts:

![Spiral to the crying rewrite](https://danlebrero.com/images/blog/why-tdd/spiral-to-crying-rewrite.jpg "Spiral to the crying rewrite")

1. As you have little tests, you cannot refactor.
1. As you cannot refactor, your codebase starts to accumulate garbage.
1. The more garbage in your codebase, the longer features take to be implemented.
1. The longer features take to be implemented, the more time pressure there is.
1. The more time pressure, the fewer tests.

And this completes the downward spiral that always ends up with us, developers, crying for a complete rewrite.

Let’s see the same situation but when doing test-first:

&nbsp;&nbsp;&nbsp;Manager: “We have to deliver yesterday!”<br/>
&nbsp;&nbsp;&nbsp;Team: “But we haven’t finished yet!!”<br/>
&nbsp;&nbsp;&nbsp;Manager: “Let cut some corners!!!”<br/>
&nbsp;&nbsp;&nbsp;Team: “What features can we cut?!?!?!”<br/>
&nbsp;&nbsp;&nbsp;Manager: “Cutting features??? Do not write any tests!!!!!”<br/>
&nbsp;&nbsp;&nbsp;Team: “We already wrote them!!!!!!”<br/>
&nbsp;&nbsp;&nbsp;Manager: “Bastards!!!!!!!”<br/>

And then follows a long conversation about what features to trim, which your manager hates. Welcome to agile.

## Watching the watchman

Seeing a test fail is the test that tests if the test tests what is supposed to test.

Or in simpler words, how do you know that your test doesn't have any bugs? Who watches your watchmen?

If you write a test first and you see it fail, there is a strong indication that there is some piece of production code, some logic, that is not there.

If you write the test and you never see it fail, you don’t know if it is because you’ve ready implemented a feature, or because you forgot on an assert in your test or the setup code is not correct.

![watchmen](https://danlebrero.com/images/blog/why-tdd/watchmen.jpg "Watching your tests")

Moral: Writing your tests first keeps the watchmen honest.

## Test-first all the things?

But no matter how many reasons you have, when you present this test-first approach, most of the time you get the ominous answer: “How can I write the tests first if I don’t know what I am going to build?”

And as any experienced TDD developer will tell you, that is BS ... but not always. We will see next time when is and when is not TDD the best approach.