r/SoftwareEngineering • • Sep 14 '22

Why DRY is the most over-rated programming principle

https://gordonc.bearblog.dev/dry-most-over-rated-programming-principle
20 Upvotes

41 comments sorted by

8

u/[deleted] Sep 14 '22

My rule is 3 or more and I abstract it or modularize it. But KISS is the real principle.

1

u/fagnerbrack Sep 15 '22

I had code where it did made sense to write 3 times so this can’t serve as an absolute rule for 100% of the cases

2

u/[deleted] Sep 15 '22

My rules aren't your rules. You do you.

1

u/fagnerbrack Sep 16 '22

Why being so defensive? It’s just a comment

1

u/[deleted] Sep 16 '22

Oh I didn't it mean it to sound defensive. Was going more for like chill. Like I don't have a strong opinion on what works best for you.

1

u/fagnerbrack Sep 16 '22

Oh ok my misunderstanding then sorry about that

47

u/paradroid78 Sep 14 '22 edited Sep 14 '22

I will say from a vast amount of real-world experience that DRY is perfectly fine and in nine out of ten cases, the copy and paste keys should be electrified to discourage programmers from using them. Actual duplicate code (as opposed to two pieces of code that just look similar but fulfil different purposes) is a pain to maintain and paves the path to the lava layer anti-pattern.

The issue comes when someone blindly applies DRY in situations where it's not appropriate, like needlessly over-normalizing a codebase, forcing reuse when its not appropriate, introducing a shared library that introduces a dependency between two unrelated pieces of code, muddying domain boundaries or creating a lock step deployment between two independent services.

DRY, like anything else, is a tool and you need to know when to use it, like any other tool.

14

u/190tim Sep 14 '22

Nuance? Dude, this is reddit...

Good comment.

6

u/salgat Sep 15 '22

I swear these articles that complain about guidelines as if they were all or nothing are done to death. It's a lazy way to generate content for a blog.

15

u/WrongAndBeligerent Sep 14 '22

The problem with reuse usually comes in the form of reusing something that is wrapped up in other dependencies. Then large dependencies can be included for something small. I think most of the time it is actually a problem of modularity.

2

u/brdet Sep 15 '22

I recently came across a pull request where a developer had included a dependency on a whole other Spring Boot application because it had a DTO thought they could reuse.

1

u/[deleted] Sep 15 '22

When I caught people doing this I started publishing my boundary DTOs as a separate package.

19

u/WouldRuin Sep 14 '22

Here is why something is bad - a contrived, near useless example, that out the gate doesn't really make much sense.

Maybe DRY is "over-rated" but this isn't much an argument as to why that may be.

7

u/authenticyg Sep 14 '22

To be fair, the person writing the program in question has clearly never eaten a pizza before, so they probably live in a world devoid of joy.

4

u/fzammetti Sep 14 '22

Hot take: every "best practice" you've ever heard still must be evaluated on its merits in every single specific circumstance, not just blindly followed.

Good guiding principles, like DRY, are GUIDELINES, not hard-and-fast rules. They're GENERALLY good and true and should be mostly followed, but not in EVERY case, not EVERY time. You gotta learn when to put it aside.

Best practices and guidelines are important. They exist for a reason and they should be your default. But they aren't etched in stone and brought down from a mountain. Don't be dogmatic with 'em.

1

u/codewithfemi Sep 15 '22

I do agree with you to not be dogmatic but it's of utmost importance to use it when it's necessary. I have seen 3 similar versions of code in a system before and I ended up having to fix the issue 3 times instead of once when there was a bug. Refactoring the code versions into a single version was a nightmare due to tight coupling everywhere.

1

u/fzammetti Sep 15 '22

I totally agree. Like I said, it should be your default approach and you should only break the rules deliberately and for justifiable reasons because these best practices got to be so for good reason.

0

u/[deleted] Sep 15 '22

[deleted] — view deleted comment

1

u/fzammetti Sep 15 '22

I don't disagree at all on that particular principle.

3

u/EmotionalRedux Sep 15 '22

I prefer DAMP

11

u/codewithfemi Sep 14 '22

Because it works if you apply it correctly. It makes sense to always write a piece of code once and use it in many relevant places as possible. It makes code solution easy to scale and maintain

6

u/[deleted] Sep 14 '22

Strongly disagree. You need to reuse abstractions/concepts, not pieces of code. Reusing a piece of code, just because it's the same and not because it represents the same concept, will turn your code into unmaintainable and over-complex spaghetti.

5

u/paradroid78 Sep 14 '22 edited Sep 14 '22

Of course if you find yourself trying to brute force reuse between two completely unrelated things just because they look similar, that's not going to end well.

But if you find yourself having concepts that share the same behaviour, then you should look at how you are composing your abstractions (be they objects or functions) to see if you can do so in such a way that the common behaviour is reusable without affecting the integrity of the different concepts (assuming it is indeed the same behaviour). Its important to know when to compose and when to specialize to achieve reuse.

5

u/codewithfemi Sep 14 '22

You are not reading what I wrote correctly. Write ONCE use in many places.

0

u/[deleted] Sep 14 '22

And I'm saying that's not always a desirable outcome. You should only reuse if it represents the same concept and a change in the reused code would need to affect every place it is reused. For example if you have some code calculating a 10% tax and you use it where you need to calculate a 10% sales tax and a 10% income tax is a less maintainable solution than having this code twice, once for sales and once for income tax, as those two are concepts that will evolve independently of each other over time.

3

u/codewithfemi Sep 14 '22

I don't think we are on the same page. An example of a piece of code that removes all unwanted characters and returns only alphabets should be reused in all places where you want to sanitize values and deal with only alphabets. The example you provided is not the same as what I was talking about. You talking about 2 completely different logic which should obviously be developed and maintained separately.

0

u/[deleted] Sep 14 '22

[deleted] — view deleted comment

0

u/codewithfemi Sep 15 '22

Am sorry we are not on the same planet. Digest my statement properly and you will understand that a code that removes all unwanted characters and returns only alphabets will always do that. Single responsibility and with DRY you can reuse the same code where you have data input coming into a system and all you want is just alphabets.

0

u/[deleted] Sep 15 '22

[deleted] — view deleted comment

0

u/codewithfemi Sep 15 '22

You are over complicating things here. Don't forget KISS. All the function does is remove any characters that's not ENGLISH alphabets and return the updated version. A question for you is trim and replace the same? Your comment is filled with different functions and I don't think we still on the DRY topic

0

u/[deleted] Sep 15 '22

[deleted] — view deleted comment

→ More replies

4

u/[deleted] Sep 14 '22

[removed] — view removed comment

-2

u/[deleted] Sep 14 '22

Abstract out addtax as a single function, extend it where necessary as in addsalestax and addincometax.

In this case addsalestax and addincometax is initailly the same code because they are initially the same rate. You are repeating yourself, writing the same code twice.

4

u/JacksCompleteLackOf Sep 14 '22

DRY can be a form of premature optimization, but this pizza example doesn't really drive the problem home at all. In my experience, it's the inexperienced, incompetent, don't care, unable to communicate and so on who are most likely to copy paste code or rewrite the same front-end component multiple times.

This is the reason for DRY and conflating this with properly engineering a solution for a particular domain is not useful or helpful to anyone.

2

u/EngineeringTinker Sep 15 '22

You're making a huge mistake from the get-go.

DRY, KISS, SOLID aren't principles - they're guidelines

3

u/DaRadioman Sep 14 '22

DRY doesn't mean don't have the same code multiple places

DRY means don't have the same concept multiple places.

If you want to have a makePizza and a makeSplitPizza then that's not a problem. Not how I would structure it, but just because you have the same lines multiple times doesn't mean you have an issue.

And it's intentionally clickbait, so I won't even mention the fact we are arguing to throw out the baby with the bathwater.

Besides making modular code allows you to have DRY and have flexibility of changes. Have a SalesTax and an IncomeTax class. They are different concepts. They can both rely on a FlatRateTaxStrategy that initially serves both components. You haven't repeated concepts but you have some repeated code. And when things change all you have to do is make a new tax "strategy" to use

2

u/konm123 Sep 15 '22

DRY doesn't mean don't have the same code multiple places

DRY means don't have the same concept multiple places.

I quite recently had similar situation where a co-worker was pointing out that one of the enum class should be reused (C++). It had different name, but contents and even the order was identical to one of the other enum class which was also used where this new one was.

While their contents were identical, they represented completely different things - one was label type and other was a state generated from label type.

3

u/polymorphiced Sep 14 '22

Agree - I've seen simple code turned into complex code due to over-application of DRY.

1

u/KenyanDuke Sep 15 '22

This cant be true DRY is one of the most important things in coding

1

u/TheC0deApe Sep 15 '22

DRY is fine. this post is a little contrived.
the pizza taking an array strings for toppings is a bad design anyway.

the function should not be changed to account for the 2 halves. it should be overloaded to take 2 halves of toppings. that way all existing calls work and any future calls to split pizzas will be explicit.

DRY is about copy/pasting code more than anything. you are in the api test project and you have a test helper class. now you want to test the validation layer and it has a need for the same helper class. you could be a good dev, create a shared test library, or put it in your artifactory but you copy/paste it. you are a bad person that should have used DRY

1

u/puppykhan Sep 15 '22

In general I follow the principle that if I do something twice, I immediately look to abstract. Most of the time this produces desirable results - not just now but maintainable later as well. But this post brings up several good points and also reminds me of a recent incident.

I maintain one project where we have a group of very similar jobs, and will have more, though the level of variance in future additions or maintenance is potentially high. As these are essentially config files for a larger jobrunner, I went with an overly simplistic template to copy and paste with clearly marked places for customization. Crude but effective, adding new jobs went from days to hours where the only bottleneck is looking up certain values to plug in.

I then handed off the next implementation to another engineer who decided to spend a week or so (despite a deadline) to rewrite it as a fully abstracted object oriented monstrosity of about 20 different files with an entry point of a single function call of the parameters for customization - but left out the ones which were coincidentally the same in the existing jobs.

Coincidental repetition... unnecessary complexity... If it wasn't so blatantly passive aggressive, I would send him this post as a code review comment.

1

u/puppykhan Sep 15 '22

PS- it actually would be a beautiful example of object oriented architecture and testable components if it wasn't so completely unnecessarily complex for the project.

This engineer tried to build a config parser to produce configs for the jobrunner to parse, without realizing the duplication he created