Path: utzoo!attcan!uunet!seismo!sundc!pitstop!sun!decwrl!purdue!haven!aplcen!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: `if (a = b)' (was Standard indentation?) Message-ID: <14945@mimsy.UUCP> Date: 10 Dec 88 20:22:18 GMT References: <1988Dec8.173158.11839@utzoo.uucp> <846@starfish.Convergent.COM> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 46 In article <846@starfish.Convergent.COM> jerry@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@mimsy.umd.edu Path: uunet!mimsy!chris