News for hackers. Stuff that matters.
Re:Prefer duplication over the wrong abstraction by jbooth on Saturday July 09, 2016 @12:57PM (#12062195)
So then you make all your nice pretty abstractions, and something comes from the business where it needs to be different in some wacky pathological case. Now your abstractions suck.

Duplication leaves you room for that messy business logic. Increased abstraction often means increased coupling, and that can be much harder to maintain. It's a judgment call on a case-by-case basis.

Re:Prefer duplication over the wrong abstraction by TeMPOraL on Saturday July 09, 2016 @01:34PM (#12062384)
What you should do, when messy business requirements break your pretty abstraction is a) find an appropriate new abstraction, and b) refactor the current code to use the new abstraction. Abstractions aren't forever, they're meant to change and be replaced as appropriate.

Now doing the Right Thing may sometimes require additional work even for a simple (but unfortunately abstraction-breaking) feature. That's why people like to duplicate and then leave things duplicated - because it's the lazy option. You can get some more technical debt in exchange for being done this afternoon. But we all know what happens when you accumulate too much of that debt.

Re:Prefer duplication over the wrong abstraction by jbooth on Saturday July 09, 2016 @01:37PM (#12062404)
It's sometimes actually the best option. We're talking about principles in a vacuum here but I have seen so many tall class hierarchies where now in order to change one isolated feature that shouldn't affect the rest of the system, I'm creating consequences for the whole system.

Decoupling is a more important principle for maintenance than DRY. Obviously you want to factor out common bits of utility code but you absolutely do not want to force all code into the same model.

Re:Prefer duplication over the wrong abstraction by TeMPOraL on Saturday July 09, 2016 @01:46PM (#12062456)
I think the problem here is with inheritance, which I personally consider a code smell. There are few specific places when you need it, and by using inheritance there you're strongly signalling a particular abstraction happening - one of the "is-a" type. It definitely should not be used for just code deduplication.
Re:Prefer duplication over the wrong abstraction by sitkack on Saturday July 09, 2016 @01:03PM (#12062233)
If refactorings were described as bidirectional ST (syntax-tree) transformations we would automatically get the defactoring and the refactoring.

I could definitely use "inline method" or "name mangle method and special case".

https://en.wikipedia.org/wiki/Bidirectional_transformation

Re:Prefer duplication over the wrong abstraction by gnaritas on Saturday July 09, 2016 @01:33PM (#12062382)
If new business rules completely invalidate your abstractions in ways that aren't easily fixable, you're doing it wrong. Those wacky pathological cases should just be new implementations that plug right into your existing stuff, even if it requires slightly reworking the abstraction it's better than giving up and duplicating everything. If you're finding duplication easier, it just means you're abstracting things poorly and need to get better at doing it. Mostly likely, you've abstracted too early before you had enough different cases to actually understand how to split the concrete from the abstract correctly.
Re:Prefer duplication over the wrong abstraction by TeMPOraL on Saturday July 09, 2016 @01:36PM (#12062398)
> If new business rules completely invalidate your abstractions in ways that aren't easily fixable, you're doing it wrong.

Why, you might have been doing it right all along. The abstractions were good for old business rules, but are wrong for new business rules. You need to replace your abstractions with better ones. You can't expect to predict future business requirements perfectly 100% of the time; there's no shame in having to rewrite stuff to accomodate changing reality.

Re:Prefer duplication over the wrong abstraction by gnaritas on Saturday July 09, 2016 @01:38PM (#12062410)
Good abstractions aren't about predicting, they're about being nimble so that when change does come along, it's easy, because you don't have a ton of duplication to fix in order to change the abstraction. The new business rules don't belong in the abstraction in general anyway, they belong in the concrete implementations, that's the point of having implementations.
Re:Prefer duplication over the wrong abstraction by jbooth on Saturday July 09, 2016 @01:38PM (#12062418)
Like I said, it's a judgment call on a case by case basis. Taking 'DRY' to an extreme leads to an overly self-coupled code base, taking decoupling to an extreme leads to the need to apply the same fix in multiple places.
Re:Prefer duplication over the wrong abstraction by gnaritas on Saturday July 09, 2016 @01:45PM (#12062451)
You can remove the judgement call by making it a design rule, as extreme programming did long ago. When you've duplicated something 3 times, it's time for some abstraction because you have enough examples now to see what needs abstracting and what doesn't. The problems you're describing are just symptoms of premature abstraction. Abstractions tend to fail when they come before the concrete, they tend to succeed when they were lifted out of several examples of the concrete.
Re:Prefer duplication over the wrong abstraction by jbooth on Saturday July 09, 2016 @02:12PM (#12062558)
Agree with all of that. My hatred of extreme DRY is due to having been burned by premature/poor abstraction so many times.

Also, there are even exceptions to that rule.. I've seen a number of cases, let's say you've got a bunch of command line utilities that launch different jobs, there's some code that's duplicated across like 10 of them. Be careful with deduplicating that, you can wind up forcing very different things to pretend they're the same.

To put it another way -- it doesn't make a whole lot of sense to abstract some "forNtimes(closure, N)" function instead of just writing a for loop. There's a limit.. that example is laughable but I've seen a lot of abstractions that actually increase the amount of code in my day.

Re:Prefer duplication over the wrong abstraction by gnaritas on Saturday July 09, 2016 @04:40PM (#12063228)
We're probably in close agreement then. However I'll disagree with your last statement as that's actually often a standard abstraction and the clearer less error prone way to write an n times loop. I'd rather see

    20.timesRepeat(() => doStuff());
than

    for(var x = 0; x<20; x++){
        doStuff();
    }
for and foreach loops are virtually always in need of some cleaner functional abstractions that are already standard parts of virtually every collections library and are far more amenable to chaining. Abstracting loops is not premature abstraction, they're already abstracted in your collections lib.
Re:Prefer duplication over the wrong abstraction by jbooth on Saturday July 09, 2016 @04:54PM (#12063273)
That depends on the language, I love flatMap, but I never want to see functions attached to the integer type. Just stop with that :) Principle of least astonishment and all.
Re:Prefer duplication over the wrong abstraction by gnaritas on Saturday July 09, 2016 @05:11PM (#12063346)
An integer is an object, objects have methods, such methods are standard fare; if it's astonishing, try more languages. This is the idiomatic way to do this for example in Smalltalk.

    10 timesRepeat: [ self doStuff ]
What's relevant isn't whether you're astonished or not, but whether it's idiomatic or not. If you can't see an integer as an object with methods, then you haven't fully grokked OO.
Re:Prefer duplication over the wrong abstraction by jbooth on Saturday July 09, 2016 @06:07PM (#12063551)
I've tried lots of languages, thanks. It's still astonishing and will always be astonishing.

WTF does "timesRepeat" have to do with what an integer represents? Nothing, that's what. Integer is a noun, conventionally a 32-bit value type, while "timesRepeat" is a control structure. It's absolutely crazy for "timesRepeat" to be a member of integer.

http://thecodelesscode.com/case/223

Re:Prefer duplication over the wrong abstraction by gnaritas on Saturday July 09, 2016 @07:43PM (#12063876)
You're completely ignoring what I said, idomatic; this is how it's normally done in Smalltalk, and it's absolutely idiomatic to hang control structures off objects since that's how the entire language is implemented from top to bottom, it's not crazy at all, it's good design that keeps the language itself minimal. As for what #timesRepeat: has to do with Integer, I'd think that's pretty obvious, an object should be able to enumerate itself, to write a loop where you inc a counter is procedural, not object oriented and I'm very much talking about OO here, not procedural programming.

Same thing applies to ranges.

    1 to: 10 do: [:num | Transcript show: num  ]
#to:do: is a control structure on Number of which Integer is a subclass. Anonymous methods even have methods, consider the basic while loop.

    [ aList isEmpty ] whileFalse: [ aList removeFirst ]
#whileFalse: is a control structure on BlockContext, the class represented by [ ]. No, there's nothing weird at all about hanging control structures off of system objects, that's one of the best ways to design a minimal language and avoid the plague of a hundred reserved words that you encounter in C variants.