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

Laura Creighton laura at l5.uucp
Thu Oct 3 06:51:19 AEST 1985

I took a look at that and thought: ``Wow! Does that *ever* offend
my aesthetic sense! Ugly, ugly, ugly...''.  Now I have this cherished
belief that my aethetic sense is inherantly rational; that is to say
that if I think something is ugly then I have some good reason to
say so even if I can't put my finger on it at the time.  And when I
get a strong reaction as I got to that construct, I try to take the time
to find out what that reason is.  But I digress...

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''.  So, when you invert this
you shoot my conceptual model all to hell.  To rectify it I would have
to start writing things like:

	NULL = fp;
	sizeof(string) + 1 = arraylength

and this means swapping lvalues with rvalues -- the language isn't C.

In short, I couldn't use that style because it conflicts with my internal
model of what a program in any block-structured language should look like.
I think that you would be better off to run all your C programs through a
script that prints all instances of ``if(condition)'' where ``condition''
contains a single equals (not precedded by a !, obviously).  The times
I have meant to use this construct (ie it wasn't a bug) I can count on
the fingers of my hands.  But, then again, the times I have done this
wrong I can count on the fingers of my hands as well. My favourite
typo which I can never see is to substitute ``>'' for ``<'' or vice-versa.


Laura Creighton		(note new address!)
sun!l5!laura		(that is ell-five, not fifteen)
l5!laura at

More information about the Comp.lang.c mailing list