Wondering good practices on creating variable names

agolangf · · 524 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Sometimes I get lost when I make variable names, like I know what I want to do with them but sometimes if I have a lot of like variables it becomes difficult and ends up looking like crap. Just wondering what practices you guys follow when creating readable variable names. </p> <hr/>**评论:**<br/><br/>Fwippy: <pre><p>My guess is that the functions you&#39;re writing are too long. If you can break them up into smaller pieces, it&#39;ll be easier to track what variables you have in play at any given point.</p> <p>As for the names themselves, that&#39;s largely a matter of practice. If you can&#39;t think of a good short name and you don&#39;t use the variable often, it&#39;s fine to use a long one. Chances are you&#39;ll rename it later if it gets too cumbersome.</p></pre>nosmileface: <pre><p>I agree with opinion, however will add the following.</p> <p>It doesn&#39;t matter how you name your variables within functions, yes, there is a polite way to do it, for example use &#34;i&#34; for iterator variables in loops and &#34;n&#34; or &#34;num&#34; for number of things. But in general I prefer simple short names, often one letter. If you&#39;re having a hard time reading variables within a function, the problem is not with variable names, it&#39;s the function. It&#39;s an old fact that average human cannot process more than 7 things at the same time. If you have more than that, think about creating abstractions or breaking up the function into multiple pieces (in that order).</p> <p>However, as a reminder, abstractions always have complexity cost, prefer simplest possible abstractions. If you can group some of your variables into structure and think about it as a single element, that&#39;s the best way to do it. If that doesn&#39;t help, maybe you can break a function into multiple functions, this abstraction is harder to read and understand, but still rather simple. And only after than you can try hardcore OOP path with interfaces and encapsulation and methods and what not. Data design first, then function flow and only after that heavy OOP drugs.</p> <p>And a very good and old link is Rob Pike&#39;s article: <a href="http://doc.cat-v.org/bell_labs/pikestyle" rel="nofollow">http://doc.cat-v.org/bell_labs/pikestyle</a></p></pre>Gacnt: <pre><p>This helps but it&#39;s more like I go to make a variable outside a scope so I make like <code>username</code> then in the scope the retrieve the variable it becomes <code>dbUsername</code> then <code>username = dbUsername</code> or something like that </p></pre>apriendeau: <pre><p><a href="https://github.com/golang/go/wiki/CodeReviewComments#variable-names" rel="nofollow">https://github.com/golang/go/wiki/CodeReviewComments#variable-names</a> is the best, short resource that I have found.</p></pre>DualRearWheels: <pre><p>Give them female names. And use IDE with good refactoring so you can rename them easily once you are fed up &amp; realize your mistake.</p></pre>BoTuLoX: <pre><blockquote> <p>And use IDE with good refactoring so you can rename them easily once you are fed up &amp; realize your mistake.</p> </blockquote> <p>Go has gorename.</p></pre>

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

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