Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site ulysses.UUCP Path: utzoo!watmath!clyde!bonnie!akgua!gatech!ulysses!ggs From: ggs@ulysses.UUCP (Griff Smith) Newsgroups: net.lang.c Subject: Re: condition convention 'if (10 == j)...' Message-ID: <958@ulysses.UUCP> Date: Sun, 28-Apr-85 21:53:54 EDT Article-I.D.: ulysses.958 Posted: Sun Apr 28 21:53:54 1985 Date-Received: Mon, 29-Apr-85 04:37:19 EDT References: <137@bocklin.UUCP> <1456@amdahl.UUCP> Organization: AT&T Bell Laboratories, Murray Hill Lines: 48 > > 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@ulysses.uucp UUCP: ulysses!ggs ( {allegra|ihnp4}!ulysses!ggs )