bug-indent
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RFE: option to insert braces after single-statements for conditionals


From: Jarkko Hietaniemi
Subject: RFE: option to insert braces after single-statements for conditionals
Date: Sat, 10 May 2014 13:02:35 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.4.0

("One True Brace Style", OBTS, 1TBS)

That is, for the keywords 'if', 'else', 'while', 'do', and 'for', if there's only one statement, insert the braces.

Suggested option name: --braces-single-statement-conditionals, -bssc, or maybe -otbs

(for the truly maniacal, -nbssc would remove such braces... but I'm not asking for the removal, but the insertion)

This style makes the code less error-prone by always having an explicit block, which makes it easier to insert/delete lines at will from the block. For example these

if (foo) bar();
if (zzz) a=b; else c=d;
while (xyz) p++;
do re() while (mi);
for (...) j--;

would become these (assuming K&R with -i2 otherwise):

if (foo) {
  bar();
}
if (zzz) {
  a = b;
} else {
  c = d;
}
while (xyz) {
  p++;
}
do {
  re();
} while (mi);
for (...) {
  j--;
}

One tricky part I see is handling of trailing comments:

if (foo) /* blah */
  /* blefuscu */
  bar(); /* bleh */


should probably become

if (foo) { /* blah */
  /* blefuscu */
  bar(); /* bleh */
}

and an evil thing like

if (foo) /* evil */ bar();

should *probably* become

if (foo) { /* evil */
  bar();
}

That is, the braces are inserted immediately after the conditional and the single-statements, possibly before a trailing comment (they are *not* to be inserted as the last thing in the line...)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]