Java switch cases: with or without braces?

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

    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.

提交回复
热议问题