Thoughts on TDD

Person in a lab coat using a syringe to transfer a liquid

I admit it; I used to hate testing. It never made any sense. Why would I want to write more code (test code, that is) when I already have code to write?

With time (and many years of force by the industry), I have learned to understand its importance and place in a software project. However, I have also learned to understand its abuse.

Let's take a look at a practice that is commonly abused, TDD.

TDD: the "poster child" of software development

What is TDD?

Another method that developers made to help you code slower and miss deadlines more often. TDD stands for Test-Driven Doom, oh wait, I mean Test-Driven Development, and it's all about telling you how your code is wrong and how you can stop whatever you are doing to make sure you are writing code correctly.

First off, you may think that the first objective is to write code, but really, your first objective is to write test code for code you haven't written yet. That is the proper way to write code.

Then after you write that code, make sure that the code doesn't pass. If it passes on first run, you are definitely doing it wrong. In fact, if it compiles on the first run you are one confused individual. You have to make sure it doesn't pass. Then you write the code for the test code you wrote earlier, then keep writing and correcting the code until it passes โ€” then, and only then, have you completed TDD.

Of course, I'm being hyperbolic here, but you see my point.

The problem with TDD

So as you may have noticed, TDD is not my cup of tea. It has a few shortcomings:

But still, I use TDD...

It's ironic, I know. But sometimes it works. Just not every time.

TDD purists would swear up and down that they always use TDD and follow the laws of programming to the T.

But let's be honest: the importance of practices like TDD are nowhere close to that of other practices and philosophies like KISS (Keep It Simple Stupid), SOLID, or even YAGNI (You Ain't Gonna Need It).

...but most times I don't

TDD is bad when it gets in the way of tinkering.

There are times when I'm writing experimental code. Writing test code for experimental code is not wise as the code may be deleted later.

I consider most of the code I write as experimental; I don't know the final structure I would choose when I start writing code. Most times I just plan on paper and just jump into code right after: I don't see why I should do otherwise.

I'm not saying that you should not plan when writing code. I'm just saying that it is okay to not use TDD. Sometimes, I even write the tests after I finished writing the code.

I guess the main problem with TDD for me is that I have to write test code for code that I'm not sure is going to stay in the project. That's way more code than I'm willing to write.

What is the alternative?

Simple. Write the code first, then write the test code. A foolproof solution that has been working for years for me. That way when I do write test code, it's more likely to stay in the codebase.

(I know, TDD purists would argue I deserve death by lethal injection for making such a claim but they're have to catch me first! ๐Ÿ™ƒ)

The bottom line

I use TDD when:

I don't when:

In conclusion, don't let anyone tell you how to write code. Hone your craft; choose the practices that work for you.

If you would like to reply to or comment on this blog post, feel free to email me at efe@mmhq.me.