Need help?
<- Back

Comments (247)

  • lg5689
    I believe that "single source of truth" is a principle that should always be followed. If there's duplicated code where it'd be a bug if they diverge, then you should refactor. It creates a long-distance coupling in your code that may be invisible to future developers until a bug emerges.But with that in mind, I mostly agree with the article: if it's not a violation of "single source of truth", then abstractions are just a convenience. If it starts being inconvenient, then it's not doing its job and there's no reason to use it. It's a serious code smell if a function needs several flags for custom behavior; that means it's probably the wrong abstraction or violating the single responsibility principle. If there is a legit need for lots of customization, an often-good way to handle is to take a function/functor as an argument for the customization. E.g., rather than `solve(f:double -> double, max_iters = 99, x_abs_tol = 1e-15, x_rel_tol = 1e-15, ...)` you can do `solve(f:double -> double, stopping_criteria: StoppingCriteriaClass)`
  • Waterluvian
    I think about this on occasion. Most recently I ran into an issue during a personal project: 2d sprites for RTS units were packed on spritesheets in a consistent manner: 5 sprites for 8 directions (you mirror 3). Packed in order of: stand, move, attack, die. So I made a loader that understands how to take action + direction and offer an array of sprites to play through.But then I came across more cases: sprites with no directionality (an explosion), and corpse sprites (which were only 4 directions, 2 mirrors, and most except the first four were shared by both orcs and humans).I agonized for a little bit on what the hell the common abstraction is for all this. In the end, I factored out some of the loading code, and made a UnitLoader, CorpseLoader, EffectLoader and moved on. Now, there's probably a better abstraction in there because all 3 loaders have to reason about the same things a little bit. But I will discover that abstraction later on and it's easier to just de-duplicate the code then, rather than try to identify the abstraction now and make some complicated EverythingLoader that handles all those cases.
  • bhouston
    I used to struggle with abstractions back in my OOP days but since moving pretty much to a purely functional approach I find that code duplication is rare. Just have a function and call it in two parts. The main abstraction issue is then data structures but with TypeScript interfaces being duck typing essentially I run into few problems there as well.So code duplication because of abstraction issues is rare. Code duplication because of siloed developers is so much more common.
  • aftbit
    Similarly, I've seen some developers who seem to think that any inline string or numeric constant is evil. In one PR, I saw: HTTPS_SCHEME = 'https' DOMAIN = 'www.example.com' url = HTTPS_SCHEME + '://' + DOMAIN I don't understand what they think this is buying, other than just cargo culting "don't embed constants." And of course, the constant definitions were at the top of the file and the url building code was hundreds of lines away.
  • strongpigeon
    Echoing the article, anyone who has experienced both will agree: it’s far easier to work with an under engineered code base than an over engineered one.
  • znkr
    +1 The worst code I had to maintain was code that tried to follow DRY (without the trying to understand what the original intention of that principle was). The only way out of that mess was widespread code duplication.
  • Rendello
    Two talks come to mind here: Mike Acton's Data-Oriented Design and C++ [1] and Brian Cantrill's The Complexity of Simplicity [2].Mike's talk argues that code solutions need not be modelled on the real world, and that different data creates different problems, which need different solutions. I can't do the talk justice, but it's had a big impact on me.Brian's talk is about abstraction generally, and how it's difficult to find the "right" abstraction.1. https://www.youtube.com/watch?v=rX0ItVEVjHc2. https://www.youtube.com/watch?v=Cum5uN2634o
  • ultim8k
    Nobody wants to listen. Nobody. In 90% of the companies there are some so called senior devs that get ecstatic when they create a new abstraction.Overengineering, abstractions and premature optimisation are the 3 worst plagues of engineering.At the same time I’m happy they exist because it means we’ll always have a job.
  • irishloop
    Too many abstractions are bad. Too many code duplication is bad.Part of being a good engineer is finding the right balance.I know engineers who would gladly duplicate code all over the code base to avoid creating a new abstraction.I know engineers who create polymorphic abstractions for a single caller with a very obvious set of parameters.So much of wisdom is in finding balance and not being dogmatic about rules.
  • cryo32
    You can do both with microservices!
  • infinitebit
    I feel this deeply. Although abstraction isn’t a one way door, “deduplicating” logic tends to be much easier than breaking big functions back down, and so these days I tend to leave a comment with the date wondering if it is too similar to some other code. then if i come across it again months later and it still is, then maybe it is safe to make it DRY.I think DRY is the first heuristic for “good code” that most junior devs actually grasp, and so they become very dogmatic about it for a while
  • dofm
    No it's not. This has always been a needlessly iconoclastic rather than sensible suggestion.At the very least it is not once you're working at the wrong kind of scale.Once you have an awkward number of customers (more than five and less than a hundred), maintaining duplicated code that should have been abstracted and modularised will only seem cheap if you don't mind that you burn through even junior employees at a pace.And in the LLM era the wrong kind of scale appears in different ways; code generated and duplicated without proper abstraction and then maintained by an LLM that cannot be trusted to do the same modification each time it encounters a pattern or to have enough of an overview to slowly rescue duplicated code through good abstractions.I would go as far as to say that any abstraction you can maintain (that is in active maintenance, I mean) is better than code duplication once you are past a de minimis threshold.
  • agentifysh
    i recall very early in my career i did exactly this. i took what worked duplicated it—my reasoning being that it was far safer to reuse what has been battle tested and leave refactoring at a later stageit wasn't received well and senior developer told me that 'good developers know exactly what patterns to use all the time before writing any piece of code and that he will clean up my mess'long story short his refactoring caused what was otherwise a stable system into a complete mess and it reminded me of Nassim Taleb's book
  • platz
    2016 (up to 2018 or so) may have been the peak of such varied activity in the developer ecosystem, including articles like this, whether it was discussion, ideation, OSS variety, language development.There has been growth since but it's been concentrated into fewer channels and somewhat industrialized.
  • stevefolta
    "If you have a procedure with ten parameters, you probably missed some." -- Alan Perlis
  • dang
    I dislike duplicate code as much as anyone, but agree with the OP that bad abstractions can be worse. They add confusion and complexity which compounds over time, since people are forced to build on top of them in ways that (by definition) don't suit the underlying domain and ultimately become self-referential. This leads to contortions, workarounds and even more bad abstractions which ought not to be there—they're reactions to the code not fitting the problem, or as Fred Brooks called it, accidental complexity. You end up in an evolutionary dead end where the system is hard to extend because it's too hard to understand.I've learned to tolerate a small amount of duplicate code for this reason. If the duplication remains small, it's not that harmful, and if it starts to grow, one has a better shot at finding a good abstraction for it. Bad abstraction is premature abstraction.One thing I'm not sure this thread has mentioned yet is how LLMs alter the cost-benefit curve of this. They are much better at managing duplication than humans are, and much better at noticing inconsistencies - the sort of small bugs which duplication traditionally leads to. I don't know if this is enough to count as a different kind of good abstraction; I doubt it. It reminds me of a petroleum economist I once knew who had 200 duplicate spreadsheets analyzing different projects and who hired a junior analyst to keep them all consistent. An LLM would be like the junior analyst.
  • stcg
    This is like saying "A slow leak is cheaper than a burst pipe"Yes, okay. But with both you will have a bad time cleaning up.There is a third option: good abstractions.I did see this pattern described in the blog in practice a lot (and fell victim to it myself) and I think that in general this comes down to inexperienced programmers. Object oriented programming makes it worse.Teaching these programmers that they should not abstract is not the solution. It is blocking their growth.Teach them how to make better interfaces instead.
  • fjfaase
    I once used code duplication to implement a fourth type of dialog that looked somewhat similar to the others, that were sharing a lot of code, because I felt that although it looked much the same as the others, there was some fundamental difference. Took me about a day to implement. When some other engineer saw this, he spend the next three weeks trying to integrate all of them with some shared class. His work was not completely worthless, because he did find some small bug during all his efforts to avoid any possible code duplication. I already had predicted that it would take a lot effort, but I did not object, because I hoped that he would learn something from it and the next time think twice before always trying to avoid code duplication.
  • Verdex
    Cheaper is skipping a step.Code duplication and 'wrong' abstractions both count themselves amongst the other foibles of programming. But they don't directly produce a cost which can be cheap or expensive.They produce some other high dimensional intermediate value which can then produce highly variable cost dependent on the domain, goals, and scenario.As ever, it depends.The depends is quantifiable, but it doesn't fit in a blog post. Think more along the lines of war and peace.
  • time4tea
    You dont know immediately if something that superficially seems the same actually is.Copy and paste once is fine, twice, not so much.Often I've seen two totally different things exist in one bit of code, no overlap!Premature generification is bad, and leads the developer to believe that two things are the same, making it harder to see they are not.Also, can make it much harder to see that a different abstraction would give a cleaner outcome....
  • jbvlkt
    It depends if duplication is accidental or real. I.e. if two taxes are using the same formula, it is accidental. If you use the same physic formula on multipla places, it is real duplication.
  • christophilus
    Yes. I’m dealing with a graphql, urql, Next, Prisma stack at the moment. Something that would be a handful of lines of code in a different stack ends up being hundreds in this one.The Node ecosystem is full of wrong abstractions.
  • hakunin
    I think what this advice is really getting to, is that you should prefer everything generally build-time/hardcoded/static rather than runtime/dynamic. Wrote about this in 2013 (calling it a CMS trap[1], back then seemed pertinent).[1]: https://max.engineer/cms-trap
  • dang
    Related. Others?The Wrong Abstraction (2016) - https://news.ycombinator.com/item?id=35927149 - May 2023 (69 comments)The Wrong Abstraction (2016) - https://news.ycombinator.com/item?id=27095503 - May 2021 (17 comments)The Wrong Abstraction (2016) - https://news.ycombinator.com/item?id=23739596 - July 2020 (240 comments)The Wrong Abstraction (2016) - https://news.ycombinator.com/item?id=17578714 - July 2018 (207 comments)Prefer duplication over the wrong abstraction - https://news.ycombinator.com/item?id=12061453 - July 2016 (96 comments)The Wrong Abstraction - https://news.ycombinator.com/item?id=11032296 - Feb 2016 (119 comments)
  • fpoling
    With LLMs the cost of duplication is much lower both to write and maintain. So abstractions needs much higher justification.
  • andix
    I always try to design in a way, that using abstractions/shared logic is optional.I've worked in too many projects, where every new feature needs to be built on top of existing abstractions, that often lead to severe restrictions if something slightly different is required. I always try to create reusable units/components, that can either be used as intended or replaced by something that behaves slightly different if needed.Components are not necessarily frontend components, this extends also to backend logic.
  • MrGando
    I once had to work with a system that was refactored and abstracted away heavily to use Redux. It didn't work then, the implementation had way too many abstractions, doing any change meant you had to touch dozens of files. It was insanity. Left me with a bitter taste regarding the redux pattern for ever (probably not the pattern's fault).
  • antonymoose
    Twice a coincidence, thrice a pattern.
  • zadikian
    I always felt like I abstract and modularize things way less eagerly than other programmers. Was pleasantly surprised to find that LLMs do it mostly my way by default, then again they're also bad at abstracting when it's actually needed.
  • originalcopy
    While I see the point, I think I more often encounter the opposite. Duplication, but not exactly duplication. Then the "sunk cost fallacy" is not an issue but there is huge maintenance cost and no-one feels like refactoring it. I'd rather refactor bad abstraction than 10x duplication.
  • felooboolooomba
    The bad thing with abstractions is when you start it too early in your code base for things. It's also a bad thing when you start it too late, although not as bad. If you start it way, way, way too late it's very, very, very bad.Of course, the worst abstractions are the ones you don't need at all.
  • bogrollben
    One thing I don't see talked about often is the fact that not all duplication is equal. Duplicated html/xml/markup does not equal template-based boiler plate, which does not equal almost everything else. I'm far more forgiving of duplicate html/markup because that code is so cheap.
  • northisup
    Duplication is fine, triplication and above is the issue.
  • omoikane
    > Programmer A sees duplication.This step should also be parameterized by how many times the duplication has occurred. Refactoring preemptively may lead to poor abstractions, but not refactoring after seeing the exact same thing tens of times would also be weird. See also:https://wiki.c2.com/?DuplicationRefactoringThresholdhttps://wiki.c2.com/?ThreeStrikesAndYouRefactor
  • alkhimey
    The disadvantages of duplication are greatly reduced in the world of AI. From my experience it can easily detect the duplicates and refactor code safely. On the other hand, code without abstractions is easier to read and easier for AI.With AI, we really need to rethink the clean code principles.
  • LunicLynx
    The generic repository "pattern" is the prime example of this. There might be CRUD operations shared between repositories, but they should not be that base of every repository. I've seen this so many times, because in the beginning the CRUD stuff is what you code over and over again and then suddenly business logic emerges and everything breaks down, but the repository prevails because sunk cost fallacy ...
  • bazoom42
    Depends. If the abstraction is just a level of indirection, then it is usually pretty simple to eliminate - just hit “inline function” in the refactoring tool a few times.On the other hand it is pretty difficult and error prone to consolidate duplicated code which have drifted apart over time.If in doubt, chose the approach which is simplest and least risk to revert if you discover in the future you made the wrong choice.I do agree a bad abstraction can cause huge problems. But it’s usually not the kind of abstractions introduced to eliminate code duplication, but the kind of top-down “architecture astronaut” abstractions, where a model is chosen which does not fit the complexity of the problem.
  • luckystarr
    How I see this:Refactoring code to reduce the number of lines is _compression_, akin to RLE coding.Refactoring the code to lift conceptually coherent parts is _abstraction_.Less compression, more abstraction. Then you're fine.
  • gb2d_hn
    Interface over inheritance is the paradigm I try and stick to. I'd rather maintain orthogonal code than code with overuse of inheritance because of over adherence to DRY.
  • davnicwil
    One way I like to think about is that often abstraction is an automation for a task that doesn't need automating.You hardly ever change the thing and if you do, changing it in two or three places 'manually' is really not a big deal.Now changing something fairly often, that affects logic in 50+ places? Then it makes sense to automate with an abstraction so it all flows through the same lines of code.I know I've personally spent way more time over the years debugging bad abstractions than changing things in a few places.
  • joshmoody24
    I've seen the pendulum swing between duplication and abstraction a few times in my career, and I'm currently on team "it's usually not that hard to find a good abstraction up front."IMO it's easier to inline a bad abstraction than it is to consolidate a bunch of subtly different things that should have been abstracted from the beginning.But I expect people's opinions on this differ wildly based on their personal experiences. Just my anecdotal take.
  • bob1029
    If you work backward from the schema these sorts of things tend to evaporate before they can become a problem.Some of the biggest rabbit holes come from naming conventions not aligning across the business and technology silos. If everyone agrees that Customer has exactly 34 attributes, then it is possible to move to the next step of sharing libraries of types across the team. Getting your POCOs/DTOs 1:1 across the board is when the duplication really starts to melt away.
  • threethirtytwo
    Duplication and the wrong abstraction are looking more and more to be implimentation details handled by AI. A possible future may be a place where none of this matters.
  • DmitryOlshansky
    I would argue that _premature_ abstraction is worse than _some_ duplication of code.Also I’ve seen the kind of codebase that seems to be LZW packed due to the sheer desire to DRY everything out. Not pleasant thing, by the time you goto 10 layers deep on some “helper” function you forgot why you in there.
  • _pdp_
    The biggest mistakes young engineers make is working out a problem from bottom up... i.e. building frameworks and libraries, rather than exploring the problem space which is more chaotic.You cannot find the edges of the system with structure you don't understand because once the abstraction are set in place solutions often have the same shape as the frameworks which leads to ultimately really bad systems.The best way is often not the obvious way. Once you reach the edges then you can think how to program the abstraction but that is many versions down the line from the original.
  • andai
  • tetha
    I watched a talk by her about this, and this post is missing half of the equation, which is really important:Having a wrong abstraction means you end up with a class/function/module with a huge amount of configurations through boolean/enum parameters. It's not even clear that all combinations of configurations is even valid. This situation may be simplified by duplicating, and then eliminating code, thus creating more streamlined code for each use case. This may require fixing similar or cross-cutting bugs in multiple places (eg: JSON serialization is stupid, need to hack a workaround), but keeps the business logic changes simple. Maybe a bit more numerous, but the code is able to raise all the scenarios to consider.Having no abstraction means you may have to change business logic consistently in multiple places, or you have to fix exactly the same misconception (aka a bug) in multiple cases. e.g. tax rate management in a multi-national context. This is also terrible, because you may fix an important problem in one place and forget other places with the same issue. Now you missed 12 potential bugs by fixing one. This can however allow you to discover a true abstraction. Maybe these 12 places should call just one place?But for code evolving across a team understanding this tension, a bit of duplication while waiting for confirmation that these pieces of code break together and change together is better than just shoving the same 3 if-statements into a function to avoid "line duplication". Concept duplication is more important.
  • KHRZ
    This is the biggest lesson I got from LMMs. I have a 1 million LOC vibe coded project that I can only imagine would fit in a few hundred thousand lines. But it's still holding up, I expected some kind of development collapse long before this point.
  • anon-3988
    The problem with coming up with a rule that works for everyone is that everyone have a different idea of what makes a good abstraction.Do you want to iterate using for loop or using .iter().step(2).map()?I would rather have consistency than a mixed bag of levels of abstractions.
  • jeffypoo
    I've always told engineers to duplicate until the abstraction is punching them in the face.
  • williadc
    The "99 Bottles of OOP" book mentioned at the bottom was an excellent introduction to refactoring. I highly recommend it if you struggle with finding the right data models for the problems you work on.
  • ozgrakkurt
    The discussion around this topic would be nicer if the title had "can be" instead of "is".Otherwise what is better is better and we don't know what we don't know
  • jstimpfle
    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.
  • dmos62
    If it's duplication, it's the same abstraction by definition. The fundamental unit of programming is intent, not code.
  • aappleby
    The smallest amount of simple code that solves the problem wins. Everything else is irrelevant.
  • ChrisArchitect
  • hedora
    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.
  • mohamedkoubaa
    Duplication is often a small price to pay for isolation
  • sebastianconcpt
    Oh the self-contradiction here...Generalizing this in the abstract is a wrong abstraction.
  • mcculley
    Yes, if your programming language/environment is weak.
  • nullbio
    Code duplication is terrible in the age of LLMs, unless you want to maximize on drift.
  • TexanFeller
    > Code duplication is far cheaper than the wrong abstractionVery 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.
  • ilvez
    Just three words: rule of three.
  • anon
    undefined
  • lazide
    Yes, but counterpoint - code duplication is also the wrong abstraction.Pro tip - which is the least bad abstraction? Answer: it depends!
  • vcryan
    The sweet spot is really duplicating the wrong abstraction: I see you Claude!
  • johnwheeler
    These are not mutually exclusive.
  • slopinthebag
    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).
  • atmanactive
    No it's not.
  • gilleswr
    [flagged]
  • aplomb1026
    [flagged]
  • meerita
    [flagged]
  • SameerVers3
    [dead]
  • anon
    undefined
  • Ozzie-D
    [flagged]