DEV Community

Discussion on: What was your TDD aha moment?

Collapse
 
n_develop profile image
Lars Richter

It's hard to tell when exactly my "aha moment" happened. The only thing I can really say is, that it took a long time.

I've read "Clean Code", "The Clean Coder", "Test-Driven Development By Example", "Growing Object-Oriented Software Guided by Tests", "The Art of Unit Testing" and much more. I knew how to write a test. No problem here. I knew "Red-Green-Refactor". It's simple, isn't it?

Let me tell you a secret: TDD is not simple. I hear people over and over saying "TDD is the one and only way to develop maintainable software. Everyone should do it NOW."
And don't get me wrong: I think TDD is great. I love those days when I have the discipline to TDD all my work.
But again: TDD is not simple. Or... maybe I should rephrase. "TDD is not simple to use in your everyday work." Because TDD is not difficult at its core. "Red-Green-Refactor" is not hard to learn. In fact, it makes a lot of sense and it's even fun while you learn it.
I've done the FizzBuzz kata at least a dozen times. Then the bowling game kata, the roman numbers kata, and a lot more.
But the truth is: It's pretty unlikely that you are getting paid to create FizzBuzz. Nor are you paid to create a simple command-line calculator (at least in most cases).
We are paid to tackle complex problems in complex systems

My point is: It's easy to start using TDD if you are doing just another FizzBuzz kata. But where do you start when you are developing a web app? I'm doing ASP.NET Core most of the time. So let's pretend we are using C# and ASP.NET (MVC). Do you start with a controller? If so, how do you test the results? A controller action returns a view in most cases. Do I test the view? If so, HOW do I test it? Should I assert for all the HTML elements? Or do I just test the model? What part of it?

Today I'm comfortable TDDing my work. I know where to start. I know where to stop. But it took time. A long time.

Long story short, you sum it up well in your original question.

It's also a reasonably difficult habit to form.

Amen to that.