Java 交换机用例: 带括号还是不带括号?

考虑以下两个带大括号的代码片段:

switch (var) {
case FOO: {
x = x + 1;
break;
}


case BAR: {
y = y + 1;
break;
}
}

没有牙套:

switch (var) {
case FOO:
x = x + 1;
break;


case BAR:
y = y + 1;
break;
}

我知道,在使用大括号的代码片段中,通过将每个大小写用大括号括起来,创建了一个新的作用域。但是,如果每个大小写都不需要新的作用域(也就是说没有变量名被重用) ,那么在大小写中使用大括号是否会带来性能损失?

56215 次浏览

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.

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")

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 cases by mistake. They don't declare any variables today, but someone will add them tomorrow and without the braces the code is error-prone.

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.

With braces.

There are so many things that can go wrong with switch statements I try to avoid them where I can, i.e.

  1. Forgetting breaks and thus having case fall-throughs
  2. Forgetting a default case and thus not catching an un-catered for condition
  3. Accidentally reusing variables between case statements, or worse yet, affecting a variable which IS sharing a variable between case statements.

Using braces is one way to prevent both intentional and accidental sharing of variables between case statements

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.

No braces ... less syntax is more

I wouldn't use braces for switch cases.

  • 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.

Now off to maintaining some legacy C code which sports switch cases of 500+ lines ...

As we know braces for switch cases are not necessary. Using braces cases may cause a confusion about the scope of a 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.

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.

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.

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.

My suggestion : Just avoid using surrounding braces for switch cases.