Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!rice!uw-beaver!uw-june!ka From: ka@cs.washington.edu (Kenneth Almquist) Newsgroups: comp.lang.c Subject: Re: C style peeve and knowing the rules Summary: keep expressions moderately simple Message-ID: <11251@june.cs.washington.edu> Date: 29 Mar 90 14:51:39 GMT References: <2205@osc.COM> <340018@hplvli.HP.COM> <19356@megaron.cs.arizona.edu> <894@dino.cs.iastate.edu> Organization: U of Washington, Computer Science, Seattle Lines: 53 hascall@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