Java switch cases: with or without braces?

前端 未结 8 1786
猫巷女王i
猫巷女王i 2020-12-23 11:05

Consider the following two snippets, with braces:

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

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


        
8条回答
  •  既然无缘
    2020-12-23 11:29

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

提交回复
热议问题