this post was submitted on 20 Dec 2024
94 points (93.5% liked)

Programming

17668 readers
215 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
94
Don't DRY Your Code Prematurely (testing.googleblog.com)
submitted 2 days ago* (last edited 1 day ago) by [email protected] to c/[email protected]
 

DRY = Don't repeat yourself

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 2 days ago (1 children)

There are plenty of bad abstractions in the wild and novices applying DRY is a common source of them.

You're both saying the same thing though. Novices aggressively apply DRY the moment a second bit of identical code appears, while experienced developers often wait for a third copy and then think about whether DRY fits.

That said, I think "don't apply DRY too aggressively is the whole point of this discussion, and the person you're replying to was kind of needlessly disagreeing.

[–] [email protected] 1 points 2 days ago* (last edited 2 days ago) (1 children)

You're both saying the same thing though.

We're not quite saying the same thing though because ...

It's not a 2 vs 3 issue. You can have an infinite number of instances of the same logic and it still not be a case for generalization because it's not actually general ... it's just an infinitely large program. You can also have two copies of the same code that should be reduced because they are general (e.g. you have the exact same algorithm for generating a UUID copied into two different spots). If you're thinking about it in terms of quantity you're already doing it wrong.

It's not fixable by "just" copying something.

Those two points are really important points.

[–] [email protected] 1 points 2 days ago* (last edited 2 days ago) (1 children)

If you're thinking about it in terms of quantity you're already doing it wrong.

You're ignoring that simple principles make great guidelines for not overthinking things.

And you're doing so in the context of an article about the dangers of overthinking things.

You've over thought an article about the dangers of overthinking, while alienating potential collaborators with a condescending tone.

You're coming across like one of the rookies who need this warning.

Consider counting to three, before applying DRY. It works.

[–] [email protected] 2 points 1 day ago* (last edited 1 day ago)

You're ignoring that simple principles make great guidelines for not overthinking things.

Name some great "simple principles;" everything has nuance and trying to distill things into "well it's just this simple principle..." is a great way to get catastrophic mistakes.

And you're doing so in the context of an article about the dangers of overthinking things.

You did not understand the point of that article if you think it's about the dangers of over thinking. The issue with DRY is that it leads to making refractors without thinking about whether or not the refractor makes sense. That's the exact issue the author is warning about, think about whether or not dry makes sense.

That has ABSOLUTELY NOTHING to do with how many times the code has been repeated. It has everything to do with why it's repeated.

You're coming across like one of the rookies who need this warning.

I'll toss that right back at you bud. You don't seem to understand the actual problem.

Consider counting to three, before applying DRY. It works.

It does not. I literally fixed a bug today because the same algorithm, doing the same job, was used in two different places formatted differently, exactly two, and they got out of sync resulting in memory corruption.

That's what DRY is intended to fix. Not "I have three [or whatever number] things doing the same thing so now I should DRY this code up", I've seen HORRIBLE refractors from DRY applied to 3 things; absolute spaghetti inheritance hierarchies that were "DRY."

I hate talking about DRY because it's this principle that so many people think "oh I'm doing it correctly; I'm doing good things!" and they actually make the code SO MUCH worse.

EDIT: Here's exact quotes from the article (emphasis theirs):

Applying DRY principles too rigidly leads to premature abstractions that make future changes more complex than necessary. Consider carefully if code is truly redundant or just superficially similar. While functions or classes may look the same, they may also serve different contexts and business requirements that evolve differently over time. Think about how the functions’ purpose holds with time, not just about making the code shorter.