Java switch cases: with or without braces?

前端 未结 8 1830
猫巷女王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:37

    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

提交回复
热议问题