Java switch case:有没有大括号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/633497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java switch cases: with or without braces?
提问by cdmckay
Consider the following two snippets, with braces:
考虑以下两个带大括号的片段:
switch (var) {
case FOO: {
x = x + 1;
break;
}
case BAR: {
y = y + 1;
break;
}
}
Without braces:
没有大括号:
switch (var) {
case FOO:
x = x + 1;
break;
case BAR:
y = y + 1;
break;
}
I know that, in the snippet with braces, a new scope is created by enclosing each case in braces. However, if each case does not need the new scope (i.e. no variable names are being reused), is there any sort of performance penalty for using the braces with a case?
我知道,在带大括号的代码段中,通过将每个案例括在大括号中来创建一个新范围。但是,如果每个 case 都不需要新的作用域(即没有重用变量名),那么在 case 中使用大括号是否会有任何性能损失?
采纳答案by MrValdez
is there any sort of performance penalty for using the braces with a case?
使用带有案例的大括号是否有任何性能损失?
None.
没有任何。
The curly braces are there to help the compiler figure out the scope of a variable, condition, function declaration, etc. It doesn't affect the runtime performance once the code is compiled into an executable.
大括号用于帮助编译器确定变量、条件、函数声明等的范围。一旦代码被编译为可执行文件,它就不会影响运行时性能。
回答by Andy White
This question is probably going to be closed as "argumentative" (BRACE WAR!) but what the heck. I actually like the braces after the cases. To me it makes the ugly switch syntax look more like the rest of the language constructs. (There is no penalty for using braces in this "case")
这个问题可能会以“争论性”(BRACE WAR!)的形式结束,但到底是什么。我实际上喜欢病例后的括号。对我来说,它使丑陋的 switch 语法看起来更像语言结构的其余部分。(在这种“情况”中使用大括号没有惩罚)
回答by Miserable Variable
You say the braces can be omitted because no variables names are being reused. By reusing variable names, I assume you mean declaring a new variable of the same type.
您说可以省略大括号,因为没有重用变量名称。通过重用变量名,我假设您的意思是声明一个相同类型的新变量。
The braces are actually most useful to ensure you don't end up reusing the same variable in different case
s by mistake. They don't declare any variables today, but someone will add them tomorrow and without the braces the code is error-prone.
大括号实际上是最有用的,以确保您最终不会case
错误地在不同的s 中重用相同的变量。他们今天不声明任何变量,但明天有人会添加它们,如果没有大括号,代码很容易出错。
回答by TofuBeer
No performance penalty from an execution point of view.
从执行的角度来看,没有性能损失。
Slight performance penalty from a compiling point of view as there is more to parse, but if anyone were actually that concerned about it they would have to write their code all on one line :-)
从编译的角度来看,性能略有下降,因为要解析的内容更多,但如果有人真的那么关心它,他们将不得不在一行中编写代码:-)
And now for the opinion part of our post... I always put in the { and } because there is a penalty for maintainability in that you will likely have to put them in at a later date, and it can be a pain putting them in later... but that is 103% personal preference.
现在对于我们帖子的意见部分......我总是把 { 和 } 放在里面,因为可维护性会受到惩罚,因为你可能不得不在以后把它们放进去,把它们放进去可能会很痛苦稍后......但这是103%的个人偏好。
回答by jklp
With braces.
带牙套。
There are so many things that can go wrong with switch statements I try to avoid them where I can, i.e.
switch 语句有很多可能出错的地方,我尽量避免它们,即
- Forgetting breaks and thus having case fall-throughs
- Forgetting a default case and thus not catching an un-catered for condition
- Accidentally reusing variables between case statements, or worse yet, affecting a variable which IS sharing a variable between case statements.
- 忘记休息,从而导致案例失败
- 忘记默认情况,因此没有捕捉到未满足的条件
- 意外地在 case 语句之间重用变量,或者更糟的是,影响在 case 语句之间共享变量的变量。
Using braces is one way to prevent both intentional and accidental sharing of variables between case statements
使用大括号是防止在 case 语句之间有意和无意共享变量的一种方法
回答by Gareth Davis
I've never thought about it before. Never needed the braces in a case clause so can't really see why you would need them. Personally I don't go for the "It'll be easier to maintain" idea, that's just rubbish, it'll be easier to maintain if the code makes sense and is documented.
我以前从未想过。在 case 子句中从来不需要大括号,所以无法真正理解为什么需要它们。就我个人而言,我不赞成“维护起来会更容易”的想法,那只是垃圾,如果代码有意义并记录在案,维护起来会更容易。
No braces ... less syntax is more
没有大括号......语法越少越好
回答by starblue
I wouldn't use braces for switch cases.
我不会在 switch case 中使用大括号。
The switch statement looks baroque enough already without braces.
Switch cases should be very short. When you need to declare variables it is a sign that you are doing it wrong.
switch 语句在没有大括号的情况下看起来已经足够巴洛克了。
开关盒应该很短。当您需要声明变量时,这表明您做错了。
Now off to maintaining some legacy C code which sports switch cases of 500+ lines ...
现在开始维护一些传统的 C 代码,这些代码支持 500 多行的切换案例......
回答by Real Red.
As we know braces for switch cases are not necessary. Using braces cases may cause a confusion about the scope of a case.
正如我们所知,switch case 的大括号不是必需的。使用大括号案例可能会导致对案例范围的混淆。
An opening brace is generally associated with something meaningful like a start of a function or start of a loop or start of class declaration or start of array initialization etc...We know that, a case breaks out of switch block when it encounters a break statement. Thus using curly braces seems to imply idea of a different scope for case to an ignorant reader. So, its better to avoid using curly braces for the sake of better programming readability.
左大括号通常与一些有意义的东西相关联,例如函数的开始或循环的开始或类声明的开始或数组初始化的开始等……我们知道,当遇到中断时,case 会从 switch 块中跳出陈述。因此,对于无知的读者来说,使用花括号似乎暗示了区分大小写的不同范围的想法。因此,为了更好的编程可读性,最好避免使用花括号。
i.e. When I have something like,
即当我有类似的东西时,
switch(i)
{
case 1 :
{
//do something
}
System.out.println("Hello from 1");
case 2:
....
}
"Hello from 1" gets printed. But use of curly brace may suggest an ignorant reader that the case ends with '}', already knowing what curly braces generally signify in case of loops, methods etc.
“Hello from 1”被打印出来。但是大括号的使用可能会让无知的读者认为案例以 '}' 结尾,他们已经知道大括号在循环、方法等情况下通常表示什么。
Like we have jump-to-label statements in 'C' the control just shifts to case and continues it's execution. So, with that understanding it's just a BAD practice to use curly braces when writing cases for switch.
就像我们在“C”中有跳转到标签的语句一样,控制只是转移到大小写并继续执行。因此,有了这种理解,在为 switch 编写案例时使用花括号只是一种不好的做法。
Technically speaking you can surround any block of your code with an additional pair of curly braces when used with valid syntax. Using braces in switch looks so bad at least to me as it seems to give a different feel like I have said above.
从技术上讲,当与有效语法一起使用时,您可以用一对额外的花括号将代码的任何块括起来。至少对我来说,在 switch 中使用大括号看起来很糟糕,因为它似乎给人一种不同的感觉,就像我上面所说的那样。
My suggestion : Just avoid using surrounding braces for switch cases.
我的建议:只是避免在 switch case 中使用周围的大括号。