<p>switch/case vs if/else mostly comes down to preference and project consistency, but i'd like to hear from the community concerning their personal preference. what are your rules for switch/case vs if/else?</p>
<hr/>**评论:**<br/><br/>daveddev: <pre><p>Generally, it is recommended to avoid the use of <code>else</code> (<a href="https://golang.org/doc/effective_go.html#if" rel="nofollow">https://golang.org/doc/effective_go.html#if</a>). First turn to control statements such as <code>return</code>, <code>break</code>, <code>continue</code>, etc. If "else" still makes more sense, prefer switch/case for readability (<a href="https://golang.org/doc/effective_go.html#switch" rel="nofollow">https://golang.org/doc/effective_go.html#switch</a>). Also, avoid deep nesting of conditionals (<a href="https://because.complexity.example" rel="nofollow">https://because.complexity.example</a>).</p></pre>natefinch: <pre><p>about the only time I use else is for switching on booleans and even then it feels a little dirty.</p>
<p>i.e.</p>
<pre><code>var val string
if hasFoo {
val = doWithFoo()
} else {
val = doWithoutFoo()
}
</code></pre>
<p>I basically never use <code>else if</code>, a switch is better.</p></pre>drvd: <pre><p>Well, with the exception of type switches which cannot be rewritten to if/else: It really depends on taste.</p></pre>earthboundkid: <pre><p>You can write a type switch as <code>if x, ok := v.(Type); ok</code>.</p>
<p>Switches are theoretical more efficient but in practice they break inlining, so you need to do a benchmark to know for sure which is faster. Just do whichever reads better 99% of the time. </p></pre>jechols: <pre><p>Only true if you're looking for a single type, not if you do a different action based on the type, as is common in some libraries which deal with things like databases, JSON, etc.</p></pre>earthboundkid: <pre><p>Yes, but then you'd just need a series of <code>else if</code> statements, the same as any time you try to convert a <code>switch</code> into an <code>if</code>. I agree that <code>switch</code> is a lot more readable if you have more than one type to change into, but nothing stops you from using <code>if</code>.</p></pre>jechols: <pre><p>Ah, fair point. It never occurred to me to do it that way :)</p></pre>SSoreil: <pre><p>I like reading switches a lot, the alignment is much clearer.
As soon as I have 3 clauses or more I normally prefer the switch.</p></pre>chewxy: <pre><p>My personal preference for switch/case is when I want to be very explicit AND exhaustive about all the cases. In my codebase there are only a handful of incidences where I have a switch without the default case.</p>
<p>If for me is mostly for optional stuffs (if some condition is met the state is changed). I try not to use elses.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传