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

Robert Osborne robert at isgtec.UUCP
Mon Mar 25 23:51:53 AEST 1991


In article <355 at smds.UUCP>, rh at smds.UUCP (Richard Harter) writes:
|> 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.
Well the biggest argument has been if you use a) the maintainer can't tell
if you meant a) or b);  if you use c) the maintainer KNOWS you meant a).
This isn't rubbish.

|> (c) is no more or no less clean than (a).
It's just more readable.

|> 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 'C' is obvious in all of a), b) and c); the programmers INTENT is only
obvious in b) and c)!

|> 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.
And don't comment (or comment in katakana :-) for that macho
'I'm a *REAL* C programmer' feeling.

|> C has very simple and explicit rules about conditional tests.
Beautiful isn't it (NOT a flame :-).

|> 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".
No, saying it is clearer is know more than saying "I want to read C and
see the developers intent".   The C should be a concise as possible, with
'possible' being defined as 'developer's intent is very clear'.  Trying to
convert C into some other language means using #define EQ ==.

|> Richard Harter, Software Maintenance and Development Systems, Inc.
                            ^^^^^^^^^^^
                    you should know better :-)

Rob.
---
Robert A. Osborne   ...uunet!utai!lsuc!isgtec!robert or robert at isgtec.uucp



More information about the Comp.lang.c mailing list