condition convention 'if (10 == j)...'

Peter S. Shenkin peters at cubsvax.UUCP
Mon Apr 29 14:47:00 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

Just that instead of writing, in the traditional manner,
	if(pc=malloc(nbytes)==NULL)...
you have to insert extra parens if you use the other form:
	if(NULL==(pc=malloc(nbytes)))..
This is because == has higher precedence than =, and the expression will
first evaluate NULL==pc, then try to set the resulting constant to whatever 
malloc() returns (except, of course, the compiler doesn't let you get that far).

I used to use the (NULL==var form but reverted to the more traditional
form because the extra parens kept making me do a double-take.
I'd rather skip the parens and always put the constant at the end. That way
I always know where to look for it.  You pays your money & takes your choice.
If it weren't for this problem, though, I'd use the proposed form.

Peter S. Shenkin,  Biology, Columbia Univ.   cubsvax!peters



More information about the Comp.lang.c mailing list