> Programming Languages > C++
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on C++



C++ - "Position of test values in conditional expressions" in Programming Languages


Old 06-20-2004   #1
..y ..d.. .. ..l.. ..ni..
 
Default Position of test values in conditional expressions

Hello.

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.

Thank you,

--
Ney André de Mello Zunino
 
Old 06-20-2004   #2
.... ..e..
 
Default Re: Position of test values in conditional expressions

On Sun, 20 Jun 2004 00:40:25 -0300, Ney André de Mello Zunino
<zunino@inf.ufsc.br> wrote in comp.lang.c++:

> Hello.
>
> 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.
>
> Thank you,


The compiler does not care, one way or the other. And except for the
case of a C++ function returning a reference to a non-const object,
there is no gain.

But when doing equality tests against constant expressions, consider
this:

int x;
// ***ign some value to x
if (x == 3)
// do something

Now consider what happens if the programmer commits the not uncommon
error of typing only '=' when '==' was intended. The resulting code:

if (x = 3)
// do something

....is perfectly legal, and has the effect of evaluating the constant
value ***igned to x. If the constant value is 0, the test is always
false and the conditional code is always skipped. If the constant is
not 0, the test is always true and the conditional code is always
executed.

On the other hand, if you always write:

if (3 == x)

....then if you accidentally leave off the second '=' the result is a
constraint violation requiring the compiler to issue a diagnostic.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Old 06-20-2004   #3
.. ..be.. ..sda..
 
Default Re: Position of test values in conditional expressions

Ney André de Mello Zunino wrote:

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


What you have noticed is a *habit* that some programmers adopt
to help them avoid a common mistake:

int x = 0;
// . . .
if (x = -1) // Error! Should have been (x == -1)

One problem with C and C++ is that
operator== is too *close* to operator=
and the programmer might mistype the ***ignment operator
when the comparison operator was intended.
This typographical error is difficult to spot
when debugging your code so some programmers write:

if (-1 == x) // . . .

so that if they mistype

> cat main.cc

int main(int argc, char* argv[]) {
int x = 0;
// . . .
if (-1 = x);
return 0;
}

> g++ -Wall -ansi -pedantic -o main main.cc

main.cc: In function `int main(int, char**)':
main.cc:4: error: non-lvalue in ***ignment

The get a diagnostic message from the compiler like the one above.
In your example:

> cat main.cc

int foobar(void) {
return -1;
}

int main(int argc, char* argv[]) {
if (foobar() = -1);
return 0;
}

> g++ -Wall -ansi -pedantic -o main main.cc

main.cc: In function `int main(int, char**)':
main.cc:6: error: non-lvalue in ***ignment

You get a diagnostic if you type the ***ignment operator
instead of the comparison operator because foobar(void)
does *not* return an lvalue.
This is probably just an example of *force-of-habit*.
Programmers who use this trick get used to seeing the constant
on the left-hand side of a comparison and feel more comfortable
writing *all* comparison expressions that way
even if they don't contain an lvalue.
 
Old 06-20-2004   #4
....
 
Default Re: Position of test values in conditional expressions

Jack Klein posted:

> On the other hand, if you always write:
>
> if (3 == x)
>
> ...then if you accidentally leave off the second '=' the result is a
> constraint violation requiring the compiler to issue a diagnostic.



Very clever indeed!


-JKop
 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0