Thursday, August 9, 2018

How Can I Test this Code?

Early in a test-driven development transition, a lot of people want me to make TDD work with their code.

"What you showed me was fine...great, even...but how can I make this work with my existing code?" they ask.

When we go look at their code, most of the time the answer is "You can't and that's a good thing".

There's almost always an implied "...without changing any of my production code..." in the question. Asking "How can I write tests for this code?" is taking an important option - perhaps the most important option - off the table: changing the code.

Paying down the technical debt embodied in bad design yields dividends in so many ways that I can't explore them all in one blog entry. Maybe I can, but I won't.

Suffice to say, fixing your design is so valuable that you can safely treat enabling TDD as a bonus. On the other hand, test-driven development is so valuable, you can safely treat forcing you to fix your design as a bonus.

You choose which benefit justifies remediation as there is plenty of value to be had.

The first step on the road to redeeming a code base is admitting it has a problem. The next step is to start solving little pieces of that problem, one step at a time.