Two comments:<div><br></div><div>1) In terms of indentation and placement of curly braces, I prefer option A or D but with extra indentation for statements inside the switch block. This should fix the issue of having two closing braces at the same level.</div><div><br></div><div>2) Mixing block and non-blocked statements under the same case clause seems confusing. I would suggest we mandate that if block is used inside the case clause, it should always cover the entire case clause.</div><div><br></div><div>Wentao</div><div><br>On Wednesday, August 12, 2015, Junxiao Shi <<a href="mailto:shijunxiao@email.arizona.edu">shijunxiao@email.arizona.edu</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US"><div><p>Dear folks</p><p><u></u> <u></u></p><p>rule 1.10 gives two forms for switch statements. The preferred form is:</p><p><u></u> <u></u></p><p style="margin-left:.5in"><span style="font-family:Consolas">switch (cond) {<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 1:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  stmts;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  // Fallthrough<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 2:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  stmts;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">default:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  stmts;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">}<u></u><u></u></span></p><p><u></u> <u></u></p><p>In reality, ‘stmts;’ may need to declare variables, but declarations are disallowed directly under a ‘case’ label.</p><p>The solution is to use a compound statement <span style="font-family:Consolas">{ stmts; }</span> , inside which declarations can be used.</p><p><u></u> <u></u></p><p>My question is, how to properly indent a switch statement in which some ‘case’ labels have compound statements?</p><p><u></u> <u></u></p><p>I prefer the following form:</p><p><u></u> <u></u></p><p style="margin-left:.5in"><span style="font-family:Consolas">// option A<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">switch (cond) {<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 1: {<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  int i = 1;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(i);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">} // Fallthrough<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 2:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(2);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 3: {<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  int j = 3;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(j);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">} break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">default:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(0);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">}<u></u><u></u></span></p><p><u></u> <u></u></p><p>I have also seen:</p><p><u></u> <u></u></p><p style="margin-left:.5in"><span style="font-family:Consolas">// option B<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">switch (cond) {<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 1:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  {<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">    int i = 1;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">    doSomething(i);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  }<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  // Fallthrough<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 2:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(2);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 3:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  {<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">    int j = 3;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">    doSomething(j);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  }<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">default:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(0);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">}<u></u><u></u></span></p><p><u></u> <u></u></p><p style="margin-left:.5in"><span style="font-family:Consolas">// option C<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">switch (cond) {<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 1:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  {<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">    int i = 1;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">    doSomething(i);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">    // Fallthrough<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  }<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 2:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(2);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 3:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  {<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">    int j = 3;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">    doSomething(j);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">    break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  }<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">default:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(0);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">}<u></u><u></u></span></p><p><u></u> <u></u></p><p style="margin-left:.5in"><span style="font-family:Consolas">// option D<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">switch (cond) {<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 1:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">{<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  int i = 1;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(i);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  // Fallthrough<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">}<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 2:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(2);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">case 2:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">{<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  int j = 3;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(j);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">}<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas"><u></u> <u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">default:<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  doSomething(0);<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">  break;<u></u><u></u></span></p><p style="margin-left:.5in"><span style="font-family:Consolas">}<u></u><u></u></span></p><p><u></u> <u></u></p><p>Option B exactly matches rule 1.10 because a compound statement, by definition, is a statement. However, it’s less readable than option A because the actual statements under a ‘case’ label with a compound statement is indented more than the statements under a ‘case’ label without a compound statement.</p><p>Option C has the same problem as option B, and also has hides ‘break’ or ‘// Fallthrough’ inside the compound statement making them less visible.</p><p>Option D looks okay but in case the last ‘case’ or ‘default’ label contains a compound statement, two ‘}’ would be indented at the same level at the bottom, which is confusing.</p><p><u></u> <u></u></p><p>What do others on the mailing list think about this?</p><p>I intend to amend rule 1.10 when a consensus is reached.</p><p><u></u> <u></u></p><p>Yours, Junxiao</p></div></div></blockquote></div><br><br>-- <br><div dir="ltr">PhD @ IRL, CSD, UCLA</div><br>