DRY Principle Of Software Development. Common Mistake

DRY Principle Of Software Development. Common Mistake

DRY is a basic principle in any software development. Certainly, it is the most understandable software principle, but not everything is so obvious. I want to show you when you don’t need to follow this principle ????.

DRY is an acronym, of “Don’t Repeat Yourself”. The main problem that DRY can solve is a reducing repetition of code. Sometimes you need to have already existed function in another module, class, etc. And the easiest way to do that is just copy&paste this function. Congratulations, DRY principle violated ☹️. Factual copping of code creates for you one huge problem:

It is hard to maintain all these functions on the whole project, and if you need to change this function — you should change it in all places where did you copy it.

Fortunately, it is easy to resolve this problem. You just need to create some shared class, global module, plugin, parent class, and this code will be shareable now.

Okay, so duplicated code is pure evil and it is easy to resolve this issue. So what’s the point to write this post?

After a few years in software development, I noticed that I resolve automatically any duplications of code. But sometimes I feel that is something wrong. The answer was in Clean Architecture (Robert C. Martin book). This book is a classic for any software developer. I saw much negative feedback about this book, but right now I am talking about the part with which I agreed on one hundred percent. I am talking about Chapter 16: Independence, more precisely about Duplication topic in this chapter (warning — you can’t find it in Contents section of the book).

Architects often fall into a trap — a trap that hinges on their fear of duplication. Duplication is generally a bad thing in software.

But there are different kinds of duplication. There is true duplication, in which every change to one instance necessitates the same change to every duplicate of that instance. Then there is false or accidental duplication. If two apparently duplicated sections of code evolve along different paths — if they change at different rates, and for different reasons — then they are not true duplicates.

Sometimes you will have identical codes in a few places. But you need to understand what is that duplication type. Maybe, for some reason, these codes are identical right now, but if you will have various reasons to change it in the future — it is better to leave it separately and give it a possibility to be a false duplication of code. Yes, these codes are identical but don’t relate to each other. I tried to remove avoid duplications at all costs in any project. I created a parent class and some shared stuff only for one reason — follow the DRY principle.

Now you have another vision on this simple principle and I hope you will start thinking about duplication type before fanatical avoidance of this problem. Duplication has a place in software. You need to think like:

“Yes, these codes are identical right now. Does the change of one duplicated part can affect some other duplicated part? If It isn’t — you can duplicate the code”.