Why Test-Driven Development (TDD) is important?

We all are onboarded with the fact that “The more we test a software the more quality emerged”. Proper testing is one of the key attributes of high-quality software.

According to the book: Code Complete by Steve McConnell we get 1-25 bugs out of 1000 lines of code. Check the statistics below:

  • On average, a developer creates 1-25 bugs per 1000 lines of code (!)
  • 7-15 bugs per 1,000 lines of code find their way to the customers
  • Fixing a bug takes 30 times longer than writing a line of code
  • 75% of a developer’s time is spent on debugging (1500 hours a year!)

Cost of production bug

Fixing a production bug may cost 100x more than fixing a bug at design time.
Fixing a production bug may cost 15x more than fixing a bug at implementation time.

cost of production bug

Facts of Production Bug

  • Most production bugs caused by simple programming mistakes
  • 58% of them are trivial and can be addressed with test coverage
  • Tiny mistakes will have a huge impact on production

How to reduce production bug?

We all are aligned that production bugs are too costly most of the time. To reduce the production bug Test Driven Development (TDD) is one of options. Check the bar chart below about the relative cost of with and without TDD:
total cost with and without TDD

What is TDD?

TDD is a technique of developing software where the developer starts with writing test cases. Then run the test cases, obviously, the tests are going to fail. Write some code to pass those test cases. Run the tests again. Test cases are passed and now refactor the code to make it well designed, cohesive and reusable. When things look good repeat the same process. Check the diagram below:

tdd life cycle

Why TDD is important?

Writing tests from the very beginning has some advantages:

TTD Results in Fewer Bugs?

A case study of engineering teams at Microsoft and IBM showed that the defect density decreased between 40% and 90% relative to similar projects that did not use the TDD practice.

TDD Produces Better Quality?

A study carried out among developers with 10+ years of professional experience (on average), to investigate their perceptions when employing TTD. Many of them agreed that “TDD allows greater quality and maintainability”

TDD Promotes Simpler Design?

The Department of Computer Science at North Carolina State University, ran an experiment with two groups of programmers: one used TDD and the other the linear approach.

It turned out that “92% of developers believed that TDD yields higher quality code, 79% thought that TDD promotes simpler design.

When TDD is not a good choice?

All the processes or techniques have their own limitations.

Developing GUI With TDD

Trying to test the exact placement of UI components is pointless. First, the layout is subjective and should be “tested” by humans.

TDD experience in team

With an enthusiastic team and at least one experienced developer who knows how to write good tests and also knows a few things about good architecture, otherwise, think twice before going down the TDD road.

TDD slows down initial process

If you’re pressed for time and need to quickly launch your product or solution, the TDD approach may not be your best choice.

Test Code Requires Maintenance

All lines of code require maintenance, which means cost. The cost is easiest to acknowledge when a change to existing functionality is made.

TDD approach is difficult

In TDD we need mocks/stubs etc. When we start using mocks, after a while, we will want to start using a Dependency Injection (DI) and a Inversion of Control (IoC) container. To do that you need to use interfaces for everything. At the end we end up writing a lot more code.

Reference
  • https://medium.com/javascript-scene/the-outrageous-cost-of-skipping-tdd-code-reviews-57887064c412
  • https://theqalead.com/general/statistics-studies-benefits-test-driven-development/
  • https://coralogix.com/blog/this-is-what-your-developers-are-doing-75-of-the-time-and-this-is-the-cost-you-pay/
  • https://www.jrebel.com/blog/when-to-use-test-driven-development
  • https://stackoverflow.com/questions/64333/disadvantages-of-test-driven-development