[DTrace-devel] [PATCH v2] Refactoring for greater stylistic consistency

Kris Van Hees kris.van.hees at oracle.com
Fri Jan 8 16:40:30 PST 2021


On Fri, Jan 08, 2021 at 11:10:12AM -0800, Eugene Loh wrote:
> On 01/08/2021 06:26 AM, Kris Van Hees wrote:
> 
>     On Wed, Jan 06, 2021 at 03:30:40PM -0500, eugene.loh at oracle.com wrote:
> 
>         diff --git a/CODING-STYLE b/CODING-STYLE
>         +Braces should typically be avoided on single-statement branches.  E.g.,
>         +       if (foo1) {
>         +               bar1();
>         +               bar2();
>         +       } else
>         +               bar3();
> 
>     Braces should be avoided on single-statement branches, unless one of the
>     branches (other than the else-branch) contains multiple statements.
> 
>     GOOD:
>             if (foo1) {
>                     bar1();
>             } else if (foo2) {
>                     bar2();
>                     bar3();
>             } else
>                     bar4();
> 
> 
> It seems to me that the rule and the example conflict.  Here, one of the
> branches other than the else branch contains multiple statements.  Therefore,
> the "unless" condition is triggered.  Therefore, the "avoid braces" practice
> should not be used, not even on the "else bar4()" branch.  Complicated.

I am giving a rough description of the style and examples.  My wording was
not meant to be an exact phrasing of a rule (which you are much better at).

> And, do "else" branches include "else if" branches?
> 
> The rule that I think is being illustrated seems hard to state and not present
> in any style guide I managed to find.  Do you mean:
> 
>         When a simple-else branch has only a single, simple statement,
>         its braces should be omitted.  Further, when the other branches
>         are all, each, single, simple statements, their braces should also
>         be omitted.
> 
> Or, how about employing a simpler rule, like the one in the Linux kernel?  That
> is, omit the "other than the else-branch" exception.
> 
> 
>     BAD:
>             if (foo1)
>                     bar1();
>             else if (foo2) {
>                     bar2();
>                     bar3();
>             } else
>                     bar4();

I find the mix of 'else if' and '} else' less clean and (when the statement
block is quite large) less clear.  By having the left hand side of the line
be consistently either 'else ...' or '} else' I find it easier to determine
the delineation of the statement blocks.

I don't see where it is complicated as a rule...  if (and any else-if in the
same conditional statement) either all use braces or not, i.e. if one of them
needs braces (because it contains multiple statements) they all need braces.
A final else only uses braces if necessary (multiple statements).



More information about the DTrace-devel mailing list