Live data from Hacker News

Prefer duplication over the wrong abstraction

sandimetz.com

21–30 of 101 posts

Re: Prefer duplication over the wrong abstraction

#21
I see the author's point, but it overlooks code maintenance.

At step 4, where "Time passes" it should read, Programmer A fixes bugs, optimizes, adds features, etc. Repeat.

If the code is duplicated, the maintenance effort is also duplicated or the duplicates diverge significantly making them more difficult to understand in relation to each other. Why does methodA do this, but methodB does that?

Tools can help prevent ProgrammerB and ProgrammerX from coming in and stinking up the place. Set up sonarqube and let it tell them they must pass the quality gate before merging.

Re: Prefer duplication over the wrong abstraction

#22
Another useful rule: if you find yourself needing to change an existing abstraction, especially if you need to make it more complicated by (say) adding a parameter, this is often an indication that your design is broken, and that you need to change that so that you don't need the extra parameter. Most often in my experience the root cause of most problems of this sort is different data representations. Something is big-endian over here, little endian over there. This quantity is represented as a string over here, as an integer over there. I've found that being absolutely ruthless about being consistent in data representations goes a very long way towards keeping my code from spinning wildly out of control.

Re: Prefer duplication over the wrong abstraction

#23

This fits in very well with the concept write code that is easy to delete: http://programmingisterrible.com/post/139222674273/write-cod... And here's the link to that discussion earlier this year: https://news.ycombinator.com/item?id=11093733 Edit: fixed the typo where I said wife instead of write.

Kids these days. Back in my day, wife code was forever /s ;)

Re: Prefer duplication over the wrong abstraction

#24
post #17
post #7

Earlier quoted context omitted.

Duplication is fine until it gets out of hand, at which point you'll be able to identify the right abstraction.

Then you're in the situation that your code is a mess of copy-paste and its obvious what you should do, but management prefers that you work on features instead of changing "proven, reliable" code. Thus the mess is there to stay.

That bad culture is a separate problem which will make everything worse.

In your case, it seems mixed - while you do have to update code in multiple places the flip side is that you can more confidently change code in one location without breaking something else. If you're on a death-match piling up technical debt, that might be less painful than dense inter-connected code, albeit still worse than working somewhere with more long-term thinking.

Re: Prefer duplication over the wrong abstraction

#25
post #17
post #7

Earlier quoted context omitted.

Duplication is fine until it gets out of hand, at which point you'll be able to identify the right abstraction.

Then you're in the situation that your code is a mess of copy-paste and its obvious what you should do, but management prefers that you work on features instead of changing "proven, reliable" code. Thus the mess is there to stay.

[deleted]

Re: Prefer duplication over the wrong abstraction

#27

This fits in very well with the concept write code that is easy to delete: http://programmingisterrible.com/post/139222674273/write-cod... And here's the link to that discussion earlier this year: https://news.ycombinator.com/item?id=11093733 Edit: fixed the typo where I said wife instead of write.

Kids these days. Back in my day, wife code was forever /s ;)

Ha ha yeah, I came back to look and saw that interesting typo. Whoops.

Re: Prefer duplication over the wrong abstraction

#28
post #7

Earlier quoted context omitted.

Duplication is fine until it gets out of hand, at which point you'll be able to identify the right abstraction.

Yeah. I'd agree with this. I've had to maintain some terrible codebases with duplication everywhere. The truth is, I have an easier time fixing bugs in simple but duplicated code than I do fixing bugs in super abstract but DRY code. Which has me thinking: clear, obvious code with duplication is better than magic code which is DRY. Obviously, the best is DRY, obvious, and explicit... Also, a rule of thumb is premature…

This is an very important concept, "premature abstraction is bad"

Re: Prefer duplication over the wrong abstraction

#30
For a long time I've thought that Don't Repeat Yourself is the most important software engineering rule. I still do, but in line with this article, I've learned that some things that appear to be duplication if you just look at the literal code actually aren't. Just because you've got a chunk of ten or twenty lines that are identical right now doesn't mean that they are actually identical.

It's hard to give concrete examples of such an abstract concept, but one I encounter with some frequency is some chunk of code that grabs something from a URL. Assuming you're using some half decent library that takes care of the brute mechanics, it's easy to end up with repetitive code to deal with setting up a form and putting on a couple headers and dealing with authentication or whatever, and it's tempting to try abstract on that yet further. Yet I find all the attempts to do so make the problem worse, because the superficially-similar code is actually quite dissimilar. The pressures on the instances are different. Here I may be deeply concerned about SSL, there I may be adding header hacks for some broken auth scheme, over there I've got some sort of serialization problem. Trying to put them all behind the same abstraction is just pain. It turns out the level of abstraction offered by these often years-old, mature HTTP libraries is more correct than may initially meet the eye.

For code to be "duplicated", the full context of the code need to be duplicated, not just have superficial visual similarities. It's important that all the code with the same context, the same evolutionary pressures, the same likely change planes end up in one place, and it is equally important that code that lacks that similarity not end up bound together. In the end, it's the same error.

Post reply on HN