<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:SimSun;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
@font-face
        {font-family:"\@SimSun";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
p
        {mso-style-priority:99;
        margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style></head><body lang=EN-US><div class=WordSection1><p>Dear folks</p><p><o:p> </o:p></p><p>rule 1.10 gives two forms for switch statements. The preferred form is:</p><p><o:p> </o:p></p><p style='margin-left:.5in'><span style='font-family:Consolas'>switch (cond) {<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 1:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  stmts;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  // Fallthrough<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 2:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  stmts;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>default:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  stmts;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>}<o:p></o:p></span></p><p><o:p> </o:p></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><o:p> </o:p></p><p>My question is, how to properly indent a switch statement in which some ‘case’ labels have compound statements?</p><p><o:p> </o:p></p><p>I prefer the following form:</p><p><o:p> </o:p></p><p style='margin-left:.5in'><span style='font-family:Consolas'>// option A<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>switch (cond) {<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 1: {<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  int i = 1;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(i);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>} // Fallthrough<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 2:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(2);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 3: {<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  int j = 3;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(j);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>} break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>default:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(0);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>}<o:p></o:p></span></p><p><o:p> </o:p></p><p>I have also seen:</p><p><o:p> </o:p></p><p style='margin-left:.5in'><span style='font-family:Consolas'>// option B<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>switch (cond) {<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 1:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  {<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>    int i = 1;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>    doSomething(i);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  }<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  // Fallthrough<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 2:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(2);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 3:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  {<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>    int j = 3;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>    doSomething(j);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  }<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>default:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(0);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>}<o:p></o:p></span></p><p><o:p> </o:p></p><p style='margin-left:.5in'><span style='font-family:Consolas'>// option C<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>switch (cond) {<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 1:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  {<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>    int i = 1;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>    doSomething(i);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>    // Fallthrough<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  }<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 2:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(2);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 3:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  {<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>    int j = 3;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>    doSomething(j);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>    break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  }<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>default:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(0);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>}<o:p></o:p></span></p><p><o:p> </o:p></p><p style='margin-left:.5in'><span style='font-family:Consolas'>// option D<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>switch (cond) {<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 1:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>{<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  int i = 1;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(i);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  // Fallthrough<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>}<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 2:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(2);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>case 2:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>{<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  int j = 3;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(j);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>}<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'><o:p> </o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>default:<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  doSomething(0);<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>  break;<o:p></o:p></span></p><p style='margin-left:.5in'><span style='font-family:Consolas'>}<o:p></o:p></span></p><p><o:p> </o:p></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><o:p> </o:p></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><o:p> </o:p></p><p>Yours, Junxiao</p></div></body></html>