constructs like if (NULL == (fp = fopen("foo", "r")))

Alexis Dimitriadis alexis at reed.UUCP
Wed Oct 9 12:57:48 AEST 1985

In article <173 at l5.uucp> laura at l5.UUCP (Laura Creighton) writes:
> The reason I found out that I hate this style is because I see a strong
> conection between statements like:
> 	if (fp = fopen("foo", "r")) == NULL)
> and ones like
> 	fp = NULL;
> that is to say, mentally I have a strong binding between ``this is a
> known value that I want to test things against'' and ``this is a known
> value that I want to assign my variable to''.  

  You are not alone, of course.  Most people, including myself, write
comparisons with the lvalue (or the "interesting" variable, when
neither side is a constant) on the left.  But,, inverting the terms for
comparison was suggested precisely to distinguish it from assignment.
(It would certainly defeat the purpose if you started writing
assignments backwards as well!) Comparison is different from assignment
(some would say _very_ different), and if we can make them look
distinct without loss of clarity somewhere else, why, we should!!

Actually, I said I found 
	if (NULL == (fp = fopen("foo", "r")))
less convoluted than
	if ((fp = fopen("foo", "r")) == NULL)
simply because the function call and its arguments do not get between
the terms of the comparison (fp and NULL).  Anyway, in my code I use
the "standard" form, because it is.

Alexis Dimitriadis
  As soon as I get a full time job, the opinions expressed above
will attach themselves to my employer, who will never be rid of
them again.
				alexis @ reed

More information about the Comp.lang.c mailing list