`if (a = b)' (was Standard indentation?)
Chris Torek
chris at mimsy.UUCP
Sun Dec 11 06:22:18 AEST 1988
In article <846 at starfish.Convergent.COM> jerry at starfish.Convergent.COM
(Gerald Hawkins) writes:
>Is it ever ok to use the form:
[slightly edited]
> if (a = b * 2 + 39) /* intentional assignment within condition */
> ...
>INSTEAD OF:
> a = b * 2 + 39 /* more normal */
> if (a)
> ...
>I say "no!" The code is unsupportable. EVERYONE who ever reads it will
>assume it is a trivial error (and perhaps try to correct it).
Not necessarily everyone, but it is certainly a dubious construct.
This is especially true since you can write
if ((a = b * 2 + 39) != 0)
...
and make it clear that you did not accidentally omit one `='.
I still prefer two separate statements, with one exception:
/* typical sequence from Unibus drivers */
if (unit >= NFOO)
return (ENXIO);
ui = fooinfo[unit];
if (ui == NULL || !ui->ui_alive)
return (ENXIO);
Here one would like to write:
ui = fooinfo[unit];
if (unit >= NFOO || ui == NULL || !ui->ui_alive)
return (ENXIO);
but it is not safe to refer to (fooinfo[k] where k>=NFOO).
Hence:
if (unit >= NFOO || (ui = fooinfo[unit]) == NULL || !ui->ui_alive)
return (ENXIO);
This is rather borderline; if the contents of the `if' statement
were more complex I would be more solidly for it.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.lang.c
mailing list