A question of style

Andrew P. Mullhaupt amull at Morgan.COM
Sun Dec 3 18:37:50 AEST 1989


In article <427 at jhereg.Minnetech.MN.ORG>, mark at jhereg.Minnetech.MN.ORG (Mark H. Colburn) writes:
> In article <1989Nov30.001947.14883 at aqdata.uucp> sullivan at aqdata.uucp (Michael T. Sullivan) writes:
> >From article <547 at mars.Morgan.COM>, by amull at Morgan.COM (Andrew P. Mullhaupt):
> >> 
> >> ...I can't think of what I would call a 'proper' use of the comma operator...
- Just for the record, I said this (A.P.Mullhaupt)
> >
> >I posted this instead of mailing it to see what, if any, reaction it got.
> >I'm always interested in coding style.
> 
> I agree with Henry.  There are few "proper" uses of a comma operator.
Who's Henry? I'm not sure if you mean me or someone who agrees with
me also in this thread. Worse yet, Henry might not agree with my
fairly provocative statement.
> The comma operator, since it is vastly underused tends to confuse most
> programmer, even competent ones.

I would like to elaborate: There are no good excuses for the comma
operator because it masquerades as true concurrent assignment where
it is a poor relation of it. The thing invites you to cram all sort
of gooey side effects into for loops with increment and assignment
operators. (This is the "Damn the Correctness! Full Speed Ahead"
school of programming.) Now with real concurrent assignment, you
really can put your loop invariant maintenance in one place:

(i++ , j--)

can be replaced by the much more attractive:

(i, j) := (i + 1, j - 1)

where concurrent assignment has the usual semantics. It evaluates
the right hand side completely before making the assignments to
the left hand side. Thus the swapping of two variables is simply

(x, y) := (y, x)

let's see that with the comma operator...maybe you like

(x+=y, y=x-y, x-=y)

instead? (Just kidding. The comma operator is really pretty low
class compared to concurrent assignment.)

One of the most important aspects of programming for clarity is
to avoid the near occasion of operator precedence or side effects.
You want the flow of control to be fully represented on the page
in as consistent a manner as is possible. I am preparing a long
treatment of this issue for posting in this newsgroup but I won't
post it until it shrinks considerably, so for now I'll just divulge
my provisional title: 'For construct considered harmful in C'.

Later,
Andrew Mullhaupt
Disclaimer: Any opinions expressed above (or threatened to be
expressed) are my own, as a result of careful study of the C
language, and are not necessarily those of Morgan Stanley.



More information about the Comp.lang.c mailing list