Don’t Repeat Yourself (DRY) can be over applied. If you see x = 10
and y = 10
together it doesn’t mean you need to replace y
with x
. Here x
and y
mean different things even if they happen to hold the same value. Eliminating y
destroys information and harms flexibility.
It’s the same with lines of code. Just because two functions happen to have the same lines of code within doesn’t mean one must be eliminated. Those functions do the same thing but depending on their names and what they are obligated to support when called they can still be very different functions. Ones that may soon change independently.
You must protect that need to change independently when it exists. It can be just as real as the need to change together when you’re copying around a bug.
But none of that is an excuse to blindly copy and paste duplicate code everywhere. Never just focus on getting done what needs to be done. Always keep an eye on how well you’re showing why you’re doing what you’re doing.
Unexplained duplicate code causes a serious problem because it’s been stripped of it’s meaning. Without meaning people fall back on dogma to make decisions and DRY gets over applied. If you dupe, make why you dupe clear.