Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is violation of DRY principle always bad? [closed]

I have been discussing about DRY (Don't Repeat Yourself) principle also known as DIE (Duplication Is Evil) and there are votes, that any simple code repetition is always an evil. I would like to hear your opinion about the following points:

  1. Uncertain future. Let's say, that we have the same code in two places. The key is, that these two places have only incidental connotation. There is a possibility, that they will vary in the future because their context and semantics are different. Making an abstraction from these places is not cheap and if one of these places change, unwrapping from abstraction will be even more expensive.
  2. Readability. There is a complex computation that involve several variables or steps. In other place of code there is another one, that have some parts identical. The problem is, that if we take out the common parts, the readability of calculation will decrease and created abstraction will be very hard to give it a descriptive name. Worse, if some part of algorithm will change in the future like in point 1.

Does the above cases are good reason to give up abstraction process and just leave duplicated code in favor of risk of future changes or just readability?

like image 302
Ryszard Dżegan Avatar asked Jul 22 '13 13:07

Ryszard Dżegan


People also ask

Can you please explain the DRY principle?

DRY, which stands for 'don't repeat yourself,' is a principle of software development that aims at reducing the repetition of patterns and code duplication in favor of abstractions and avoiding redundancy.

Why is the DRY principle so important?

DRY stands for Don't Repeat Yourself and the principle is that there should only ever be one copy of any important piece of information. The reason for this principle is that one copy is much easier to maintain than multiple copies; if the information needs to be changed, there is only one place to change it.

When developing the DRY principle don't mean yourself?

"Don't repeat yourself" (DRY) is a principle of software development aimed at reducing repetition of software patterns, replacing it with abstractions or using data normalization to avoid redundancy.

What is DRY principle explain with an example?

DRY BenefitsLess code is good: It saves time and effort, is easy to maintain, and also reduces the chances of bugs. One good example of the DRY principle is the helper class in enterprise libraries, in which every piece of code is unique in the libraries and helper classes.


2 Answers

Those are entirely valid reasons to violate DRY. I should add a third: performance. It's rarely a big deal, but it can make a difference, and abstraction can risk slowing things down.

Actually, I'll add a fourth: wasting time and potentially introducing new bugs by changing two (or more) parts of a codebase that might already be working just fine. Is it worth the cost of figuring out how to abstract these things if you don't have to and it probably won't save any or much time in the future?

Typically, duplicated code is not ideal, but there are certainly compelling reasons to allow it, probably including further reasons than what the OP and myself have suggested.

like image 160
pattivacek Avatar answered Oct 11 '22 10:10

pattivacek


Let's try to understand why DRY is important, and then we can understand where breaking the rule is reasonable:

DRY should be used to avoid the situation where two pieces of code are conceptually doing some of the same work, so whenever you change the code in one place you have to change the code in the other place. If the same logic is in two separate places, then you have to always remember to change the logic in both places, which can be quite error prone. This can apply at any scale. It can be an entire application that is being duplicated or it can be a single constant value. There also may not be any repeated code at all, it may just be a repeated principle. You have to ask "If I were to make a change in one place, would I necessarily need to make an equivalent change somewhere else?". If the answer is "yes", then the code is violating DRY.

Imagine that you have a line like this in your program:

cost = price + price*0.10 // account for sales tax 

and somewhere else in your program, you have a similar line:

x = base_price*1.1; // account for sales tax 

If the sales tax changes, you are going to need to change both of those lines. There is almost no repeated code here, but the fact that if you make a change in one place it requires a change in another place is what makes the code not DRY. What's more, it may be very difficult to realize that you have to make the change in two places. Maybe your unit tests will catch it, but maybe not, so getting rid of the duplication is important. Maybe you would factor the value of the sales tax into a separate constant that can be used in multiple places:

cost = price + price*sales_tax; x = base_price*(1.0+sales_tax); 

or maybe create a function to abstract it even more:

cost = costWithTax(price); x = costWithTax(base_price); 

Either way, it is very likely to be worth the trouble.

Alternatively, you may have code that looks very similar but isn't violating DRY:

x = base_price * 1.1; // add 10% markup for premium service 

If you were to change the way sales tax is calculated, you wouldn't want to change that line of code, so it isn't actually repeating any logic.

There are also cases where having to make the same change in multiple places is okay. For example, maybe you have code like this:

a0 = f(0); a1 = f(1); 

This code isn't DRY in a few ways. For example, if you were to change the name of function f, you would have to change two places. You could perhaps make the code more DRY by creating a small loop and turning a into an array. However, this particular duplication isn't a big deal. First, the two changes are very close together, so accidentally changing one without changing the other is unlikely. Second, if you are in a compiled language, then the compiler will most likely catch the problem anyway. If you are not in a compiled language, then hopefully your unit tests will catch it.

There are many good reasons to make your code DRY, but there are many good reasons not to also.

like image 28
Vaughn Cato Avatar answered Oct 11 '22 08:10

Vaughn Cato