DEV Community

Cover image for Why bother with TDD?
Ephrathah Oyedoh
Ephrathah Oyedoh

Posted on • Updated on

Why bother with TDD?

Disclaimer :

I am still very much learning about best
practices and clean code so all views shared are personal to me and where I currently am in my coding journey.

Testing. Ever since I started embarking on my coding journey, testing has been nothing to me other than a nemesis that I avoided at all costs. Delegating the task of testing was always easy up until now because you don't have to look too far in a project for green tick addicts that equate testing to pure joy and happiness.

What is TDD?

TDD stands for 'Test Drive Development'. TDD is a software development process that involves writing failing tests before you actually write any fully developed software and continuously testing the software against all tests cases. TDD is a popular alternative to writing software and then testing later.

Why implement TDD in your work ?

I would say that TDD is the code equivalent to cleaning as you go in the kitchen compared to cleaning the kitchen after a very messy Christmas dinner. TDD is more effective in forming the habit of caring about your code and the effects it leaves behind. It doesn't really matter if your code works when you don't know if it will break or even worse you have no clue when your code will break.

I'm an apprentice at 8th Light and currently entering my second month at the company. I was tasked with building a tic tac toe (noughts & crosses) CLI game in ruby. The aim of the project wasn't to get the game working but to challenge me to learn another language while learning how to implement TDD.

I started building my app while using both Ruby and Rspec for the first time. I had experiences with classes but it definitely wasn't a strong point for me. This challenged me to learn a more object oriented approach to programming while thinking about how to implement unit and integration tests.

What are unit and integration tests ?

Unit testing is the process of testing a unit. In programming that unit is most often a function or the smallest amount of code you can isolate in a codebase. Integration testing pretty much does what is says on the label. Integration tests help test the flow of an application forming tests for where units of software are connected together. Therefore you are testing how your software integrates and how it combines different functionalities.

Advantages of TDD ?

  • You only write code that's needed.
  • Modular design.
  • High Test Coverage.
  • Easier to maintain code.
  • Easier to refactor.
  • Easier Debugging.

Disadvantages of TDD ?

  • Slow process.
    • Writing test before your software takes time, even straight forward implementations will take a little longer.
  • It takes a team.
    • TDD becomes pointless if only one person on a team implements it. TDD influences the planning of code so a team approach is necessary to plan and implement code in that manner. There is no point cleaning the kitchen when five other people are making a mess and never cleaning.
  • Tests need t be maintained when code is changed.
    • Similar to the slow process of writing tests it also then takes more time to maintain said tests.

So is TDD for you ? If it's not that's understandable because it definitely wasn't for me and right now with a failing test in front of me I'm reconsidering this conclusion. I joke.

Overall my experience with TDD has been good the only downside being the effort it requires, I personally think it pays off in the end. Currently my greatest benefit from TDD has been Thinking through why I actually want or need to write a specific piece of code. TDD has helped me cut down on repeated and redundant code.

Resources :

Discussion (2)

reubengt profile image
Reuben George Thomas

I think my biggest takeaway from this is "tdd takes a team". A lot of testing tutorials online are based on the simplest examples, scaling this to testing a whole app is something I have struggled with doing alone. Probably much easier to learn testing in a team that does a lot of it

ephieo profile image
Ephrathah Oyedoh Author

Thanks Reuben !! Yes I've noticed in my short time focusing on TDD how tedious it is if just one person adheres to the rules.