DRY = Don’t repeat yourself
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.
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.
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.
As already mentioned, the blue book by Evic Evans is a good reference, but it’s a ittle dry. Vaughn Vernon has a book, “Implementing Domain-Driven Design” that is a little easier to get into.
Personally, I found that I only really grokked it when I worked on a project that used event-sourcing a few years back. When you don’t have the crutch of just doing CRUD with a relational database, you’re forced to think about business workflows - and that’s really the key to properly understanding Domain-Driven Design.
Yeah for me the understanding really came when working in a federated GraphQL API. Each team had us own little slice of overall object graph, and overlap / duplication / confusing objects across the whole domain were a lot easier to see in that environment.
“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.
If you can work with python cosmic python (http://www.cosmicpython.com/book/preface.html) is a great resource
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”.
I guess I never thought of it like this, but it resonates.
Ultimate DRY: just keep refactoring the one method to accept hundreds of parameters and do everything.
Add two numbers? DoIt(1, 2);
Subtract? DoIt(null, null, 3, 1);
Etc.
invokeOperation(new Object[]("multiply", 2, 5))
This guy seniors
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.
I personally factorize as soon as there are two copies, but do not hesitate to inline the code and redo the abstraction when there is a 3rd use if it doesn’t fit. I find it much easier to inline and re-abstact a bad abstraction, than check if two copies are indeed identical.
The exception is business logic. Usually I want all of them to be dupplicates because there is a very high chance that it’s just accidental that part of the logic is similar. I take great care to have good primitives but the actual business logic that glue those primitives together is written as many time as needed.
Yeah, I’m reminded of how Germanic languages used to have singular, dual and plural. If we’d still had dual, we’d probably also be talking about not abstracting until we actually have a plural.
Another older blog post saying the same: https://sandimetz.com/blog/2016/1/20/the-wrong-abstraction
This is silly. Everyone knows that DRY is telling you that if you do the same sequence of mouse clicks three times in a row, you should spend the day writing a script to automate the task instead of quickly finishing what you were doing by doing the same sequence of clicks a fourth time. If you are supposed to apply it to the code you write, then there’d never be boilerplate-heavy languages like Java.
OOT, I’m pretty sure I saw you on OpenMW forum before
I’m one of OpenMW’s developers, so that’s understandable.
Ahh yes. Nice to see you on the Fediverse.
We’ve had !openmw@lemmy.ml for ages, and been present on Mastodon and Matrix for a long time, too.
deleted by creator
I thought I was clear enough there that I could get away without a
/s
at the end. Of course the real meaning isn’t it’s a really good idea to spend a day automating four mouse clicks you only need to do one more time.Ha, I see.
Yeah, sarcasm over text forums is sometimes difficult to pick up on.