Cover image by Mark Duffel on Unsplash.
Rules are made to be broken, right? TDD is a strict set of rules that define a process for building software. But experienced practitioners have moved beyond the rules and work towards something else entirely.
Ron Jeffries wrote a tweet thread in which he discussed the notion of what he calls pure TDD (PTDD), and is sometimes called strict TDD. He talked about how he doesnât like to subscribe to this purest approach.
But rule-breaking is an acceptable part of any skillful process.
The Dreyfus model of skill acquisition talks about skilled experts being intuitively aware of rules and can make decisions without conscious thought. That means they know not only how to apply rules but also when to ignore rules.
Then thereâs the four stages of competence model which goes from unconscious incompetenceâyou donât know what you donât knowâthrough conscious incompetence and conscious competence and finally to unconscious competence which, again, is saying that experts are intuitively aware of what theyâre doing without having to think about it.
What does it mean to be intuitive?
In some sense, the word intuitive means you no longer need rules. Instead you understand the core values behind the rules, and work towards those values, not the rules.
Because you know the values behind the rules, you can understand the trade-offs involved. You can begin to see when the rules might not apply. You can begin to answer the question âwhen should I use TDD, and when should I not?â
Contrast this with TDD aficionados. Many of them will want to use TDD all the time, because TDD is just so amazingly great when you get the hang of it. (Iâm not being sarcastic here; it really is great.) We have a phrase for this attitude: âIf all you have is a hammer, everything looks like a nail.â
When is it okay to NOT use TDD? When can I cheat?
Assuming then that you agree with me that itâs okay to move beyond the rules, then on what occasions should we do so?
Here are some I can think of.
- When you work with a team who arenât versed in / donât follow TDD. Now your choice is to accept that fact and try to win them over, which probably wonât happen very quickly.
- When the risk is low. This is an interesting one, because the more experienced you get, all those functions and objects start to look the same. That means development becomes less risky because itâs familiar. Then again, your risk of mistake goes up if youâre tired, or working alone. There are many considerations. Knowing how to weigh them up can take considerable experience.
- When the cost/benefit ratio is not in your favor. An example is React components which are often complicated to test. But you can make a design decision to move all business logic out of components and into Redux reducers, and by doing that the need to test the components at all becomes much less, since their size relative to the codebase has reduced. You can test the Redux functions instead. Theyâre just plain functions, so they should be easier.
- When youâre experimenting. Many applications donât need quality right away, but do need a period of discovery where features can be fluid. In this scenario TDD can slow you done, not speed you up.
But the rules are still VERY important.
A common belief in education is that experts make bad teachers. This follows naturally from the Dreyfus model if you think that being intuitive means losing sight of the rules.
This doesnât mean that all experts are bad teachers. Actually they can make great teachers. They just have to put in extra effort to be great, otherwise they might very well suck.
If youâre involved with working with novices, which is typical for the industry today with mixed skill-level teams, then you must be able to switch back into âpure TDDâ mode.
When MUST I use TDD?
So we can now start to see occasions when TDD is ultra-important and should not be skipped. Here are the two that come to my mind:
- When youâre teaching TDD novices. In order to help them learn, you have to show them the rules. Not your cheats!
- When youâre pairing with someone youâve never worked with. Even if theyâre an expert, they will have a different set of cheats and trade-offs from you. So you should fall back to strict TDD. Whenever a situation occurs where either of you are considering cheating, stop and discuss it. If you canât reach agreement, then use TDD.