Test-Driven Development: What Is It And Why You Should Be Using It as a Developer to Reduce Your Bugs

Tomasz Dobrowolski
2 min readApr 9, 2022
Photo by Mars on Unsplash

Test-driven development is seen as a popular way to write code nowadays.

But what is it, and why should you be using it as a developer?

Test-Driven Development involves writing tests before you write your code

It’s that simple. The theory behind it is by writing tests that fail at first you do the following:

Plan out your code in a structured format

Writing tests first is hard.

It’s not natural, as we are testing for scenarios that are not in our application first. But by doing this, we are forced to think about our code.

Not planning your code can lead to rewrite after rewrite.

Write code that is loosely coupled

It is difficult to write a unit test for code with dependencies.

Unit tests do not account for dependencies well. We cannot mock a dependency that’s inside a function, considering a unit test can only test the output.

Therefore, if we have an object that is instantiated inside a function, we cannot mock this in our unit test.

Using TDD forces us to use dependency injection. A technique used to pass objects as a parameter of another object. If you want to learn more about Dependency Injection here is an article about the topic.

Write code that sticks to your plan

By writing your tests first, you use a concept known as ‘Red light, green light’.

This involves writing tests that fail first and produce a red light in your terminal. As you write code, that meets the desired outcome of your test, you will eventually get a green light.

That way there are no surprises, as you are coding to meet the aim of your tests.

Tests in your code reduce bugs and increase visibility

When you need to develop something quickly, often tests are the first thing to get sacrificed.

Stakeholders and customers do not see tests, so they are easy to miss off when deadlines are tight. But with TDD, tests are the priority so you cannot avoid them.

The practice of writing tests will stop bugs from sneaking into production. And bugs in your code cause unplanned work, which is the worst type of work. Unplanned work causes delays to deadlines, as you cannot account for it.

Tests are the easiest way to reduce bugs in your code, and test driven development is a great way to ensure you do not slack on tests.

Tomasz Dobrowolski

I break down Software Engineering and Tech concepts | Backend Engineer 🐘