News for hackers. Stuff that matters.
Re:Prefer duplication over the wrong abstraction by js8 on Saturday July 09, 2016 @11:55AM (#12061841)
How do you know that the abstraction is wrong (and how) if you don't create it?

It seems to me like the article is saying - don't ever make mistakes, they are costly.

Re:Prefer duplication over the wrong abstraction by stevedt on Saturday July 09, 2016 @11:59AM (#12061863)
Perhaps a better tag line would be "duplication is far cheaper than a bad abstraction".

The take-away from the article for me is if subsequent activity (the changes requiring parameters and if-branches) show it was or has become a bad abstraction, then undo the abstraction and re-introduce the duplication.

Re:Prefer duplication over the wrong abstraction by js8 on Saturday July 09, 2016 @12:02PM (#12061887)
Then, why not fix the abstraction instead?
Re:Prefer duplication over the wrong abstraction by stepanhruda on Saturday July 09, 2016 @12:05PM (#12061902)
Sometimes you don't know what the right abstraction is yet, you only know it's not the one you have.
Re:Prefer duplication over the wrong abstraction by nerdponx on Saturday July 09, 2016 @12:00PM (#12061871)
I think the point of the article is that, if you aren't totally sure that an abstraction is correct or justified, then it's better to avoid building one in the first place. Not saying to never make mistakes, it's highlighting a particular kind of mistake that people don't always think about, and suggests that you can avoid making this mistake by not being so obsessive with DRY
Re:Prefer duplication over the wrong abstraction by js8 on Saturday July 09, 2016 @12:10PM (#12061929)
I think such conservative approach can be justified when working on existing code, which has been somewhat proven to work. But I don't think it's justified when you're building the code.

I simply don't think you can always get the good abstraction right. But if you don't attempt it, you will never get it right. What you're advocating is not attempting it.

Re:Prefer duplication over the wrong abstraction by ktRolster on Saturday July 09, 2016 @12:41PM (#12062091)
If it's new code, and you are unsure what abstraction is needed, build the simplest abstraction possible. YAGNI

That way, as the code develops, and it becomes more clear what is needed, then you can easily build it at that time.

If you have code that needs to be duplicated, throw it into a function, a lightweight and powerful abstraction.

Re:Prefer duplication over the wrong abstraction by js8 on Saturday July 09, 2016 @12:57PM (#12062190)
That's what I am advocating in this thread. ;-)
Re:Prefer duplication over the wrong abstraction by smcl on Saturday July 09, 2016 @12:22PM (#12061993)
I think the point of the article is to catch eyeballs and get shared because "I'm pleased to announce that I'll be teaching a public Practical Object-Oriented Design course in San Francisco on May 9-11, 2016" ...
Re:Prefer duplication over the wrong abstraction by krick on Saturday July 09, 2016 @12:40PM (#12062089)
In some sense, yes, it's what article is saying. But yet I agree with it absolutely: it's basically the same thing I try to convey to some my colleagues for years.

Because the problem is that many programmers — almost all novices and even some pretty experienced ones — don't admit to themselves the simple fact, that they might be wrong in the what they are thinking right now, at the moment. Sure, they won't deny that they make mistakes sometimes, but yet somehow they are pretty self-confident while actually working. Basically, they think they are much smarter than they are. What's making the whole thing even worse is the fact that people (again, especially the novices) don't actually think about "how to make it cheaper (less painful)", they think about "how to code properly". Basically, "what should I do to not be laughed at, how do I seem smart?" They just know they must follow some "golden rules", so called "best practices". You know, the patterns, no goto, no code duplication ever. All that programmer etiquette.

That's why often instead of simple, short and not-elegant-on-purpose solution, non comprehendible, thousands of lines long abstractions are born. Programmer tries to justify it by that it "solves the problem we don't have yet", but usually it turns out it doesn't — which isn't a real surprise, as it's hard to solve the problem you don't know about yet. And while the crude and simple solution might have been rewritten in an hour, that not so perfect abstraction of yours requires days of works, and usually just keeps collecting hacks until it is so complicated that it's impossible to change anything without breaking something else.

Of course, the point is, as always, that you have to keep balance. But there is a strong bias towards "abstraction over duplication" and such, so it's better to push programmers towards simpler, imperfect solutions.

Re:Prefer duplication over the wrong abstraction by klagermkii on Saturday July 09, 2016 @02:15PM (#12062570)
I don't think it's necessarily just people trying to appear smart to their peers and creating abstractions to do so. I think it's an intermediate programmer problem that comes from seeing their own novice programmer code spiral out of control from sprawling redundancy. It comes from one too many meetings where someone says something like "can't you just change the website so that all the red links and controls are now blue", and kicking oneself in hindsight for not somehow tying it all together. And I think it culminates in programmers thinking that if only they'd made it with some kind of unified but flexible abstraction it wouldn't have been a problem.

So when it comes time to write the next app that "unified but flexible" becomes the mantra, and yes it is fun at the early stages of a project to fantasize about permutations and invent great abstraction towers to cover these cases. But I think the problem that people have is that they don't respect that flexibility comes in infinite dimensions, and that you can only pick and choose a few specific dimensions of flexibility to cover. Not only that, but each unnecessary dimension of flexibility added makes the code significantly longer and more complicated and just generally difficult to reason with. In hindsight from the previous project it all seemed obvious which dimensions should have been flexible, but making that call correctly up front is incredibly difficult.

Re:Prefer duplication over the wrong abstraction by js8 on Saturday July 09, 2016 @12:53PM (#12062164)
I think we can agree that the abstraction that has more lines of code than the original duplication is probably wrong. But that's something very different. I don't think abstracting duplicated code precludes sticking to YAGNI.
Re:Prefer duplication over the wrong abstraction by corecoder on Saturday July 09, 2016 @12:05PM (#12061909)
Not that I am that good at following her advice, but: you have to look at your code in a more dynamic way; you try several designs, thus several abstractions, and you know which abstractions are bad because they don't work for you at solving the problem at hand. That and some generic rules of thumb, like: prefer composition over inheritance.