Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site cubsvax.UUCP Path: utzoo!linus!philabs!cmcl2!rna!cubsvax!peters From: peters@cubsvax.UUCP (Peter S. Shenkin) Newsgroups: net.lang.c Subject: Re: condition convention 'if (10 == j)...' Message-ID: <332@cubsvax.UUCP> Date: Mon, 29-Apr-85 00:47:00 EDT Article-I.D.: cubsvax.332 Posted: Mon Apr 29 00:47:00 1985 Date-Received: Tue, 30-Apr-85 02:30:43 EDT References: <137@bocklin.UUCP> <> Reply-To: peters@cubsvax.UUCP (Peter S. Shenkin) Organization: Columbia Univ Biology, New York City Lines: 29 Summary: >> 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