<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're writing are too long. If you can break them up into smaller pieces, it'll be easier to track what variables you have in play at any given point.</p>
<p>As for the names themselves, that's largely a matter of practice. If you can't think of a good short name and you don't use the variable often, it's fine to use a long one. Chances are you'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't matter how you name your variables within functions, yes, there is a polite way to do it, for example use "i" for iterator variables in loops and "n" or "num" for number of things. But in general I prefer simple short names, often one letter. If you're having a hard time reading variables within a function, the problem is not with variable names, it's the function. It'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's the best way to do it. If that doesn'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'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'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 & 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 & realize your mistake.</p>
</blockquote>
<p>Go has gorename.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传