logoalt Hacker News

Prefer duplication over the wrong abstraction (2016)

317 pointsby rafaeptatoday at 4:08 PM229 commentsview on HN

Comments

jstimpfletoday at 4:55 PM

Code duplication is the wrong abstraction too -- unless it's not really code duplication but code that only happens to be similar for some really "unstable" reason.

show 1 reply
dmos62today at 5:24 PM

If it's duplication, it's the same abstraction by definition. The fundamental unit of programming is intent, not code.

aapplebytoday at 5:10 PM

The smallest amount of simple code that solves the problem wins. Everything else is irrelevant.

nullbiotoday at 6:24 PM

Code duplication is terrible in the age of LLMs, unless you want to maximize on drift.

lazidetoday at 7:27 PM

Yes, but counterpoint - code duplication is also the wrong abstraction.

Pro tip - which is the least bad abstraction? Answer: it depends!

mohamedkoubaatoday at 5:46 PM

Duplication is often a small price to pay for isolation

hedoratoday at 5:10 PM

I’ve seen code bases that evolved like that. The problem is almost always outside the abstraction that has a pile of conditionals.

Usually, some moron decided to copy paste things a few levels up and then the top half of the system metastasized into two parallel universes of broken garbage.

For instance, one might decide to perform auth later in the flow so unauthorized handlers can run and set a “this requires auth” bit that defaults to false, and the other flow could add a forged auth header before the auth step.

Now, the auth handler needs a “allow forged header” flag and a “already authenticated” flag.

I’ve seen that grow to a half dozen cases until massive production dataloss occurred. A buggy client tried to delete something local to their account without specifying a userid as a parameter (this codebase was garbage!) and deleted the something for all users instead.

I can’t remember how the dataloss was “fixed”, but it definitely wasn’t “all requests go through a simple auth check, and all handlers declare/implement their auth requirements in the same way”.

Getting a design approved to require a user id be specified exactly once for account-level operations was fantasy land for that team. (Most hires with any sort of engineering talent bounced in under a year.)

Anyway the “abstractions are hard so copy paste” approach did provide job security for the lifers on that product. I can’t imagine them holding a job elsewhere, but they were completely immune to layoffs (hostage style).

This is a pretty valid approach if you’re an agent hired to perform industrial sabotage, or if you keep replacing keyboards after you knaw through the corner.

mcculleytoday at 5:31 PM

Yes, if your programming language/environment is weak.

sebastianconcpttoday at 5:00 PM

Oh the self-contradiction here...

Generalizing this in the abstract is a wrong abstraction.

TexanFellertoday at 5:13 PM

> Code duplication is far cheaper than the wrong abstraction

Very true in some sense, but I continue to encourage DRY-bias because I've literally never seen teams duplicate code responsibly and later dedupe it when it's the right time. 95% of the time this sentiment is quoted to justify shipping quick slop and stable reusable bits are never extracted into a shared lib later.

show 1 reply
ilveztoday at 5:55 PM

Just three words: rule of three.

vcryantoday at 6:53 PM

The sweet spot is really duplicating the wrong abstraction: I see you Claude!

johnwheelertoday at 6:46 PM

These are not mutually exclusive.

slopinthebagtoday at 5:26 PM

I prefer the go mantra: a little copying is better than a little dependency.

Abstraction is a vague term when used here. Is a shared function an “abstraction”? It’s more like implementation hiding, maybe some data hiding. But you definitely have a dependency on it now.

Acronyms like DRY are for beginners. Once you get good you know when to break the “rules” (and when not to).

atmanactivetoday at 4:53 PM

No it's not.

gilleswrtoday at 7:53 PM

[flagged]

aplomb1026today at 5:35 PM

[flagged]

SameerVers3today at 7:42 PM

[dead]

meeritatoday at 5:47 PM

[flagged]

Ozzie-Dtoday at 5:06 PM

[flagged]