this post was submitted on 20 Dec 2024
43 points (92.2% liked)

Programming

17658 readers
206 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 !webdev@programming.dev



founded 2 years ago
MODERATORS
 

DRY = Don't repeat yourself

top 10 comments
sorted by: hot top controversial new old
[–] jonc211@programming.dev 24 points 6 hours ago (5 children)

I’ve always understood DRY to be about not duplicating concepts rather than not duplicating code.

In the example here, you have separate concepts that happen to use very similar code right now. It’s not repeating yourself as the concepts are not the same. The real key is understanding that, which to be fair, is mentioned in the article.

IMO, this is where techniques like Domain-Driven Design really shine as they put the business concepts at the forefront of things.

[–] magic_lobster_party@fedia.io 1 points 1 hour ago

That’s how DRY is described in Pragmatic Programmer, where DRY was first coined. They’re clear that just because code look similar, doesn’t necessarily mean it’s the same.

[–] hono4kami@piefed.social 1 points 2 hours ago (1 children)

IMO, this is where techniques like Domain-Driven Design really shine as they put the business concepts at the forefront of things.

Do you have a resource on where to learn DDD? I feel like I never understood the concept well.

[–] Dunstabzugshaubitze@feddit.org 2 points 47 minutes ago

"Domain Driven Design" by Eric Evans, aka the blue book. It's very dense however and very object oriented, but concepts apply even if you dont work with object oriented languages, you might have to do more footwork to get from a domain model to services that adhere to the model.

"Head first Software Architecture" might be an easier on ramp and touches on simmiliar concepts.

[–] Dark_Arc@social.packetloss.gg 1 points 2 hours ago

It should be about concepts but it's more often applied to duplicate algorithms by inexperienced people (which is a huge mistake).

[–] sip@programming.dev 1 points 3 hours ago

I guess I never thought of it like this, but it resonates.

[–] Dunstabzugshaubitze@feddit.org 1 points 5 hours ago

yes, this is exactly what you have to think about. the left example even aknowledges that deadlines for "tasks" might be different from deadlines for "payments", which suggests that the abstraction is not "clean".

[–] wccrawford@lemmy.world 1 points 3 hours ago (1 children)

First off, I generally don't worry about DRY until there are 3 instances, not 2. With only 2, it's really easy to over-generalize or have a bad structure for the abstraction.

But otherwise, I disagree with the article. If it's complicated enough to bother abstracting the logic, the worst that can happen in the above situation is that you just duplicate that whole class once you discover that it's not the same. And if that never happens, you only have 1 copy to maintain.

The code in the article isn't complicated enough that I'd bother. It even ends up with about the same number of lines of code, hinting that you probably haven't simplified things much.

[–] Dark_Arc@social.packetloss.gg 2 points 2 hours ago

The code in the article isn't complicated enough that I'd bother. It even ends up with about the same number of lines of code, hinting that you probably haven't simplified things much.

I think it's a good example of the problem though. People take that same idea and apply it too liberally. The point isn't that specific code, it's about not apply DRY to code that's coincidentally identical.

But otherwise, I disagree with the article. If it's complicated enough to bother abstracting the logic, the worst that can happen in the above situation is that you just duplicate that whole class once you discover that it's not the same. And if that never happens, you only have 1 copy to maintain.

That's... Not at all true in practice. What often happens with these "DRY" abstractions when they've been improperly applied is you end up with an inheritance hierarchy or a crazy template or some other thing. You're really lucky if you can just copy some code and find your way out of the weeds.

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

[–] airbreather@lemmy.world 6 points 6 hours ago