[net.lang.c] Poll on style issue in comparison

rcd@opus.UUCP (Dick Dunn) (09/20/85)

I'd like to poll you folks on a small matter of style.  I've seen it
mentioned from time to time in lang.c.  PLEASE email (DON'T POST) answers
to the questions below; I'll post responses and comments in a bit.

The issue is this:  People tend to stumble over the difference between ==
and =.  It's not that they don't know the difference; they just screw up,
typically writing something like
	if (x=0) ...
when they mean
	if (x==0) ...
Some people have suggested reversing the order of comparisons, viz:
	if (0==x) ...
so that the compiler will complain if = is substituted for ==.
One argument against this form is that it is an unconventional order which
is harder to read.
- - - - - - - - - - - - - - - - - - - - - - - - - -
The questions:

1.  Do you use this form of comparison?

2.  Regardless of whether you use it, how do you feel about it (e.g., other
    people using it in code that you will maintain)?
	- dislike it
	- neutral (OK if you want to do it)
	- like it

3.  If you use or like this way of writing comparisons, how do you feel
    about also writing ordering tests with the constant first, as
	if (0<x) ...
    - dislike
    - neutral
    - like

Opinions welcome in addition to answers to the above; I'll summarize.
Again, please MAIL responses rather than posting them.
-- 
Dick Dunn	{hao,ucbvax,allegra}!nbires!rcd		(303)444-5710 x3086
   ...Lately it occurs to me what a long, strange trip it's been.

jpm@BNL44.ARPA (John McNamee) (09/25/85)

[I tried to send this via mail to rcd%opus.uucp@BRL.ARPA, but it was
 rejected, so I'm posting it instead. The original article was asking
 about placing the constant on the left side of the equals sign in if
 tests (i.e. "if (0==strcmp(a,b))").]

>1.  Do you use this form of comparison?

No.
	
>2.  Regardless of whether you use it, how do you feel about it (e.g., other
>    people using it in code that you will maintain)?
>	- dislike it
>	- neutral (OK if you want to do it)
>	- like it

I refuse to work on code that is written that way. I once spent a whole day
undoing that style from a program I had to make changes to.

>3.  If you use or like this way of writing comparisons, how do you feel
>    about also writing ordering tests with the constant first, as
>	if (0<x) ...
>    - dislike
>    - neutral
>    - like

People who do that should be taken out and shot. No, on second thought that
would be too quick. They should be tortured before being taken out and shot.

Comment:

What you do in private with consenting compilers is your own business, but
if I'm going to have to maintain your code then you don't have the right
to inflict your style on me. K&R style is readable, and is the standard.
Other than special permission from God, there is no excuse for anything
but K&R style.
--
			 John McNamee
		..!decvax!philabs!sbcs!bnl44!jpm
			jpm@BNL44.ARPA

		  "MS-DOS is a communist plot"

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

This is in response to an article asking about the proposed style of
comparisons where the constant is placed on the left, to eliminate
typos like
	if (x=NULL)
> >1.  Do you use this form of comparison?
> 
> No.
As a matter of fact, I tried.  I was rather intrigued when the subject
came up, so I thought I'd give it a go.  One pleasant side effect is things
like 
	if (NULL == (fp = fopen("foo", "r")))
		scream();
The above looks, to me at least, more readable than the other leading product.
The approach seems to work OK when testing for equality, but I felt that
consistency required that I use it in all tests.  However, to reverse
	if (foo > 0)
I have to write
	if (0 < foo)
which is more than a little twisted.  It may be my conditioning, but I _do_not
immediately perceive the two as equivalent.  I stuck with it, but it 
required extra effort to reverse the operators.  
  Another problem was, not all comparisons involve a hard constant.  
Do we reverse
	if (i == some.external->variable)
to convey the fact that we are testing the value of i?  And so on.
  A few weeks later I had to modify the program, and since I did not at
the time feel like going through the same, I put everything back the way
God meant it to be.  Too bad, but mixing the two styles is a complete
no-no.  If there weren't all that existing code that we all need to look at,
this style might stand a chance.  It may have a chance if used for
equality tests only, but that is not as orthogonal.

I hope someone cared about my experience with this silly, but interesting
style.

-- 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