One more point regarding = and == (more flamage)

Richard Harter rh at smds.UUCP
Sun Mar 24 16:26:50 AEST 1991


Re:
	a) while (*foo++ = *bar++)
	b) while (*foo ++ == *bar++)
	c) while ((*foo++ = *bar++) != 0)

Sundry claims have been made that (c) is "good style" versus (a).  The
original objection was couched in terms of "testing on assignment inside
conditionals is bad form".  IMNSHO most of the arguments and dogmatic
statements are so much rubbish.

C is a language in which perversion is inherent.  In this case the perversions
are (a) The lack of a Boolean type, and (b) using assignments as expressions
with values.  The latter is an abomination and an uncleanliness that makes
for obscure and baroque code no matter how it is used.  It is a systemic
semantic type confusion which overloads statements, expressions, and values.
It is however, quite convenient.

(c) is no more or no less clean than (a).  In both cases you are testing
the value of the contents, not of foo, but of foo before it was incremented.
Does either statement say that?  Not obviously -- you have to understand C,
get down in the muck with it, before it is is obvious.  The argument, such
as it is, for (c) is that the expression being tested is a boolean expression
whereas in (a) it is an integer [or a char or a pointer or a ...].

But that is a different argument, one that says one should not test on any
thing except Booleans.  If it makes you happy, fine.  Go for it; transform
all tests into Boolean expressions.  Know, though, that in your heart you
are not a C programmer.  C has very simple and explicit rules about 
conditional tests.  In Fortran and Pascal you test against true and false;
in C you test against 0 and non-zero.  Sticking an irrelevant !=0 into the
test is no more than another of the many ways that people have of trying
to convert C into some other language.  Saying that it is clearer is no
more than saying "I want to read C and see Pascal".

Now where did I put that abestos vest?
-- 
Richard Harter, Software Maintenance and Development Systems, Inc.
Net address: jjmhome!smds!rh Phone: 508-369-7398 
US Mail: SMDS Inc., PO Box 555, Concord MA 01742
This sentence no verb.  This sentence short.  This signature done.



More information about the Comp.lang.c mailing list