|
|||||
|
|
#1 |
|
|
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 |
|
|
#2 |
|
|
<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 |
|
|
#3 |
|
|
> 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. |
|
|
#4 |
|
|
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 |