condition convention 'if (10 == j)...'
Griff Smith
ggs at ulysses.UUCP
Mon Apr 29 11:53:54 AEST 1985
> > To prevent silly mistakes like
> > if (j = 10)
> > I usually write
> > if (10 == j)
> > By putting the constant first, I ensure that the compiler will catch the
> > typo.
>
> I think this is a good idea. Any criticisms? The only problem
> I have with it is that I am not accustomed to reading code written
> this way.
> --
> Gordon A. Moffett ...!{ihnp4,cbosgd,sun}!amdahl!gam
Don't get me wrong, I also think the idea has some charm, but it's not as
innocent a change as it appears to be. I recently found a bug in a C
compiler: if (j == 0.0) worked, if (0.0 == j) didn't. It seems the
implementors wanted to avoid creating double precision constant zeros,
so they planned a two-step optimization where
L1: .long 0x00000000, 0x00000000
.text
cmpd2 4(fp),L1
would be reduced to
cmpd2 4(fp),$0
and then further reduced to
tstl 4(fp)
Unfortunately, they forgot that "cmpd2 $0,j" doesn't fit the pattern;
the intermediate code pattern escaped to the final output. Since $0
was not a legal representation of a floating point number (this was not
a VAX), the assembler screamed about an illegal address and sulked.
The compiler had apparently never encountered this case in any of the
UNIX(TM) System source.
The point of all this is: if you want to use unusual conventions, be
prepared to evade bugs in compilers designed for those who are less
imaginative. Also be prepared to defend against flames from people
who find it hard to read your code.
--
Griff Smith AT&T Bell Laboratories, Murray Hill
Phone: (201) 582-7736
Internet: ggs at ulysses.uucp
UUCP: ulysses!ggs ( {allegra|ihnp4}!ulysses!ggs )
More information about the Comp.lang.c
mailing list