TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Hang your code out to DRY

76 点作者 hejsna超过 3 年前

11 条评论

ebiester超过 3 年前
My first heuristic is: If I change the code in block A, is it assured that I will need to change the code in block B?<p>My second heuristic is: Can I name the method I wish to de-duplicate in a way that is honest for all cases I wish to cover, yet explains its business purpose?<p>The more it deviates from these heuristics, the more likely I am to duplicate the code in object oriented programming.
评论 #29909657 未加载
评论 #29907811 未加载
评论 #29907285 未加载
recursivedoubts超过 3 年前
like many older developers, I have become less ideological about DRY over time, particularly as I have seen it motivate extremely abstract solutions that have proven difficult to understand and maintain<p>i have coined the term &quot;Locality of Behavior&quot; (LoB) as a competing design principle to DRY (as well as Separation of Concerns, SoC) that advocates more inlining of (potentially repetitive) logic in the interest of code maintenance and understandability:<p><a href="https:&#x2F;&#x2F;htmx.org&#x2F;essays&#x2F;locality-of-behaviour&#x2F;" rel="nofollow">https:&#x2F;&#x2F;htmx.org&#x2F;essays&#x2F;locality-of-behaviour&#x2F;</a><p>pic related:<p><a href="https:&#x2F;&#x2F;pbs.twimg.com&#x2F;media&#x2F;FAInxTuVkAATRrn?format=jpg&amp;name=medium" rel="nofollow">https:&#x2F;&#x2F;pbs.twimg.com&#x2F;media&#x2F;FAInxTuVkAATRrn?format=jpg&amp;name=...</a>
评论 #29906589 未加载
bcrosby95超过 3 年前
&gt; On re-reading Sandi’s original article it says kind of what I remember it saying, but it also… kinda doesn’t? There’s a lot more talk about programmers honoring the abstractions of elders who came before them<p>That&#x27;s because the original article is so clearly about tearing down bad abstractions, but a large majority of programmers - based upon discussion about the article - seem to never get past the first part of it.<p>Given a long enough time horizon, all abstractions turn bad. The solution isn&#x27;t to not abstract. The solution is to tear them down when they go bad. And if you don&#x27;t learn to tear down bad abstractions, your codebase will devolve into shit regardless of what you do.
评论 #29909562 未加载
评论 #29909436 未加载
ChrisMarshallNY超过 3 年前
I find it fascinating that people are so against inheritance&#x2F;polymorphism, these days.<p>That&#x27;s one of the absolute best ways to DRY. factoring out common base classes is a classic OO exercise. It&#x27;s possible to drastically reduce the size of a codebase, and the potential error exposure, by doing some simple extractions.
评论 #29907223 未加载
评论 #29907306 未加载
评论 #29907265 未加载
评论 #29907900 未加载
评论 #29907316 未加载
评论 #29907236 未加载
评论 #29907290 未加载
评论 #29909354 未加载
sfvisser超过 3 年前
Abstraction is inherently not about deduplication, it is about capturing intent and meaning. About the universality of certain concepts within your code base. Either based on your problem domain or in the context of your application architecture.<p>Once you abstract only to shorten your code you’ll likely regret it quickly.
cjfd超过 3 年前
If there is one single article about programming that I positively hate it is &#x27;duplication is better than the wrong abstraction&#x27;. As the Jason Swett article points out the article seems to install a sort of fear of refactoring. If there is a &#x27;wrong abstraction&#x27; nobody will every change it and now we are doomed to live with this wrong abstraction for all of eternity. The wrong abstraction can be turned into the right abstraction or can be undone if it is really not going anywhere. If that is what is happening at least people are trying to improve the code and if people try something it will eventually work. In many cases a bad code base is difficult to change because it is wrong in so many respects that it is difficult to tell where to start. If there is an attitude of refactoring and improvement things that are bad can be taken out quickly. Now, one should, of course not be stupid about removing duplication. If two functions just look vaguely similar but this is more of a coincidence than something that occurs because of the nature of the problem that these two functions are solving then they should absolutely not be one abstraction.... I suppose one might need to point that out to some developers but certainly not to ones who have been developers for some time and who actually have some talent as developers.
评论 #29908704 未加载
pastrami_panda超过 3 年前
I really like WET (write everything twice). It also fits nicely with the rule of 3.
评论 #29910790 未加载
评论 #29909064 未加载
评论 #29907165 未加载
评论 #29907922 未加载
manuel_w超过 3 年前
I once read a comment I wish I&#x27;d saved.<p>It goes along the lines of: W beats X, X beats Y, Y beats Z; in terms of what principle you&#x27;d like to apply to your code. One of these letters was essentially representing DRY. I summed things up pretty nicely. Does someone happens to remember?
评论 #29913962 未加载
0xbadcafebee超过 3 年前
Can&#x27;t we all just agree that none of these solutions are perfect and you might actually have to change what you do depending on the circumstance?
AnimalMuppet超过 3 年前
But DRYing your code can cause it to shrink...
评论 #29916435 未加载
ramoz超过 3 年前
Duplication can be better than integration.