TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Wrong Abstraction (2016)

654 pointsby mkchoi212almost 5 years ago

61 comments

Tainnoralmost 5 years ago
I feel some people here are misunderstanding the blog post.<p>Sandi Metz IMHO doesn&#x27;t claim that the problem occurs at step 2 or 3. She doesn&#x27;t claim that it&#x27;s wrong to introduce abstraction when there is duplication.<p>What she is saying instead is that the problem occurs from step 6 onwards: when you find yourself wanting to reuse an abstraction that, regardless of whether it made sense in the first place or not, has outlived its usefulness.<p>I think this is in agreement with other points that she often makes, about being bold, but methodical about refactorings.<p>The whole discussion about &quot;you should never abstract away code before you see the third duplication&quot; has little to do with the article, and I&#x27;m also really not sure it&#x27;s good advice.
评论 #23740704 未加载
评论 #23740633 未加载
评论 #23746638 未加载
评论 #23740572 未加载
评论 #23740576 未加载
评论 #23741836 未加载
评论 #23742707 未加载
评论 #23743262 未加载
评论 #23742650 未加载
评论 #23740478 未加载
评论 #23746073 未加载
评论 #23740515 未加载
jpswadealmost 5 years ago
You can’t plan for what you don’t know.<p>This is why I like the &quot;Rule of three&quot;[1]. Only once you&#x27;ve done it three times will you truly begin to understand what the abstraction might need to look like.<p>1. <a href="https:&#x2F;&#x2F;wade.be&#x2F;2019&#x2F;12&#x2F;10&#x2F;rule-of-three.html" rel="nofollow">https:&#x2F;&#x2F;wade.be&#x2F;2019&#x2F;12&#x2F;10&#x2F;rule-of-three.html</a>
评论 #23740677 未加载
评论 #23739964 未加载
评论 #23740654 未加载
评论 #23739911 未加载
评论 #23739923 未加载
Pxtlalmost 5 years ago
Every Line Of Business codebase I&#x27;ve worked on has been the worst &quot;there I fixed it&quot; copypasta spaghetti, and has never made it to the point where &quot;maybe we shouldn&#x27;t add a parameter to this existing, cleanly abstracted method to handle this new similar-but-distinct use-case&quot; was anywhere near my radar for abstraction.<p>I would <i>love</i> to have developers where my problem was &quot;maybe you piggybacked on existing code <i>too much</i>, in this case you should&#x27;ve split out your own function&quot;.
评论 #23739860 未加载
评论 #23739931 未加载
评论 #23740031 未加载
评论 #23739846 未加载
评论 #23739777 未加载
评论 #23740126 未加载
评论 #23739765 未加载
评论 #23739976 未加载
leto_iialmost 5 years ago
As I gain more and more experience (I would now call myself more or less a mid-level developer), I find that the distinction that matters is not abstraction vs duplication, but the one between developer mindsets.<p>I have many times met&#x2F;worked with people who think the main task of the developer is to &#x27;get shit done&#x27;. Regardless of their level of experience, these developers will churn out code and close tickets quite fast, with very little regard for abstraction, design, code reuse etc.<p>Conversely, the approach that I feel more and more is the correct one is to treat development as primarily a mental task. Something that you first think about for a while and try to design a little. The actual typing will in this case be a secondary activity. Of course, this doesn&#x27;t mean you shouldn&#x27;t iterate on your design if during execution problems come up. Just that the &#x27;thinking&#x27; part should come before the &#x27;doing&#x27;.<p>My feeling is that with this second approach the abstraction&#x2F;duplication trade-off will not matter so much anymore. With enough experience you will figure out what you can duplicate and what you can design. And when you design you will develop an understanding of how far you should go.<p>Approaching development as a task of simple execution I think inevitably leads to illegible spaghetti down the line.
评论 #23741265 未加载
nfw2almost 5 years ago
When considering whether some abstraction is &quot;right&quot; or &quot;wrong&quot;, another important thing to consider is how cleanly the abstraction fits into a mental model of how the program works. Good abstractions provide value outside of removing duplication. They help us reason about a program by providing compression of logical concepts.<p>Consider some helper function: &quot;convertSnakeToCamelCase.&quot; This abstraction would take a string, do some operations on it, and return another string. It is easy to understand what the input and output is without having to think about these operations. This abstraction provides a benefit for anyone having to think about the program because it reduces the amount of concepts the reader has to parse from N (where N is the number of operations) to 1. This is helpful because people have limited mental bandwidth and can only reason with a finite number of concepts at any given time.<p>Consider a different helper function: “processDataPayload.” This function takes data in some arbitrary complex shape and returns data in some arbitrary complex shape. The abstraction effectively communicates nothing to the reader, and it is actively unhelpful because it forces that person to follow a reference, remember all the details of what that function does, and substitute those details into the original function.<p>Trying to find the conceptual boundaries that make the program easiest to reason about IMO is more of an art than a science and difficult to govern with hard and fast rules.
评论 #23743591 未加载
pierrebaialmost 5 years ago
Counter: Refactoring is far, far, far cheaper than duplication or wrong abstraction.<p>Duplication means you lose the wisdom that was gained when the abstraction was written. It means that any bug or weird cases will now only be fixed in one place and stay incorrect for all the places you duplicated the code.<p>About the rule of three: I personally extract functions for single-use cases all the time. The goal is to make the caller be as close to pseudo-code as possible. Then if a slightly different case comes up, I will write the slightly different case as another function right next to the original one. Otherwise, the fact that you have multiple similar cases will be lost.
评论 #23740197 未加载
评论 #23742938 未加载
评论 #23741811 未加载
seanalltogetheralmost 5 years ago
This quote from John Carmack speaks very succinctly to the problems that many abstractions in a code base can cause, and it&#x27;s a constant reminder for me when building out business logic.<p>&gt; &quot;A large fraction of the flaws in software development are due to programmers not fully understanding all the possible states their code may execute in.&quot;<p><a href="https:&#x2F;&#x2F;www.gamasutra.com&#x2F;view&#x2F;news&#x2F;169296&#x2F;Indepth_Functional_programming_in_C.php" rel="nofollow">https:&#x2F;&#x2F;www.gamasutra.com&#x2F;view&#x2F;news&#x2F;169296&#x2F;Indepth_Functiona...</a>
评论 #23739925 未加载
评论 #23739916 未加载
评论 #23739949 未加载
ragnesealmost 5 years ago
I can&#x27;t help but wonder if we&#x27;re sometimes using the wrong words for things. In this discussion we keep talking about &quot;code duplication&quot; and &quot;abstraction&quot; hand-in-hand, but I think they&#x27;re almost orthogonal concepts, at least as I think of them.<p>Seeing the same code almost copy-and-pasted in a few places might call for some code-deduplication. But that&#x27;s not necessarily a new &quot;abstraction&quot; in my eyes. It may be, but it also may not be.<p>I&#x27;m struggling to think of a specific example because I fully intended to go to bed before arriving here... But as a really stupid example, let&#x27;s say you have `val a = x + x + x` and `val b = y + y + y` and `val c = z + z + z` in your code. If you write a new function like `fun addThreeTimes(i) = i + i + i`, I don&#x27;t see that as a new abstraction at all. If, however, you invent multiplication, <i>now</i> you&#x27;re at a new abstraction! `val a = x * 3; val b = y * 3`, etc.<p>&quot;Abstraction&quot; to me is about thinking at a different semantic level, not about avoiding copy and paste.<p>Does this resonate with anyone else? Am I missing the point?
评论 #23744335 未加载
评论 #23744170 未加载
评论 #23744204 未加载
adrianmonkalmost 5 years ago
Two questions (genuine, not rhetorical):<p>(1) How much of this is because it&#x27;s <i>actually hard</i> to back out of the wrong abstraction and pivot to the correct one, and how much of it is other causes?<p>The article hints at this with, &quot;Programmer B feels honor-bound to retain the existing abstraction.&quot; Why do they feel this way, and is the feeling legitimate? Do they lack the deep understanding to make the change, or are they not rewarded for it, or are they unwilling to take ownership, or is it some other reason? I could see it going either way, but the point is to understand whether you&#x27;re really stuck with that abstraction or not.<p>(2) How much of the wrong abstraction is because people lack up front information to be able to know what the right abstraction is, and how much of it is because choosing good abstractions (in general and specifically ones that are resilient in the face of changing requirements) is a skill that takes work&#x2F;time&#x2F;experience&#x2F;etc. to develop?<p>If it&#x27;s due to being unable to predict the future, then it makes sense to avoid abstractions. If it&#x27;s due to not being as good as you could be at creating abstractions, then maybe improving your ability to do so would allow a third option: instead of choosing between duplication and a bad abstraction, maybe you can choose a good abstraction.
评论 #23742626 未加载
评论 #23740855 未加载
runaldalmost 5 years ago
For something that argues against bad abstractions, the article sure is lacking in concrete examples and makes a point in abstract. A lot of people will likely misinterpret or get the idea that abstraction is only done for duplicated code (DRY as some people would call it). I think the wrong&#x2F;bad &quot;abstractions&quot; here mostly refers to abstraction that was made over common code that is very specific in a context and is very susceptible to domain changes.<p>But there are a lot of other kinds of abstraction aside from DRY. There are abstractions made to reduce clutter and hide implementation detail and will likely be used only once. There are also abstraction that are more general and aren&#x27;t coupled to the domain. These abstractions are more reusable and composable, and are immune to domain changes such as the step 6 in the article. Some people would find these kinds of abstractions harder to digest, but I personally consider these kinds of abstractions as extensions to the standard library, or even additions to the vocabulary of the programming language.<p>Note that I don&#x27;t claim that general abstractions are necessarily better, since the generality can be made to the extreme and we&#x27;d have monads for breakfast.<p>All in all, I agree with the article, except that it is only referring to one kind of abstraction, although I hesitate to call it as such.
goto11almost 5 years ago
I&#x27;m skeptical because it is really easy to un-share code by copying it into multiple places but it is very hard to unify duplicated code. So I prefer to err on the side of sharing.<p>But yes, you should be ready to change sharing into duplication if you realize the code is just &quot;accidentally similar&quot; and need to evolve in separate directions.<p>In practice I have seen a lot more pain due to duplicate code compared to the issue of over-abstracting code, because the latter is much easier to fix.
评论 #23739739 未加载
评论 #23739752 未加载
评论 #23740339 未加载
ricksharpalmost 5 years ago
The mistake is creating an abstraction because of seeing duplication.<p>DRY is not a good guiding principle. It is an anti-principle.<p>Abstractions should only be created when they have a clear purpose and create a simpler architecture by encapsulating a single concern.<p>The reality is that all code is duplication. The reason we write code is because it is the most concise language to specify the intended goal <i>in the current context</i>.<p>What is unique is not the code that we are writing. The unique part is the code in the current context and each level of abstraction separates the context from the implementation - so that abstraction must be beneficial in organizing the overall solution into individual logical components of singular concern.
random3almost 5 years ago
This is so true, but so shallow too. I think the big mistake is to treat the code as &quot;the main thing&quot; when in reality it&#x27;s just a model (a golem) mimicking some &quot;other thing&quot;<p>We&#x27;re missing an entire set of code characterizations. Yes we have a &quot;pattern language&quot; but there&#x27;s not much to characterize it structurally wrt &quot;code distance&quot; from one part of the code to the other (e.g. in call stack depth as well as in breadth).<p>And again all of this needs to happen wrt the &quot;abstraction&quot; not the code itself. Having 10 methods 90% duplicated in a single file with 10% pecent difference is many times better than trying to abstract it.<p>Having the same &quot;unit conversion&quot; function duplicated in 3 parts of the code can be disastrous.<p>These two examples are very easy to see and understand, but in reality you&#x27;re always in a continuous state in between. And &quot;code smells&quot; like passing too many parameters or doing &quot;blast radius&quot; for certain code changes are only watching for side-effects of a missing &quot;code theory&quot;. An interesting book on the topic is &quot;Your code as a crime scene&quot;.<p>The bottom line is we&#x27;re trying to fix these problems over and over again without having a good understanding of what the real problem is and this leads to too many rules too easy to misinterpret unless you are already a &quot;senior artist&quot;
评论 #23740010 未加载
bob1029almost 5 years ago
This whole thing exists on a normalized&#x2F;de-normalized spectrum. The problem is that both ends have pros&#x2F;cons.<p>On the normalized side, you have the benefit of single-point-of-touch and enforcement of a standard implementation. This can make code maintenance easier if used in the correct places. It can make code maintenance a living nightmare if you try to normalize too many contexts into one method. If you find yourself 10 layers deep in a conditional statement trying to determine specific context, you may be better off with some degree of de-normalization (duplication).<p>On the de-normalized side, you have the benefit of specific, scoped implementations. Models and logic pertain more specifically to a particular domain or function. This can make reasoning with complex logic much easier as you are able to deal with specific business processes in isolation. You will likely see fewer conditionals in de-normalized codesites. Obvious downsides are that if you need to fix a bug with some piece of logic and 100 different features implement that separately, you can wind up with a nasty code maintenance session.<p>I find that a careful combination of both of these ideas results in the most ideal application. Stateless common code abstractions which cross-cut stateful, feature-specific code abstractions seems to be the Goldilocks for our most complicated software.
brandonmencalmost 5 years ago
Junior programmers duplicate everything.<p>Intermediate programmers try to abstract away absolutely every line that occurs more than once.<p>Expert programmers know when to abstract and when to just let it be and duplicate.
评论 #23740026 未加载
cjfdalmost 5 years ago
If there is one single article about programming that I hate it is this one. It is completely the wrong message. One should instead be very eager to eliminate duplication. To avoid the pitfalls that the article notes one should create abstractions that are the minimal ones required to remove the duplication to avoid over-engineering. Also one should keep improving the abstractions. That way one can turn the abstraction that turned out to be wrong into the right one. It is the attitude of constant improvement that will make one succeed as opposed to the attitude of fear of changing something that this article seems to encourage. When one does things one learns. When one is afraid to try things everything will just calcify until it is no longer possible to add any new features. What one does need to make the refactoring work is automated tests.
评论 #23741280 未加载
scrozartalmost 5 years ago
DRY gets abused regularly in my experience. It doesn&#x27;t stop at method&#x2F;class abstractions either; I&#x27;ve seen entire microservices &amp; plugins developed to ensure each app doesn&#x27;t have that one chunk of auth code, for instance, even though they each may have subtly different requirements (those extra params again). The logical end to this sort of thing is infinitely flexible&#x2F;generic multipurpose code, when the solution is really, probably increased specificity. DRY is probably the lowest-hanging fruit for practices&#x2F;patterns, and I think this leads to a disproportionate focus on it.
评论 #23740354 未加载
jack_halmost 5 years ago
I would say that if developers are hacking on an abstraction that is ill-suited to the task until the code base is a nightmare, they will take this advice and duplicate code until it&#x27;s a nightmare.<p>The fact of the matter is every line of code that is written has an associated cost. Developers all too often pay that cost by incurring technical debt.
haolezalmost 5 years ago
That&#x27;s mostly how I matured as a developer: I find myself abstracting less and writing less code today than I did 10 years ago, but I&#x27;m more productive today, my code is cheaper to maintain and has fewer bugs. Sometimes, I will literally copy paste a small amount of logic just to avoid making a future reader of this code to keep hunting around where the business logic is actually implemented. &quot;It&#x27;s right here, my dear future reader!&quot;.<p>Or maybe I was just a really bad programmer 10 years go :)
bcrosby95almost 5 years ago
I find it interesting that comments on these articles mainly discuss 1 aspect about it. But rarely this part:<p>&gt; Don&#x27;t get trapped by the sunk cost fallacy.<p>In my experience, yes, programmers are hesitant to throw out an abstraction. Why not work to change this, rather than telling people not to abstract?
评论 #23740479 未加载
gmalmost 5 years ago
This advice just _feels_ very wrong. After thinking about it and seeing the other comments, some remarks:<p>1) It&#x27;s fine to go back and duplicate code after you correct the abstraction. But it should be the _first_ phase in doing a larger pass to refactor code to fit the current business requirements. If you forgo the _second_ step, which should be to search for suitable abstractions again, you are absolutely guaranteed to be left with shit code that breaks in this situation, but not that other one, and no one knows why. I would absolutely only duplicate code as the prequel to deduplicating it again with updated abstractions.<p>2) If you do any of this without thorough unit tests you&#x27;re insane. Keep the wrongly-abstracted code unless you have time to thoroughly fix the mess you will have made when you duplicate code again and introduce bugs (you&#x27;re human, after all).<p>2a) If you are going to do this and there are no unit tests, create those unit tests before you touch the code initially (before the duplication).<p>3) Some of the comments saying you should wait until you implement something two or three times before creating an abstraction seem like comp sci 101 rules of thumb. It&#x27;s way too simplistic a rule, way too general. Prematurely abstracted (haha!). The type of project and the type of company&#x2F;industry will tell you what the right tradeoff is.<p>That is all.
评论 #23749745 未加载
评论 #23742725 未加载
preommralmost 5 years ago
I strongly dislike this article because the title is much broader than most of the substance of the article.<p>Advising not to overextend an abstraction is inarguable.<p>The actual title &quot;Duplication is far cheaper than the wrong abstraction&quot;, and the thing that people will really discuss, is a loaded statement that&#x27;s going to need a lot of caveats.
评论 #23740853 未加载
klyrsalmost 5 years ago
I use DRY in two ways. The first is that I&#x27;m happy to make 2 or 3 copies of a snippet before promoting that to a new function.<p>The second is when I find a bug in a duplicated snippet. I&#x27;ll mend the snippet and its duplicates, once or twice before promoting it to a function.<p>In the rarer (in my line of work) instance that a common snippet gets used with several intrusive variations, I usually document the pattern. It&#x27;s tempting to use templates, lambda functions, closures, coroutines, etc but far simpler to duplicate the code. But again, if a bug (or refactor) crops up and I need to fix it in many places, then I&#x27;ll spend some time thinking about abstraction and weigh the options with the benefit of hindsight.
crazygringoalmost 5 years ago
Another tip is: if you&#x27;re duplicating, and they&#x27;re not lines of code that are visually obviously next to each other, then leave a comment next to both instances mentioning the existence of the other.<p>There&#x27;s nothing inherently wrong with duplication, except that if you change or fix a bug in one, you need to not forget about the other. Creating a single function solves this... but at the potential cost of creating the wrong abstraction.<p>When you&#x27;re at only 1 or 2 extra instances of the code, just maintaining a &quot;pointer&quot; to the other case(s) with a comment serves the same purpose.<p>(Of course, this requires discipline to always include the comments, and to always follow them when making a change.)
评论 #23740507 未加载
gorgoileralmost 5 years ago
Brilliant insight. Always remember: (1) make it work, (2) make it right, (3) make it fast. 80% of projects get scrapped in between (1) and (2) because you end up realizing you wanted something completely different anyway.
评论 #23739736 未加载
评论 #23739835 未加载
thinkloopalmost 5 years ago
A related problem: duplication is not equality. If two things happen to be the same right now, it doesn&#x27;t mean they are intrinsically the same thing. If you have multiple products selling for $59.99 they shouldn&#x27;t share a function to generate the &quot;duplicate&quot; price. Abstractions needs to be driven by conceptual equivalence, not value equivalence, where duplication is a good hint for a potential candidate of abstraction, but not the complete answer alone.
SkyPuncheralmost 5 years ago
I think there&#x27;s a big cultural challenge with adopting duplication. It goes against most people&#x27;s career growth objectives.<p>Being able to effectively create clean, re-usable abstractions is a measure of being a &quot;senior&quot; engineer at many places. In other words, to be viewed as senior, you need to be able to effectively write abstraction frequently. It&#x27;s hard to measure an abstraction in the moment, so a lot of people assume that the senior simply knows better.<p>I find this extends to a lot of programming. Seniors will often use unnecessary tricks or paradigms simply because they can. It can make it extremely difficult for junior developers to grok code. Often this re-enforces seniority. &quot;If only the seniors can work on a section of code, then they are senior&quot;. Likewise, there are so many books on crazy architectures and patterns. It&#x27;s really neat to understand, but I&#x27;ve determined those books are pretty much self-serving.<p>----<p>I&#x27;ve found that my work is often far more limited by the domain&#x2F;business logic than any sort of programming logic. I&#x27;ll happily write code that looks really basic - because I know ANYBODY can come in and work with that code. If I write code that a junior needs to ask me questions like &quot;what is this pattern?&quot; or &quot;what does this mean?&quot;, I&#x27;ve written bad code.<p>-----<p>With all that being said, every single job interview I&#x27;ve ever had expects me to write code at the level of complexity that my title will be at. They&#x27;d much rather see me build some sort of abstract&#x2F;brittle concept than using some constants and switch statements. The prior looks cool, the latter looks normal.
评论 #23740706 未加载
评论 #23741332 未加载
allenualmost 5 years ago
In a large organization, the other thing you notice with trying to fix duplicated code is, if you take on refactoring it all, you are now responsible to make sure everything still works AND that you do not inhibit any future work. You are now responsible for more than you may have bargained for.<p>Coming up with the right abstraction takes some predicting of future use-cases. It&#x27;s more than just refactoring work to put it all in one place.
zarathustrealalmost 5 years ago
I’ve seen this “hot take” a few times before and even see developers that I would have considered very good agree with it. Consider that all code is computation, this is the point of a computer: to compute. Consider that abstraction doesn’t seem valuable -to you- for a multitude of reasons. Perhaps you’re using a flawed paradigm that emphasizes objects over computation. This would obviously mean abstraction -increases- the difficulty of reasoning about your code. Perhaps you don’t have a mental map of appropriate abstractions due to a lack of education or knowledge gap, this could lead you down the path of creating abstractions which reduce duplicate characters or lines of text but are not logically sound (“leaky abstractions.”) All of these things come together in a modern “enterprise” software environment in just the right way such that abstraction starts to seem like a bad idea. Do not fall into this line of thinking. Study functional programming. Study algebraic structures. Eventually the computer science will start to make sense.
hota_mazialmost 5 years ago
&gt; prefer duplication over the wrong abstraction<p>Such a strange advice.<p>If you&#x27;re able to recognize the wrong abstraction right away, surely you would not use it, right?
评论 #23740683 未加载
评论 #23740599 未加载
layer8almost 5 years ago
The main takeaway from the article is that abstractions which have become inadequate should be corrected (removed and&#x2F;or replaced by adequate ones) as soon as possible. A corollary is that abstractions should be designed such that they can be replaced or removed without too much difficulty. A common problem in legacy code bases is not just that they contain many inadequate abstractions, but that the abstractions are entangled with each other such that changing one requires changing a dozen others. You start pulling at one end and eventually realize that it’s all one large Gordian knot. One thing that I learned the hard way over the years is to design abstractions as loosely coupled and as independent from each other as possible. Then it becomes more practical to replace them when needed.
hackinthebochsalmost 5 years ago
I couldn&#x27;t disagree more. There is no such thing as abstracting too early (this does not go for structural abstractions like factories, singletons, etc). The best code is code you don&#x27;t have to read because of strong, well-named functional boundaries.
naringasalmost 5 years ago
sometimes it&#x27;s better to copy and paste some code only to make each copy diverge more and more over time (somewhat like a starting template) as opposed to introduction an abstraction to generalize some slightly different behaviors only to use said abstraction twice.<p>this makes even more sense when the code will live on in different programs<p>there&#x27;s a point when incurring the cognitive overhead costs of the abstraction become worthwhile, probably after the 3rd time. but my point is that it&#x27;s also important to consider that the abstraction introduces some coupling between the parts of the code.
评论 #23745855 未加载
memexyalmost 5 years ago
&gt; Re-introduce duplication by inlining the abstracted code back into every caller.<p>Ideally this type of workflow would be supported by the code editor. I&#x27;ve done this manually a few times and it&#x27;s not fun.
chiefalchemistalmost 5 years ago
Why not simply duplicate the abstraction, refactor as needed, and adjust the necessary caller(s)?<p>Having to know, find and maintain the individual duplications feels dirty and its own way wrong.<p>Choose your wrongs wisely?
kevsimalmost 5 years ago
Relevant post from earlier today <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=23735991" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=23735991</a>
ridajalmost 5 years ago
Previously discussed here: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17578714" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17578714</a>
评论 #23740368 未加载
jbmsfalmost 5 years ago
One of the reasons duplication is used badly is that it is one of the easiest abstractions to recognize.<p>One of the ways I&#x27;ve seen DRY go horribly wrong involves reusable code units evolving into shared dependencies that often interdepend in complex ways. Unfortunately, the problems of such a system are observed much later than the original code duplication and fewer people have the experience to see it coming.
adamklalmost 5 years ago
Sandi mentions this during a talk she gave on refactoring a few years ago. [0]<p>It’s a great little video for showing junior developers how a messy bit of code can be cleaned up with a few well chosen OOP patterns (and a set of unit tests to cover your ass).<p>[0] <a href="https:&#x2F;&#x2F;youtu.be&#x2F;8bZh5LMaSmE" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;8bZh5LMaSmE</a>
评论 #23740898 未加载
vxNsralmost 5 years ago
I want to thank everyone here, I’ve been stuck for about a week now on an issue that is entirely germane to this topic and the whole conversation here really helped me flesh out what was wrong and allowed me to understand a path forward. I’m honestly holding myself back from popping onto my computer right now to start working on it.
tarkin2almost 5 years ago
&quot;With C you can shoot your own foot. With C++ you can blow your own leg off&quot;. I feel the same is true here.<p>The abstraction may be right at the time of writing, yet further on it often becomes not only wrong, but a massive hindrance.<p>With time and effort, hacky code and be worked into shape. An eventual wrong abstraction normally means a rewrite.
kolinkoalmost 5 years ago
I wish this article was available two years ago when I tried to explain this to a bunch of juniors working for me...
评论 #23739740 未加载
nbardyalmost 5 years ago
This has been one of the hardest fought lessons I’ve learned it my programming career, but also one of the most fruitful. I am to make my abstractions too late rather than too early. My rule of thumb tends to me copy things six to seven times before you try to build an abstraction for it.
kureikainalmost 5 years ago
I think one of the cool thing about pattern matching or language(In my case, it&#x27;s Elixir) that support function operator is we can have same method with different argument sigunatures. So we don&#x27;t have to duplicate or inherit whatever and still share some common method.
worikalmost 5 years ago
Really this is stating the obvious.<p>The social problem at step 6, 7, and 8 is a social and economic one. Having the time, resources, and skill to do a job properly is very important. But there are social and economic pressures to &quot;just get it done&quot;.<p>This is a specific formulation of a general problem.
why-elalmost 5 years ago
Rob Pike discusses similar points in this section of his talk on Go Proverbs <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=PAAkCSZUG1c&amp;t=9m28s" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=PAAkCSZUG1c&amp;t=9m28s</a>.
Xlurkeralmost 5 years ago
I&#x27;d rather ctrl-f and change code in multiple places than deal with abstraction hell.
tomphooleryalmost 5 years ago
This again?? ;)<p>I love this post. A lot of wasted hours were spent in the past trying to use abstractions that no longer made sense, but Sandi encouraged me to go back and rethink a lot of that and now my code is way easier to read. Thanks Sandi!
recroadalmost 5 years ago
Programmer B in Step 6 should have used SOLID and refactored to extend the module (or something similar).<p>This is strawman argument which has little to do with the &quot;wrong&quot; abstraction and everything to do with poor design choices.
dfischeralmost 5 years ago
Reminds me of this discussion: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12120752" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12120752</a> (John Carmack on inlined code).
pps43almost 5 years ago
Related to <a href="http:&#x2F;&#x2F;yosefk.com&#x2F;blog&#x2F;redundancy-vs-dependencies-which-is-worse.html" rel="nofollow">http:&#x2F;&#x2F;yosefk.com&#x2F;blog&#x2F;redundancy-vs-dependencies-which-is-w...</a>
ninetaxalmost 5 years ago
What are people&#x27;s recommendations on books on how and when to create the right abstractions?<p>Last year I read Zach Tellman&#x27;s _Elements of Clojure_ and really loved the parts that touched on the subject of abstraction.
gumbyalmost 5 years ago
Early de duplication is the equivalent of early optimization: a bad idea that boxes you in.<p>Duplicate code is a sign that there <i>could</i> be a generalization missing.
kuharichalmost 5 years ago
Prior comments: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17578714" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17578714</a>
neetrainalmost 5 years ago
I think the term &quot;wrong&quot; causes all the misunderstandings.<p>It sounds like the abstraction was wrong <i>in the first place</i>.<p>Can it be called &quot;rotten&quot; abstraction?
avodonosovalmost 5 years ago
&gt; they alter the code to take a parameter, and then add logic to conditionally do the right thing based on the value of that parameter<p>But that&#x27;s a textbook example of bad code, competent coders don&#x27;t do this.<p>Update: for example see Thinking Forth chapter &quot;Factoring Techniques&quot;, around the tip &quot;Don’t pass control flags downward.&quot;. Page 174 in the onscreen PDF downloadable from sourceforge.<p>And there is no need for duplication. The bigger function can be split into several parts so that instead of one call with flag everyone calls needed set of smaller functions.
评论 #23742675 未加载
评论 #23742261 未加载
kristoalmost 5 years ago
There should be a code tool to re-inline code from an abstraction
djhaskin987almost 5 years ago
Mods this article is old, should be labeled 2016.
ulisesrmzrochealmost 5 years ago
“Premature optimization is the root of all evil”
ameliusalmost 5 years ago
A manager once asked me: please reuse as much code as you possibly can.<p>This reminded me of that.
sheeshkebabalmost 5 years ago
I’m not sure why this is #1... but since it is, both of these - duplication and wrong abstractions - are otherwise known as technical debt.
评论 #23739783 未加载