DRY Principle Of Software Development. Common Mistake

Photo by Kimberly Farmer on Unsplash

Original Post

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”.


Robert C. Martin book




iOS Software Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Responsive Design: The Dark Mode

Top 50 things you should know before migrating from Oracle to Mysql

Data Structures in Python- Trees

Stock Price Simulation with Monte Carlo Simulation in C++

Tcl Programming Language

Deleting duplicates from a Linked List in Python

Contextual Documentation for Design Tokens

Infographic describing the relationship between “centralized documentation” and “contextual documentation”

How to Manage Azure Resource Groups with Tags, Logic Apps, Automation Account and Runbooks Part — I

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Almost Engineer

Almost Engineer

iOS Software Developer

More from Medium

Clean Code to Make You Look Like a Pro

Talk is Cheap, Show Me Your Code — Part II

005: When Best Practices Break Down

Three “Clean Code” tips that will make your dev team 50% more efficient