Re: need help..
- From: Nils Weller <me@xxxxxxxxxxx>
- Date: 31 Mar 2005 21:59:13 GMT
In article <424BD8A4.9303B5B1@xxxxxxxxx>, CBFalconer wrote:
> Martin Ambuhl wrote:
>> slickn_sly wrote:
>>
> ... snip ...
>>
>>> {
>>> if( line = 2 )
>> ^^^^^^^^^^^^
>> This, and all subsequent conditions like it, are almost certainly
>> a mistake. '=' is the assignment operator; '==' is the equality
>> comparison operatior.
>
> Which you can easily avoid at all times by simply cultivating the
> habit of writing:
>
> if (2 == line) ...
>
> i.e. put the constant first.
I don't like this habit for various reasons:
1) Though the reversed version is semantically equivalent, it is harder
to read unless you are already quite used to reversing the test. The
vast majority of programmers always puts the variable they want to test
first, i.e. they write
if (foo == 0) {
The few who write
if (0 == foo) {
.... are extremely likely to do so only because they want to avoid the
comparison vs. assignment problem. (If any of the c.l.c regulars started
to use the latter version for reasons other than the mixed up operators
thing, please stand up.)
This is evidence enough that the former version is more natural, and it
follows that the latter is harder to read and understand, regardless of
the fact that it means the same thing. The fact that writing ``0 ==
foo'' is generally considered to be a programming trick is additional
evidence that it is NOT the natural way to code this.
2) If you can remember to reverse the check every time, you also ought
to be able to remember simply to use ``=='' whenever you mean ``=='',
which would save you the trouble of making your code less readable.
The reversal also only makes any sense for C beginners; Sooner or
later you *will* get used to its equality operator, and the reversed
test only serves the purpose of decreasing code readability anymore.
3) Quality compilers warn about the ``if (foo = 0)'' construct. I know
for a fact only that gcc and HP's compilers do this (and not because the
bug ever occured to me, but because I explictly tested these compilers),
but it is very likely that the offerings of vendors such as Comeau,
Compaq, Intel, SGI, IBM, Sun and a host of others are also capable of
doing this (I don't have these compilers handy right now and will let
others fill in the blanks.)
4) It is not even a reliable way to avoid the problem because it works
well only for constants!
if (foo = bar) {
and
if (bar = foo) {
.... both silently produce (probably) incorrect results (modulo corner
cases like the operand on the left-hand side being ``const''-qualified.)
(It will probably take me a couple of days to reply to any followups -
should I consider them interesting enough to reply - because the free
access to my newsserver is being terminated and I haven't yet registered
a commercial account.)
--
My real email address is ``nils<at>gnulinux<dot>nl''
.
- Follow-Ups:
- Re: need help..
- From: CBFalconer
- Re: need help..
- References:
- need help..
- From: slickn_sly
- Re: need help..
- From: Martin Ambuhl
- Re: need help..
- From: CBFalconer
- need help..
- Prev by Date: Re: execution time becomes unpredictable?!
- Next by Date: Re: strdup()
- Previous by thread: Re: need help..
- Next by thread: Re: need help..
- Index(es):