[Nfd-dev] code-style: switch with compound statement

Junxiao Shi shijunxiao at email.arizona.edu
Wed Aug 12 16:27:02 PDT 2015


Dear folks

It seems that I misunderstood Wentao’s opinion about not to mix compound and non-compound ‘case’ labels.
It indeed meant that when a compound statement is used, the ‘break’ or ‘// Fallthrough’ should be inside the compound.
So:

// option F
switch (cond) {
  case 1:
  {
    int i = 1;
    doSomething(i);
    // Fallthrough
  }

  case 2:
    doSomething(2);
    break;

  case 3:
  {
    int j = 3;
    doSomething(j);
    break;
  }

  default:
    doSomething(0);
    break;
}

• When a ‘case’ or ‘default’ contains a declaration and therefore needs a compound statement, all statements under this label should be placed inside the compound statement, including ‘break’ or ‘// Fallthrough’.
• When any ‘case’ or ‘default’ label is using a compound statement, all ‘case’ and ‘default’ labels should be indented two spaces after ‘switch’, ‘{‘ and ‘}’ of the compound statements are indented at the same level as ‘case’ and ‘default’, and statements within the compound statements are indented two spaces after ‘case’ and ‘default’.

A ‘switch’ statement without compound statements may still be formatted in the current way, i.e. no extra indentation for ‘case’ and ‘default’.

However, if we allow a mix of compound and non-compound labels, we should also permit (but not require) 2-space indented ‘case’ and ‘default’ labels even if none of the labels contains a compound statement. Otherwise, the addition or removal of a label with compound statement would require indenting or unindenting the entire switch statement, which is unnecessary.

Yours, Junxiao


From: Wentao Shang
Sent: Wednesday, August 12, 2015 16:01
To: Junxiao Shi
Cc: ;Eric Newberry
Subject: Re: [Nfd-dev] code-style: switch with compound statement




On Wednesday, August 12, 2015, Junxiao Shi <shijunxiao at email.arizona.edu> wrote:
•         When any ‘case’ or ‘default’ contains a declaration and therefore needs a compound statement, all ‘case’s and ‘default’ must use compound statements.
I wouldn't go that far as to require all cases to have the same format. I think it's ok to mix blocked case and non-blocked case, as long as the block covers the entire single case branch. But the current specification (option E by Junxiao) also looks fine to me.
•         ‘break’ and ‘// Fallthrough’ should be placed inside compound statements.
I would suggest one more readability requirement: any intentional fall-through MUST be explicitly annotated in the code. (Do we have this rule already? I can't remember.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.lists.cs.ucla.edu/pipermail/nfd-dev/attachments/20150812/34b4b1f1/attachment.html>


More information about the Nfd-dev mailing list