C style peeve and knowing the rules

John Hascall hascall at cs.iastate.edu
Mon Mar 26 04:34:00 AEST 1990


robert at cs.arizona.edu (Robert J. Drabek) writes:
}I require my students to use the absolute minimum number of parenthesis
}until they have ALL the rules down pat.

   Operator precedence and associativity in C is quite complicated
   and somewhat non-intuitive.  A few well-placed parentheses can
   go a long way toward promoting readability and understanding,
   even if they are not strictly necessary.[1]
 
   This mess has "the absolute minimum number of parentheses":

      a /= b ? c << 3 & g || f ? e || 2 + ++d & ~c : w : z;

}Please, I know parenthesis are sometimes an aid in producing readable
}code and they are also clutter which can make for less-readable code.

   Extra parenthesization, even full parenthesization need not be
mutually exclusive with readability, that's what whitespace is for!

      a /= (b ? (
                  (((c << 3) & g) || f) ? (
                                            (e || ((2 + ++d) & ~c)) :
                                            w
                                          ) :
                  z
                )
           );

But then, I'm not much for dogma,
John Hascall (that's pronounced "throat warbler mangrove" :-)
hascall at atanasoff.cs.iastate.edu
----------
[1] "Parentheses are not necessary around the first expression of a
    conditional expression, since the precedence of ?: is very low, just
    above assignment.  They are advisable anyway, however, since they make
    the conditional part of the expression easier to see."  (K&R1 p. 48)



More information about the Comp.lang.c mailing list