DRY - Don't Repeat Yourself
A house can only have one main circuit breaker.
In The Five Whys I mentioned the The Pragmatic Programmer book, by David Thomas and Andrew Hunt. This present note also explores their work; more specifically, the the Don’t Repeat Yourself (DRY) concept.
They write that “DRY is about the duplication of knowledge, of intent. It’s about expressing the same thing in two different places, possibly in two different ways”. If you’ve read my previous The Five Whys note, you might have realised that one of the issues was a business rule living on multiple different services across the organisation.
That is why the authors say that we should not repeat ourselves, and that “the alternative is to have the same thing expressed in two or more places. If you change one, you have to remember to change the others”. In our situation, it was like having two main house circuit breakers: Which one should be turned off? Both?
This is not to say, though, that code duplication is wrong by itself. Let us consider the following:
function validateAge(value: any) { ensureInteger(value); ensureMinimum(value, 0);}
function validateQuantity(value: any) { ensureInteger(value); ensureMinimum(value, 0);}Both functions run the exact same code, but they have completely different business meanings when validateAge runs on user registration and validateQuantity runs on e-commerce check-out.