HeadlinesBriefing favicon HeadlinesBriefing.com

Why Code Duplication Beats Flawed Abstraction

Hacker News •
×

Duplication is cheaper than the wrong abstraction, argues a developer who’s seen this pattern repeat across projects. The core idea: extracting code into abstractions often creates more complexity than it solves. Programmer A might refactor duplicates into a new method or class, only for Programmer B to later modify it with conditionals to handle edge cases. Over time, the abstraction becomes a tangled mess of parameters and logic, making future changes harder.

This issue stems from the sunk cost fallacy—developers feel compelled to preserve their investment in the abstraction, even when it’s no longer useful. For example, a team might add parameters to an existing abstraction instead of rethinking it, leading to incomprehensible code that’s hard to maintain. The solution? Reintroduce duplication by inlining the abstracted code and trimming what’s unnecessary. This forces a fresh start, letting the code reveal cleaner abstractions.

The moral? Abandon flawed abstractions early. Adding conditionals to shared code is a red flag—the abstraction is likely wrong. While a few conditionals might help understand edge cases, they often signal it’s time to refactor. By embracing duplication initially, developers can avoid the pain of tangled abstractions and build systems that evolve more smoothly.