Re: Position of test values in conditional expressions

From: Walter Tross (walter_at_waltertross.com)
Date: 06/20/04


Date: Sun, 20 Jun 2004 16:04:22 +0200

Ney André de Mello Zunino 2004-06-20 :

> I have noticed, in a lot of C and C++ code, that many programmers seem
> to prefer putting the test values first in conditional expressions.
> I.e., they would rather write this:
>
> if (-1 == foobar())
>
> than this:
>
> if (foobar() == -1)
>
> The second form looks more natural and easier to read IMHO and is the
> one I have always used. However, given the high ocurrence of the first
> one, I would like to know the implications of using one way or the other.

As others have already explained, it helps in avoiding the error of
writing an assignment where an equality comparison was intended.
I personally dislike this trick, because it only helps in *initially*
avoiding a mistake, while the more difficult to read code *remains*.
If you rely on this kind of trick to write correct code, it means that you
are not doing any testing, which is the worst mistake you can make.

Walter Tross



Relevant Pages

  • Re: Position of test values in conditional expressions
    ... avoiding a mistake, while the more difficult to read code *remains*. ... If you rely on this kind of trick to write correct code, ...
    (comp.programming)
  • Re: Position of test values in conditional expressions
    ... "Walter Tross" wrote in message ... > are not doing any testing, which is the worst mistake you can make. ... I think any "trick" that helps ensure correct code is worthwhile. ...
    (comp.programming)
  • Re: Position of test values in conditional expressions
    ... "Walter Tross" wrote in message ... > are not doing any testing, which is the worst mistake you can make. ... I think any "trick" that helps ensure correct code is worthwhile. ...
    (comp.lang.cpp)
  • Re: Bid with me
    ... I expect him to notice his mistake and change his call. ... he can't change an inadvertant bid I wait about 30 seconds ... What is the purpose of the BIT? ... chance of being a trick. ...
    (rec.games.bridge)
  • Re: Laws/ethics question
    ... trick to indicate that I'd won it, ... concluded that he thought his H2 was a diamond, and led a trump to get ... him to establish the revoke. ... mistake to gain a trick is totally ethical, and I imagine this is how ...
    (rec.games.bridge)