‘打破’在 switch-case 中使用花括号时的语句
我在 C/Objective-C/C++ 中的所有 switch case 语句中使用大括号
,直到几分钟前,我才考虑在大括号内包含 break;
语句是好是坏实践。我怀疑这并不重要,但我认为仍然值得一问。
switch (foo) {
case 1: {
// stuff
break;
}
default: {
break;
}
}
与
switch (foo) {
case 1: {
// stuff
} break;
default: {
// stuff
} break;
}
I use curly braces with all of my switch case statements in C/Objective-C/C++
I had not, until a few moments ago, considered whether including the break;
statement inside the braces was good or bad practice. I suspect that it doesn't matter, but I figure it is still worth asking.
switch (foo) {
case 1: {
// stuff
break;
}
default: {
break;
}
}
vs
switch (foo) {
case 1: {
// stuff
} break;
default: {
// stuff
} break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简短的回答:没关系。
Short answer: it doesn't matter.
只需给出一个稍微更详细的答案...
官方 C99 规范对 Break 语句进行了以下说明:
所以这真的没关系。至于我,我把中断放在花括号内。由于您还可以在大括号内的其他位置进行中断,因此在大括号内也进行结束中断会更合乎逻辑。有点像退货声明。
Just a give a slightly more detailed answer...
The official C99 specification says the following about the break statement:
So it really doesn't matter. As for me, I put the break inside the curly braces. Since you can also have breaks in other places inside your curly braces, it's more logical to also have the ending break inside the braces. Kind of like the return statement.
关于如何组合花括号和开关,有大量不同的编码风格。我将在示例中使用我喜欢的那个。
break
语句会跳出最内层的循环或 switch 语句,无论位置如何。例如,您可以对单个案例进行多次中断:请注意,您也可以将案例放在任何地方,尽管这在某种程度上被认为是不好的做法。 case 标签非常类似于 goto 标签。碰巧我写了这样的东西:
但是要小心使用它。
There's tons of different coding styles of how to combine curly braces and switches. I'll use the one I prefer in the examples. The
break
statement breaks out of the innermost loop or switch statement, regardless of location. You could for example have multiple breaks for a single case:Note that you can also put the cases anywhere, though that is somewhat considered bad practice. The case label is very much like a goto label. It has happened that I've written something like this:
but use it with care.
您可能一开始就不需要花括号,除非您需要它们用于词法范围。第一个例子对我来说看起来更好,但我认为真正的答案是这是一个品味问题。
You probably don't want the curlies in the first place unless you need them for lexical scope. The first example looks better to me, but I suppose the real answer is that it's a matter of taste.
正如明确指出的,这只是个人风格的问题,但我总是将break语句放在大括号外面:在我看来,将break放在右大括号之前会跳出复合语句,从而稍微增加意大利面条代码的感觉。
编辑:我在其他评论中意识到将其放在大括号内是一个好点:就像 if 条件后面的函数中间的 return 语句一样。然而,为了强化我的最初观点,我曾经研究过一个具有严格编码规则的航空电子系统,即在函数的最后只有一个返回,并具有控制返回值或执行流程的标志。
As clearly stated, this is just a matter of personal style, but I always put the break statement outside the braces: putting the break before the closing brace seems to me to jump out a compound statement thus slightly increasing the spaghetti code feeling.
EDIT: I realize in other comment a good point to have it inside the braces: just like a return statement in the middle of a funtion after an if condition. However, to enforce my initial point, I once worked on an avionic system with strict coding rules, as to have just one return at the very end of the function, and have flags controlling the return value or the execution flow.