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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

SOLID Go Design

161 点作者 davecheney将近 9 年前

6 条评论

skywhopper将近 9 年前
Definitely good stuff to think about in the piece, but even as a Go fan, I found the rhetoric to be insufferable. &quot;Go obviously doesn’t have classes—instead we have the far more powerful notion of composition&quot;. Go&#x27;s syntax may frame things differently than Java or Ruby or C++, and I don&#x27;t discount the impact of syntax and abstractions on code design, _but_ just because you don&#x27;t <i>call</i> them &quot;classes&quot; doesn&#x27;t mean they are something entirely different.<p>Go types can have public and private fields, a set of associated functions which can also be public or private, and ways to extend other structs or pose as other types. So you&#x27;ve got encapsulation, inheritance, and polymorphism. Pretending you don&#x27;t have &quot;classes&quot; is just silly.
评论 #12344759 未加载
tunesmith将近 9 年前
For middle of the road companies with middle of the road developers, I find SOLID is not really a great set of principles to focus on.<p>&#x27;S&#x27; is pretty good because it helps encourage developers to get away from huge classes that have dozens of methods.<p>&#x27;O&#x27; is basically useless when applied to a single codebase, because one of the worst parts of middle-of-the-road developers is when they start to realize they can extend classes. You end up with horrendously complicated and deep class hierarchies. I find that when it&#x27;s time to add functionality, it&#x27;s far better to just refactor and create new classes for composition.<p>&#x27;L&#x27; is deeply confusing, and the difficulty in understanding it is what leads to the deep and brittle class structures above.<p>&#x27;I&#x27; drives intermediate developers to create more and more interfaces that have 1-2 methods, which when combined with their difficulties with &#x27;O&#x27; and &#x27;L&#x27;, just leads to confusion and high cognitive load when trying to maintain the codebase.<p>&#x27;D&#x27; is good.
评论 #12341626 未加载
评论 #12342583 未加载
评论 #12341618 未加载
评论 #12344781 未加载
评论 #12357924 未加载
评论 #12344095 未加载
kasey_junk将近 9 年前
I am fairly consistent in my criticism of Go. I am also fairly consistent in my insistence that the Dependency Inversion Principle is one of the harder concepts to wrap your head around of the SOLID principles (not least because several other concepts use terms similar).<p>But golang&#x27;s structural typing actually allows for some of the easiest demonstrations of the implementation of the DIP of any of the strongly typed languages I&#x27;ve used.<p>I find Martin&#x27;s definition of the DIP counterintuitive. A description that makes more sense to me is &quot;interfaces should be defined by the &#x27;users&#x27; of the interface, not by the implementers&quot;.<p>With a language like C++ or Java, to achieve this requires either lots of adapter layers or compiler tricks. With golang it is simple.<p>As a for instance, let&#x27;s say I&#x27;m writing a restful client library for a specific application. It is tempting to to hard code a dependency on the std lib http.Client.Do method. If instead with 3 lines of code, I define my own interface for Do that matches the std library definition. I can use the std library, or any other library that matches it (one that is faster say or implements back off).<p>That is one of the few areas where the golang type system is pretty cool in comparison to its peers.
评论 #12342043 未加载
评论 #12342495 未加载
praptak将近 9 年前
There&#x27;s something strange about application of the Liskov Substitution Principle in the article.<p>Original formulation of LSP was: subtypes should not break supertypes, i.e. caller expecting supertype should not be able to tell the difference when getting an instance of subtype. That was expressed in terms of an explicit subtype&#x2F;supertype relation, like inheritance in OOP. When there&#x27;s no such thing in the language, the LSP becomes (quote article):<p>&quot;Two types are substitutable if they exhibit behaviour such that the caller is unable to tell the difference.&quot;<p>This sounds almost like a tautology. I understand that it isn&#x27;t, since &quot;substitutable&quot; is &quot;you substitute class X for interface Y in your code&quot; and &quot;observably same behaviour&quot; is &quot;X behaves like all implementors of Y are expected to&quot;.<p>In practice, X doesn&#x27;t declare it implements Y. If I review a change to X I might miss that fact. If that change breaks behavior expected from Y, it&#x27;s bad.<p>I&#x27;m not sure if this scenario is a real problem, I have written like 100 lines in Go.
评论 #12342704 未加载
akkartik将近 9 年前
<i>eyeroll</i><p>I wonder how Robert Martin was implementing SOLID before the wonder that is Go was delivered unto us. And I wonder why all our buzzword-laden codebases are still knee deep in crap if SOLID is so amazingly awesome. We&#x27;ve been hearing about coupling and cohesion for over 50 years now. At what point does thinking in those terms become part of the problem rather than the solution?<p>Is it possible that Go or any other language <i>du jour</i> just seems cleaner because it&#x27;s <i>newer</i> and so the cruft hasn&#x27;t had a chance to form yet? Come back in 10 years, and tell me how amazing it is then.
评论 #12341315 未加载
评论 #12342228 未加载
评论 #12341397 未加载
quickben将近 9 年前
What&#x27;s with all the Go pushing lately?
评论 #12341189 未加载
评论 #12342581 未加载
评论 #12341172 未加载
评论 #12342236 未加载