C style peeve and knowing the rules

Kenneth Almquist ka at cs.washington.edu
Fri Mar 30 00:51:39 AEST 1990


hascall at cs.iastate.edu (John Hascall) writes:
>    A few well-placed parentheses can go a long way toward promoting
>    readability and understanding, even if they are not strictly necessary.
>  
>    This mess has "the absolute minimum number of parentheses":
> 
>       a /= b ? c << 3 & g || f ? e || 2 + ++d & ~c : w : z;

This is the sort of expression that gives C a bad name.

> }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
>                 )
>            );

I don't find this version much more readable than the original.  Try
using "if" rather than "? :" and "||" for flow of control:

	if (b == 0)
	        a /= z;
	else if ((c << 3 & g) == 0 && f == 0)
	        a /= w;
	else if (e == 0) {
	        d++;
	        if ((d + 2 & ~c) == 0)
	                abort();
	}

I've taken the liberty of replacing the grotesque use of division by
zero with a call to abort.  This code still hasn't reached the point
where it belongs in a C program.  The variable names are poorly chosen,
and the bit operations are probably a consequence of a bad choice of
data representation.  And yes, the minimal parenthesis style I used
here could probably be improved by a few added parenthesis for the
benefit of people who don't remember the priority of the "&" operator.
				Kenneth Almquist

-- 
"The only light in the room comes from the green screen of my computer
 monitor and the small red lights on my modem.  I turn to check the
 clock:  3 a.m.  `Good,' I think.  `Three hours before I have to leave
 for school.  Too bad I didn't have time to do any homework'"
				- Bill Landreth



More information about the Comp.lang.c mailing list