Is there a reason not to use the same name for the struct point in all methods?

xuanbao · · 526 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>In packages/libraries the pointer name to the receiving struct is always different in each package. </p> <p>Example:</p> <pre><code>type data struct { data interface{} } func (d *data)SomeThing(newDate interface{}) { d.data = newData } type Animal struct { name string } func (a *Animal)SetName(newName string) { a.name = newName } </code></pre> <p>In this example d is for *data and a for *Animal. Why not always use the same name, for example this, as pointer to the receiving struct?</p> <pre><code>type data struct { data interface{} } func (this *data)SomeThing(newDate interface{}) { this.data = newData } type Animal struct { name string } func (this *Animal)SetName(name string) { this.name = name } </code></pre> <p>In a team and/or big software project, this would make things a lot more easier and simpler to understand.</p> <p>Or is there any rule or reason not to do this? I can&#39;t find any stuff about that in Effective Go or somewhere else.</p> <hr/>**评论:**<br/><br/>jere_jones: <pre><p>The name of a method&#39;s receiver should be a reflection of its identity; often a one or two letter abbreviation of its type suffices (such as &#34;c&#34; or &#34;cl&#34; for &#34;Client&#34;). Don&#39;t use generic names such as &#34;me&#34;, &#34;this&#34; or &#34;self&#34;, identifiers typical of object-oriented languages that place more emphasis on methods as opposed to functions. The name need not be as descriptive as that of a method argument, as its role is obvious and serves no documentary purpose. It can be very short as it will appear on almost every line of every method of the type; familiarity admits brevity. Be consistent, too: if you call the receiver &#34;c&#34; in one method, don&#39;t call it &#34;cl&#34; in another.</p> <p>Source: <a href="https://github.com/golang/go/wiki/CodeReviewComments#receiver-names" rel="nofollow">https://github.com/golang/go/wiki/CodeReviewComments#receiver-names</a></p> <p>However, that just said &#34;don&#39;t do it&#34; without a good reason. Having come from C++, using &#34;this&#34; seemed the most natural until I read this: <a href="https://blog.heroku.com/neither-self-nor-this-receivers-in-go" rel="nofollow">https://blog.heroku.com/neither-self-nor-this-receivers-in-go</a> I recommend giving it a read.</p></pre>Freakezoid: <pre><p>Thank you! The heroku post is a very good read and totally makes sense.</p></pre>dchapes: <pre><p>You might as well ask why not always call your function arguments <code>arg1</code>, <code>arg2</code>, etc? A method receiver is just a different way of writing the first argument (e.g. <code>func (a *Animal) Foo(x int)</code> is basically just <code>func Foo(a *Animal, x int)</code>) (<a href="https://play.golang.org/p/m4CaFyEfbn" rel="nofollow">example</a>).</p> <p>Using <code>this</code> is not descriptive and bad; when you use a language that doesn&#39;t force you to use such useless identifiers you should be more descriptive. Of course given how such identifiers are used that <a href="http://research.swtch.com/names" rel="nofollow">doesn&#39;t mean it needs to be long to be more useful than <code>this</code></a>.</p> <blockquote> <p>I can&#39;t find any stuff about that in Effective Go or somewhere else.</p> </blockquote> <p><a href="https://github.com/golang/go/wiki/CodeReviewComments#receiver-names" rel="nofollow">https://github.com/golang/go/wiki/CodeReviewComments#receiver-names</a></p></pre>weberc2: <pre><p>Honestly I don&#39;t think it matters either way. Go chose this convention; Python, Java, etc, chose another. Both seem to work fine. I can&#39;t perceive any harm or benefit in either direction.</p></pre>barsonme: <pre><p>Except in other languages &#39;this&#39; is different from Go&#39;s receivers. Go&#39;s receivers are syntactical sugar for the first argument; that is, </p> <p><code>func (t T) Foo(t2 T2)</code></p> <p>is essentially the same as:</p> <p><code>func Foo(t T, t2 T2)</code></p> <p>If you wrote this Java method:</p> <p><code>public void Foo(T this)</code></p> <p>people reviewing your code would take issue since &#39;this&#39; is a completely nondescript name.</p> <p>Plus, like I sort of mentioned above, calling the receiver &#39;this&#39; implies that it&#39;s the same as &#39;this&#39; in other languages---it&#39;s not. In JavaScript, for example, &#39;this&#39; defaults to the global object (Go has no such thing); is undefined or the global object during a function call with and without strict mode, respectively; or, if it&#39;s an object method, it refers to the object the method is called on.</p> <p>This is distinctly different from Go.</p> <p>Write Go in Go. Don&#39;t try to write Java or JavaScript or Python.</p></pre>weberc2: <pre><ol> <li>I agree that <code>this</code> and <code>self</code> in other languages are different from Go&#39;s receivers.</li> <li>This doesn&#39;t change my mind that there would be no significant consequence had the Go developers used <code>this</code> or <code>self</code> as the convention instead.</li> </ol></pre>johnbr: <pre><p>I always use this, haven&#39;t had a problem yet and I&#39;ve written thousands of lines of go code with that idiom. Very curious if there&#39;s a subtle problem I&#39;ve missed.</p></pre>xiegeo: <pre><p>You don&#39;t know what you are missing. </p> <ul> <li>You can search for a.foo or b.foo, where in a large code base there can be many more this.foo. Especially in Go where a method can be in any file. </li> <li>Follow conventions mean it is easier for others to read your code and you to read others code. Please follow conventions, if you ever collaborate with others.</li> <li>this is a keyword in other languages that does object oriented programing differently from Go. It act as a trigger word for me to change my programing model.</li> </ul> <p>You should remove all this from your .go files now, and train yourself a habit of writing idiomatic Go code. Otherwise you won&#39;t get the benefit of the above.</p></pre>natefinch: <pre><p>Don&#39;t use this or self or anything similar. It&#39;s uninformative. </p> <pre><code>func Speak(a Animal) </code></pre> <p>Is the same as </p> <pre><code>func (a Animal) Speak() </code></pre> <p>Literally the same, the latter is just syntactic sugar.</p> <p>I hope you&#39;d never write </p> <pre><code>func Speak(this Animal) </code></pre> <p>So please don&#39;t do this:</p> <pre><code>func (this Animal) Speak() </code></pre> <p>Also, it marks the code as unidiomatic, and raises red flags for people who have been coding in go for a long time. If the author ignored such an easy convention, what other conventions are being ignored?</p></pre>mdwhatcott: <pre><p>The name &#34;a&#34; isn&#39;t any more informative than &#34;this&#34;. They send slightly different messages depending on your perspective.</p></pre>natefinch: <pre><p>It&#39;s slightly more informative, especially if you have multiple types with methods in the same file. Again, I would ask - would you name a function parameter this? If not, then don&#39;t name a receiver this. </p></pre>whizack: <pre><p>i would argue that you should still treat receiver variables like parameter names. if your convention is to name parameters long form names (animal *Animal), you should do so, if it&#39;s to use shorthand (a *Animal), then do so. </p> <p>The idea is to treat them like parameters, so that when you refactor code, the locality of the type it is attached to doesn&#39;t matter. self.Speak(), for example, is specific to code attached to a struct that has a Speak method, not an animal with a Speak method</p></pre>weberc2: <pre><p>You&#39;re not missing anything, you&#39;re just deviating from the convention. People in this thread are getting religious about it and downvoting you (here, have an upvote). It&#39;s not a big deal, it&#39;s just a little annoying to look at for those of us who have acclimated to the Go convention. If you want your code to be a little more reader-friendly to Go programmers, avoid using <code>this</code> or <code>self</code>, but contrary to the impression given by other commenters it&#39;s not going to miraculously improve your productivity or make your algorithm O(1) or something.</p></pre>mdwhatcott: <pre><p>If using &#39;this&#39; makes sense for you and your team, then use &#39;this&#39;!</p></pre>mdwhatcott: <pre><p>No, there isn&#39;t any good reason not to use &#39;this&#39;, &#39;self&#39; or other generic names. The only reason we are ever given is that it&#39;s simply not &#39;idiomatic&#39; in go. Doing something just because it&#39;s been labeled idiomatic is idiotic. If it makes sense for your team to use &#39;this&#39;, then use it! I work at SmartyStreets (a predominantly Go shop for several years) where we use nothing but &#39;this&#39; for receiver names unless there&#39;s really compelling reason to do otherwise. We&#39;ve never felt hindered or regretted the decision. <a href="https://github.com/smartystreets" rel="nofollow">https://github.com/smartystreets</a></p></pre>jonbonazza: <pre><p>I&#39;d argue that not being idiomatic is perfectly valid reason NOT to do something. Writing idiomatic code promotes consistency across code bases. This makes it easier for new developers to come up to speed on your projects and when developers leave your company, they will have an easier time getting up to speed on the code bases of their new gig. Code conventions are a real thing and should be adhered to as strictly as possible. While not necessarily enforced like standards, they still have very valid purposes and should not be scoffed at. I&#39;d say your company is doing the entire Go community a disservice by not adhering to these conventions and I, for one, wouldn&#39;t be very happy working somewhere that doesn&#39;t understand this.</p></pre>natefinch: <pre><p>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

526 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传