“Don’t Repeat Yourself”. Every developer has already heard this principle.
Yet, in my experience, most of us didn’t get it. At least not how it was intended.
I won’t ask you to trust me. Let’s trust the authors. What did they say when they came up with this principle?
In 1999, Andrew Hunt and David Thomas wrote in The Pragmatic Programmer:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
Most software engineering teams translated and understood this as: « duplicate code is bad. »
And that’s true most of the time.
But what’s the subtle difference many people miss?
The Pragmatic Programmers’ section about this is titled “The Evils of Duplication”.
So clearly we’re close.
The authors explain this much better than I’d do, so let’s look at their words, then we’ll look at an example to illustrate one of my pet peeves of bad DRY misapplied in tests.
In the 20th anniversary edition of the book, Andrew Hunt and David Thomas add this:
Let’s get something out of the way up-front. In the first edition of this book, we did a poor job of explaining just what we meant by Don’t Repeat Yourself. Many people took it to refer to code only: they thought that DRY means “don’t copy-and-paste lines of source”.
This is part of DRY, but it’s a tiny and fairly trivial part.
DRY is about the duplication of knowledge, of intent. It’s about expressing the same thing in two different places, possibly in two totally different ways.
Later they have a section about “Not All Code Duplication Is Knowledge Duplication”.
If that sparks some curiosity, go read the book, it details the duplication in data, in documentation, in representation, across APIs and much more.
A very common occurrence I see during code review is the false duplication of some pre-condition knowledge.
Let’s look at a very simple function, used to validate some string:
We want to add basic tests for this function. We even want to go further and get good coverage of the various states the input can take, so we use Hypothesis to do some property-based testing on this:
We submit this for code review, pretty confident, we do cover many positive and negative cases. What could be wrong?
Which DRY violation could be happening here?
Stop reading for a second, read these snippets again until you have a guess.
My main concern with such code is that the duplication we avoid makes our tests brittle and much less valuable.
Our tests rely on the code’s pattern to generate test data. We have a single code representation for 2 pieces of knowledge: what is applied and what is expected.
That means that is the pattern is updated, maybe by mistake, then the behavior of the function will change, yet our tests will continue to pass, accepting the new values.
So what would be a good way to do it here? Obviously, in this case, we want to duplicate the pattern, or rebuild it, but basically, to ensure that any change of the knowledge in the code requires us to update the test to let them pass.
This is a toy example to fit in a post, so it might look obvious here.
But when the code gets bigger, I see similar code constants re-use in the tests very often, and it becomes less obvious for more complex examples.
Does it look familiar?
During your next code-writing and code-review sessions, try to keep this in mind, and spot this misuse of DRY. It’s surprising how common it is, and how easy we overlook it.
The key take-away?
Don’t forget that DRY is about intents and knowledge.
Don’t reject all code duplication without a thought, spend more time thinking about the other kinds of duplication.
We all know it’s better, yet we all fail to do it often enough.