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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Indirection Is Not Abstraction

163 点作者 yegor256a超过 6 年前

9 条评论

gregmac超过 6 年前
&gt; The users of this software would be very disappointed if any of these indirections involved adding any number besides 3.<p>What? No, it works great to add numbers beside 3:<p><pre><code> class Sample { public Sample(IThreeProvider threeProvider) { _threeProvider = threeProvider; } private readonly IThreeProvider _threeProvider; public int AddThree(int val) { return val + _threeProvider.Get().AsInt(); } } public class MyCustomThreeProvider() { public int Get() { return 4; } } </code></pre> Now you can just do:<p><pre><code> var sample = new Sample(new MyCustomThreeProvider()); sample.AddThree(2); </code></pre> And get 6, as you&#x27;d expect. ..So long as you read and fully understood the <i>entire</i> codebase first, anyway.<p>I think this actually highlights one of the bigger problems in adding unnecessary indirections: that it practically begs for strange work-around and hacks and strange layers added on top. It becomes too difficult to change the underlying classes -- eg, changing the AddThree method to handle an arbitrary number means also changing the entire concept of ThreeProviders and everywhere that calls or tests any of the related code -- and as a result it&#x27;s faster to workaround.
评论 #18356856 未加载
评论 #18357009 未加载
bjpbakker超过 6 年前
Of course the original post about Indirection vs Abstract was by Zed A. Shaw [0].<p>I strongly like to urge authors to give credit where credit is due. IMO this post should have at least mentioned the original article.<p>[0] - <a href="https:&#x2F;&#x2F;zedshaw.com&#x2F;archive&#x2F;indirection-is-not-abstraction&#x2F;" rel="nofollow">https:&#x2F;&#x2F;zedshaw.com&#x2F;archive&#x2F;indirection-is-not-abstraction&#x2F;</a>
评论 #18355230 未加载
评论 #18357178 未加载
taeric超过 6 年前
There is a great margin quote in Concrete Mathematics that overs this general idea, to me:<p><pre><code> There are two kinds of generalizations. One is cheap and the other is valuable. It is easy to generalize by diluting a little idea with a big terminology. It is much more difficult to prepare a refined and condensed extract from several good ingredients. </code></pre> Attributed to George Pòlya.
评论 #18359705 未加载
layer8超过 6 年前
The use of the word &quot;abstract&quot; in &quot;abstract class&quot; comes from the notion of abstract data types, introduced by Barbara Liskov and Stephen Zilles in 1974 in their paper &quot;Programming with abstract data types&quot; [0], which starts with a section &quot;The Meaninng of Abstraction&quot; (after the introduction). A simplified summary is that this is about having an interface that abstracts from implementation details:<p>&gt; We believe that the above concept captures the fundamental properties of abstract objects. When a programmer makes use of an abstract data object, he is concerned only with the behavior which that object exhibits but not with any details of how that behavior is achieved by means of an implementation.<p>It seems to me that the term &quot;abstract&quot; is justified for abstract classes whose role is to define an interface. (Note that pre-Java &quot;interface&quot; was not a language construct, i.e. the Java equivalent of &quot;interface&quot; is an abstract class in C++.) The article argues that &quot;abstract&quot; should only be used when &quot;details are left out&quot;. However, abstract classes leave out the implementation (or at least part of the implementation), so they inherently leave out the respective implementation details.<p>My point is, it is important to understand where the established terms come from, and what is meant by &quot;abstract&quot; in the context of abstract types [1].<p>[0] <a href="http:&#x2F;&#x2F;citeseerx.ist.psu.edu&#x2F;viewdoc&#x2F;download?doi=10.1.1.136.3043&amp;rep=rep1&amp;type=pdf" rel="nofollow">http:&#x2F;&#x2F;citeseerx.ist.psu.edu&#x2F;viewdoc&#x2F;download?doi=10.1.1.136...</a><p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Abstract_type" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Abstract_type</a>
adrianmonk超过 6 年前
&gt; <i>Languages such as C# and Java have done many a disservice with their keywords abstract and interface. There is a common misconception that by creating an interface one can make an object abstract and improve the design of a system.</i><p>This doesn&#x27;t make any sense. In Java, etc., &quot;abstract&quot; is just a technical term relating to the type system. It means these are types which do not have a complete implementation, so instances cannot be created. The word is used as an antonym of concrete, another technical term which describes types that can be instantiated.<p>It doesn&#x27;t mean any of this stuff about changing the abstraction level or improving the design of the system. The author seems not to understand that a word can have two different meanings even within a single subject area.<p>While I&#x27;m griping, if you are looking at the semi-arbitrary choice of programming language keywords and letting that shape the big picture of how you view software design, you&#x27;re going to have a bad time. &quot;It said &#x27;abstract&#x27; so this must be the way of creating abstractions.&quot; If that&#x27;s the type of reasoning you use, the programming language designers aren&#x27;t the ones doing you a disservice; you are.
评论 #18362299 未加载
ergothus超过 6 年前
&gt; When there are expected dimensions of software change, it can be worth paying the cost to create software with higher flexibility. Flexible software is harder to create, more complex, harder to understand, and harder to maintain.<p>I agree with what is being said, but the way it is coming across doesn&#x27;t work for me - I had to go back to the first sentence because the &quot;harder to maintain&quot; confused me: What is maintenance if not making changes? If so, flexible (less coupled) software is easier to maintain (over time). But the author&#x27;s central point feels correct: flexible software is requires more effort to think of writing in that way.<p>What definitions of &quot;maintenance&quot; and &quot;complex&quot; is the author using and how are they different from mine?
评论 #18359468 未加载
jxramos超过 6 年前
&gt; Things which are more concrete are what enables and empowers the abstractions – they make everything work. Without concrete elements, software is nothing more than a beautiful shell of uselessness.<p>That is very profound! I never thought about concrete realities as empowering the notion of an abstraction, its as if an abstraction is a compression of reality&#x2F;concepts.
yazaddaruvala超过 6 年前
More recently I&#x27;ve started to consider abstraction an optimization of code readability&#x2F;understandability.<p>Following the rules of optimization, it should only be done when absolutely needed.<p>1. Do not abstract.<p>2. Do not abstract.<p>3. Quantitative measurement of the benefit, then abstract.<p>This has worked out really well for me over the last 3 years.
评论 #18359157 未加载
infinity0超过 6 年前
Instead of developing Guice, Google should have just rewritten their entire codebase in Haskell, which actually has meaningful abstractions, as opposed to the non-abstract indirection crap that is &quot;dependency injection&quot;.<p>Fuck Guice.