Phil Nash from level of indirection
The title might read a little like click-bait, and there are certainly some nuances and qualifications here.
But, hey! That's what the article is for.
Those that know me know that I have been a practitioner of, and advocate for, TDD in C++ for many years.
I originally introduced Catch in an attempt to make that easier.
I've given many talks that touch on the subject, as well as giving coaching and consultancy.
For the last year I've added to that with a workshop class that I have given at several conferences -
in both one-day
and two-day forms
(at CppCon you can do either!).
But why am I all in on TDD? Is it really that good?
What has TDD ever done for me?
Most of the time, especially for new code (including new parts being added to legacy code)
the benefits of using TDD include (but are not limited to):
- A decent set of tests with high coverage (100% if you're following a strict approach).
- Well factored, "clean code".
- (an aspect of 2) code that is easy to change.
- A more thoughtful design that is easier to work with.
But attaining these benefits is not automatic.
Applying the discipline of TDD steers you towards the "pit of success" - but you still have to jump in!
Fully achieving all the benefits relies on a certain amount of experience - with TDD itself -
but also with a range of other design principles.
TDD is like a spirit guide, nudging you in the right direction.
It's down to you to listen to what TDD is telling you and take the right actions.
This is the first hurdle that people trying TDD fall down at.
The core steps to TDD are simple and can be taught in about 10-20 minutes.
Getting all the benefits from them takes experience.
If you start trying to apply TDD to your production code straight away
you will almost certainly just see overhead and constraints - with little-to-no benefit!
On your own this may just be a case of doing enough katas and side-projects until you feel confident
to slowly start using it in more critical areas.
If you're able to take a class (whether mine or someone else's),
you can be helped past this initial stage -
and also shown the cloud of additional things around TDD that will let you get the best out of it.
Types, Tests and EOP
First of all, I don't consider TDD to be the complete picture.
There are some cases where it's just not practical
(although these are often not the ones people think - it takes exprience to make that call).
Where it is practical (the majority of cases, in my experience)
it can form the backbone for your code-design approach - but should be complemented by other techniques.
If you think of these things as a toolbox to select from -
then combining the right tools is the only way to be fully effective. Proficiency - if not mastery -
with those tools is also necessary,
The tools that I find, again-and-again, work well with TDD are: using the type system to reduce -
or eliminate - potential errors, and what I call Expression-Oriented Programming,
which is really a distillation of Functional Programming.
These are two big topics in their own right, and I plan to follow up with deeper dives on each.
In the meantime you'll get a better idea of my views on FP from my talk, "Functional Programming for Fun & Profit".
I've yet to do a talk specifically on my views on how the type system can help,
but there are elements in my recent series on Error Handling.
The bottom line, for now, is that the more you can lean on Types and FP,
the less there will be left that needs testing.
TDD or not TDD
That's the question.
I've already hinted that it may not always be the best approach (but usually is).
But even when it is - it is not sufficient.
That is, the tests you are left with through pure TDD are only half the story.
They helped you arrive at the design, gave you a users perspective on your code,
and the confidence to make changes. But they won't find the edges of your design that you didn't explicitly consider.
TDD might help you think of some pathological cases you might have otherwise left out
(and leaning on type system may eliminate many more) -
but you're going to have to consider other forms of testing to find the things you didn't design for.
Then there's the thorny issue of how to get legacy code under test - or should you even try?
There are more tools in the toolbox to help with all of these things.
Even though they are not part of TDD itself,
to practice TDD well you'll need to be able to be able to weave it into this bigger picture.
Short-cutting the Gordian Knot
So while learning TDD, technically, is easy - mastering it is a far richer proposition.
Returning to our opening question - is it worth it?
My experience has been a resounding "yes"!
All the pieces that come into play (and we've not mentioned them all, here) will give you more tools,
greater experience and a better understanding of the bigger picture -
even if you choose not to use TDD in a particular case.
But how can we get up to speed?
You could do what I did and pick up bits here and there, reconcile it with your own years of experience,
observe the results, adjust course and try again - eventually settling on a pattern that works.
Or you could join me on one of my workshops as I try to give you the benefit of that experience in a distilled form.
You won't walk out a seasoned expert, but I think I can help you well down the road.
My next outings are:
Or if you'd like me to come into your company to deliver on-site training you can reach me on