The DRY Principle

2 min readFeb 13, 2022


Photo by Ivars Krutainis on Unsplash

DRY is a very important principle that means don’t repeat yourself.

As the name suggests, this principle tells us not to repeat code snippets.

Many times in a project, we end up copying snippets of code and putting them somewhere else, thus duplicating it.

The code can work fine in both places, but I’m sure you (even if you’re a beginner) can already see that there is a problem here.

Over time, we modify the code as new features are added to the system, and we have to modify the places where there is repeated code, and perhaps in one or more parts the modification
is different from the others, but even so, the snippet remains similar to other parts of the code with a few differences and, because of code repetition, we have to fix a bug in several parts of the code, and we can even forget some parts, especially if we don’t know all the code, in fact, this is a big issue, programmers who are new in the project probably not knowing about these repeated sections, which will make maintenance even more difficult, generating more delays.

When we have repetitive snippets of code, we must extract it to a function, class, or module so that it can be shared by other functions, classes, and modules in the project. There are many refactoring techniques that cover this issue, one of them is the Extract Method which consists of taking the parts of the code (possibly repetitive) and extracting the variables to parameters of a function and the non-variable part will be inside the function, thus this snippet can be reused in other parts, as it is now a function that can be used in other parts of the code.

Another problem with repetition is that it generates the need for memory management and runtime cycles to process identical code blocks, that is, it impacts application performance.

It is clear that DRY is extremely important, however, its excessive use can cause issues.

When used in excess, it increases coupling (the dependence between classes) and makes the code difficult to read and understand.

So it’s not enough to just apply one principle all the time; other principles must be taken into account and their relative values ​​must be weighed according to the situation.

Use it wisely.




Graduated in Computer Science and passionate about programming languages and free software. Here I find a way to share my knowledge while learning even more.