[net.lang.c] constructs like if

laura@l5.uucp (Laura Creighton) (10/02/85)

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@lll-crg.arpa

alexis@reed.UUCP (Alexis Dimitriadis) (10/09/85)

In article <173@l5.uucp> laura@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
    {decvax,ihnp4,ucbcad,uw-beaver}!tektronix!reed.UUCP