Search Sreeni's Insights

Filter by content type
crafting February 23, 2016 7 min read

Test Driven Development (TDD) - is it worth it for our team

Test-Driven Development evaluated through its design benefits, upfront costs, defect feedback, suitable code areas and a gradual adoption approach for teams.

Share:

Test Driven Development (TDD) - is it worth it for our team?

Every team wants to deliver features faster and with less bugs, but these two goals are often pulling in opposite directions. When the deadline is near, the first thing we cut is the tests. Few weeks later, the bugs reported by QA and by our customers are eating the time we “saved”. Test Driven Development (TDD) promises to break this cycle, but it also asks the developers to write more code upfront, and that is where the debate starts.

In the previous article, we discussed test doubles, the difference between stub, mock and fake. Last year, we also had an article on how to do TDD step by step with the Red-Green-Refactor cycle. In this article, we are not going to repeat the “how”. We are going to discuss the honest question we asked ourselves, is TDD worth it for our team? We will see the costs, the benefits, the situations where it pays off and where it doesn’t, and how we decided to adopt it.

TL;DR Summary

  • TDD is a design practice first and a testing practice second.
  • It costs more time upfront, published industrial studies report roughly 15-35% more initial development time.
  • The same studies report a big reduction in pre-release defects, which is where the time is returned.
  • The cost of a bug grows with every stage it travels, TDD catches most of them at the cheapest stage.
  • It pays off most for business logic and long living code, less for UI, prototypes and spikes.
  • Don’t mandate TDD for everything on day one, start with the domain logic and measure.

Challenges

”We don’t have time for TDD”

It is the most common objection. When a developer writes test first, the first version of a feature takes longer. The manager sees the slower velocity in the first sprints and the experiment is cancelled before the benefits are visible.

Learning curve

TDD is a skill. Writing a small failing test, writing just enough code and refactoring needs practice. In the first few weeks, developers write too big tests, too many mocks, or skip the refactor step. It makes TDD look harder than it is.

Legacy code

Most of our code was written without tests. The classes are tightly coupled, dependencies are created with new and static helpers are everywhere. Writing a test first for a change in this code is very hard, so developers fall back to “change and pray”.

Wrong expectations

Some people expect TDD to remove all bugs. It doesn’t. It catches logic errors in the units we test, it doesn’t catch wrong requirements, integration issues or performance problems.

What is TDD?

Test Driven Development (TDD) is a software development practice in which a developer writes a failing automated test before writing the production code, writes the minimum code to make it pass, and then refactors, in very short cycles.

In simple terms, we first describe what the code should do, then we make it do that, then we clean it up. Every line of production code has a test which asked for it.

The real cost of a bug

Before we talk about the cost of TDD, let us look at the cost of not finding a bug early. A bug found by the developer while writing the code takes minutes to fix. The same bug found by QA takes hours, because it needs a bug report, a context switch and a new build. When it is found in UAT or production, it takes days and it is having a direct impact on our customers and our SLA.

The following diagram depicts how the cost of a bug grows with every stage it travels.

Cost of a bug by stage
Cost of a bug by stage

The flow highlighted in purple is where TDD finds the bugs, at the cheapest stage. It is the main argument for TDD, we pay a little more at the start to avoid paying a lot more at the end.

Cost vs Benefit

Let us discuss both sides honestly.

TDDTests after (or no tests)
Initial development time− Higher+ Lower
Defects found by QA and customers+ Fewer− More
Design+ Small, decoupled, injectable classes− Often tightly coupled, hard to test later
Refactoring+ Safe, tests are already there− Risky, “don’t touch it” code
Documentation+ Tests describe the behaviour− Behaviour lives in developers’ heads
Learning curve− Weeks of practice+ None

There is some published evidence too. A well known study by Microsoft Research and IBM (Nagappan et al., 2008) on four industrial teams reported a pre-release defect density reduction of about 40-90% for the TDD teams, with about 15-35% increase in initial development time, which the teams’ managers considered as a good trade-off. Of course, every team is different, so we should not take these numbers as a promise, but they show the direction.

Where TDD pays off?

In our experience, TDD gives the best return in the following areas,

  • Business rules and calculations - fund pricing, fee calculations, validations, eligibility rules. These have clear inputs and outputs and the bugs here are expensive.
  • Long living code - code which will be changed many times over the years. The safety net pays back on every change.
  • Bug fixes - write a failing test which reproduces the bug, then fix it. The bug never comes back silently.
  • Shared libraries and NuGet packages - many consumers depend on the behaviour.

And it gives less return in these areas,

  • UI layout and styling.
  • Prototypes and spikes which will be thrown away.
  • Thin glue code which only maps one object to another, with no logic.
  • Code which is mostly calling external systems, here integration tests are more valuable.

Why TDD is important for us?

In order to reduce the bugs which reach QA and our customers, to keep the design of our domain code clean and to refactor the legacy modules safely, we decided TDD is worth trying. The main reason was not the test coverage. It was the design feedback. When a test is hard to write first, it tells us the class is doing too much or it is hiding its dependencies, and we get this feedback while the code is still cheap to change.

How we adopted it?

We didn’t mandate TDD for everything on day one. Here are the steps we followed,

  1. Ran few coding dojos with katas (String Calculator, Bowling Game) to practice the Red-Green-Refactor rhythm without project pressure.
  2. Picked one module with heavy business logic as a pilot, the fund fee calculation.
  3. Paired experienced and new developers for the first sprints.
  4. Agreed a simple team rule, every bug fix starts with a failing test.
  5. Measured the defects found by QA and the cycle time for the pilot module before and after.

Our results from the pilot,

  • Defects found by QA in the pilot module: <before value> → <after value> per release [confirm: numbers from the team's tracker]
  • Sprint velocity in the first two sprints: <value> lower [confirm]
  • After <N> sprints, velocity was back to normal [confirm]

Inference:

  • The first sprints were slower, as expected, and it is important to tell this to the stakeholders upfront.
  • The biggest benefit came from the bug fix rule, it was easy to adopt and the regressions stopped.
  • Some developers still prefer writing tests right after the code, and that is fine for us as long as the tests follow FIRST and are written in the same task.

Note: TDD is not a religion. The goal is well designed, well tested code. If a developer achieves it by writing the test five minutes after the code, the outcome is almost the same. What we don’t accept is “tests later” which means “tests never”.

Must Do and Don’ts

  • Must Do: Start with business logic, not UI or database code.
  • Must Do: Write a failing test for every bug fix before fixing it.
  • Should: Practice with katas before using TDD on production code.
  • Should: Measure defects and cycle time before and after, otherwise the decision is based on opinions.
  • Don’t: judge TDD by the first two sprints.
  • Don’t: skip the refactor step, otherwise TDD produces working but messy code.

Wrap up

In this article, we have looked at the challenges of adopting TDD, the real cost of a bug, the cost and benefit trade-off, where TDD pays off and how we adopted it in our team. In our case, the answer was yes, but for the right parts of the code and with a gradual adoption. Thanks for reading this article and I hope this article provided you some insights to make the same decision for your team. In the next article, I will walk you through defensive coding, guard clauses and null checks without the noise.

Related Posts