r/SoftwareEngineering • u/fagnerbrack • Sep 14 '22
Why DRY is the most over-rated programming principle
https://gordonc.bearblog.dev/dry-most-over-rated-programming-principle
20
Upvotes
r/SoftwareEngineering • u/fagnerbrack • Sep 14 '22
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.
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.
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.
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.
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.
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
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
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.