 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ney André de Mello Zunino Guest
|
Posted: Sun Jun 20, 2004 3:40 am Post subject: 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
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Sun Jun 20, 2004 4:11 am Post subject: Re: Position of test values in conditional expressions |
|
|
On Sun, 20 Jun 2004 00:40:25 -0300, Ney André de Mello Zunino
<zunino (AT) inf (DOT) ufsc.br> wrote in comp.lang.c++:
| Quote: | 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;
// assign 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 assigned 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/~ajo/docs/FAQ-acllc.html
|
|
| Back to top |
|
 |
E. Robert Tisdale Guest
|
Posted: Sun Jun 20, 2004 4:22 am Post subject: Re: Position of test values in conditional expressions |
|
|
Ney André de Mello Zunino wrote:
| Quote: | 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 assignment 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
| Quote: | cat main.cc
int main(int argc, char* argv[]) { |
int x = 0;
// . . .
if (-1 = x);
return 0;
}
| Quote: | g++ -Wall -ansi -pedantic -o main main.cc
main.cc: In function `int main(int, char**)': |
main.cc:4: error: non-lvalue in assignment
The get a diagnostic message from the compiler like the one above.
In your example:
| Quote: | cat main.cc
int foobar(void) { |
return -1;
}
int main(int argc, char* argv[]) {
if (foobar() = -1);
return 0;
}
| Quote: | g++ -Wall -ansi -pedantic -o main main.cc
main.cc: In function `int main(int, char**)': |
main.cc:6: error: non-lvalue in assignment
You get a diagnostic if you type the assignment 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.
|
|
| Back to top |
|
 |
JKop Guest
|
Posted: Sun Jun 20, 2004 9:42 am Post subject: Re: Position of test values in conditional expressions |
|
|
Jack Klein posted:
| Quote: | 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
|
|
| Back to top |
|
 |
Ney André de Mello Zunino Guest
|
Posted: Sun Jun 20, 2004 1:46 pm Post subject: Re: Position of test values in conditional expressions |
|
|
Thank you Jack and Robert for you clarifications.
--
Ney André de Mello Zunino
|
|
| Back to top |
|
 |
Walter Tross Guest
|
Posted: Sun Jun 20, 2004 2:04 pm Post subject: Re: Position of test values in conditional expressions |
|
|
Ney André de Mello Zunino 2004-06-20 :
| Quote: | 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
|
|
| Back to top |
|
 |
Ben Measures Guest
|
Posted: Sun Jun 20, 2004 3:26 pm Post subject: Re: Position of test values in conditional expressions |
|
|
Walter Tross wrote:
| Quote: | 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)
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.
|
Good point well put.
--
Ben M.
|
|
| Back to top |
|
 |
Dave Guest
|
Posted: Mon Jun 21, 2004 1:56 pm Post subject: Re: Position of test values in conditional expressions |
|
|
E. Robert Tisdale wrote:
| Quote: | 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)
|
It also makes the code slightly easier to read, if you're only
interested in this function and not the stuff that it calls:
if (POSS_1 == some_complicated_expression)
{
// this code is executed if something we're not interested in
// just yet evaluates to POSS_1
}
So here we're only really interested in POSS_1 and what happens when we
encounter that result. How POSS_1 is calculated isn't really important
right now.
If this reads the other way round then you have to scan to the end of
the equality test before you find the stuff you're interested in.
Semantically of course there's no difference; if A == B then B == A, but
visually it can make code easier to understand.
Dave.
|
|
| Back to top |
|
 |
Andre Kostur Guest
|
Posted: Mon Jun 21, 2004 3:21 pm Post subject: Re: Position of test values in conditional expressions |
|
|
Ben Measures <saint_abroadremove (AT) removehotmail (DOT) com> wrote in
news:11iBc.947$VA4.8163369 (AT) news-text (DOT) cableinet.net:
| Quote: | Walter Tross wrote:
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)
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.
Good point well put.
|
IMHO: I prefer to use the (foobar() == -1) form because I know my compiler
will issue a warning if I try to "if (foobar() = 1)". (Um, one of my two
compilers.... so, yeah, I know it's not a diagnostic that the compiler is
_required_ to issue, I just know that my tool _does_).
|
|
| Back to top |
|
 |
Corey Murtagh Guest
|
Posted: Tue Jun 22, 2004 1:22 pm Post subject: Re: Position of test values in conditional expressions |
|
|
Andre Kostur wrote:
<snip>
| Quote: | IMHO: I prefer to use the (foobar() == -1) form because I know my compiler
will issue a warning if I try to "if (foobar() = 1)". (Um, one of my two
compilers.... so, yeah, I know it's not a diagnostic that the compiler is
_required_ to issue, I just know that my tool _does_).
|
If foobar() returns a reference to a non-const object then your compiler
likely /won't/ return a diagnostic unless you tell it to be /really/
anal. For example:
int some_int = 0;
int& foobar()
{
return some_int;
}
int main()
{
if (foobar() = 1)
return 1;
return 0;
}
--
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"
|
|
| Back to top |
|
 |
Andre Kostur Guest
|
Posted: Tue Jun 22, 2004 3:37 pm Post subject: Re: Position of test values in conditional expressions |
|
|
Corey Murtagh <emonk (AT) slingshot (DOT) no.uce> wrote in
news:1087911000.283118 (AT) radsrv1 (DOT) tranzpeer.net:
| Quote: | Andre Kostur wrote:
snip
IMHO: I prefer to use the (foobar() == -1) form because I know my
compiler will issue a warning if I try to "if (foobar() = 1)". (Um,
one of my two compilers.... so, yeah, I know it's not a diagnostic
that the compiler is _required_ to issue, I just know that my tool
_does_).
If foobar() returns a reference to a non-const object then your
compiler likely /won't/ return a diagnostic unless you tell it to be
/really/ anal. For example:
int some_int = 0;
int& foobar()
{
return some_int;
}
int main()
{
if (foobar() = 1)
return 1;
return 0;
}
|
Yep... the compiler complained about it:
test.cpp: In function `int main ()':
test.cpp:10: warning: suggest parentheses around assignment used as truth
value
(Happens to be gcc v2.96, with -Wall as a compiler flag. When I get
really picky, I turn on -Werror too, to make all warnings errors....) (In
all of my makefiles, -Wall is always supplied.)
|
|
| Back to top |
|
 |
William Guest
|
Posted: Tue Jun 22, 2004 8:37 pm Post subject: Re: Position of test values in conditional expressions |
|
|
"Walter Tross" <walter (AT) waltertross (DOT) com> wrote
| Quote: | 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.
|
I think any "trick" that helps ensure correct code is worthwhile. Since
I've seen a number of problems where tests would not reveal that the
intended == was replaced with =, I wouldn't rely solely on the tests
either.
How can the tests not reveal the problem? When the code that is
excuted every time as a results of the ( x = 3) or whatever doesn't
alter the final outcome. This can happen when the test is used to
bypass an operation when it would be redundant and avoiding the
overhead is worthwhile. (I saw a problem like this in a graphics
routine - sped up quite a bit once the problem was fixed. Had the
original programmer applied a trick or two it would have been
flagged and fixed months earlier.) -Wm
|
|
| Back to top |
|
 |
Darrell Grainger Guest
|
Posted: Tue Jun 22, 2004 9:09 pm Post subject: Re: Position of test values in conditional expressions |
|
|
On Tue, 22 Jun 2004, William wrote:
| Quote: | "Walter Tross" <walter (AT) waltertross (DOT) com> wrote in message
news:1kw2w7m6eyvbt.sjqua03ibh5t$.dlg (AT) 40tude (DOT) net...
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.
I think any "trick" that helps ensure correct code is worthwhile. Since
I've seen a number of problems where tests would not reveal that the
intended == was replaced with =, I wouldn't rely solely on the tests
either.
How can the tests not reveal the problem? When the code that is
excuted every time as a results of the ( x = 3) or whatever doesn't
alter the final outcome. This can happen when the test is used to
bypass an operation when it would be redundant and avoiding the
overhead is worthwhile. (I saw a problem like this in a graphics
routine - sped up quite a bit once the problem was fixed. Had the
original programmer applied a trick or two it would have been
flagged and fixed months earlier.) -Wm
|
Just something to consider...
If it costs you nothing to do this or if it is something you are willing
to spend your time learning to do (rather than the company's time) I see
nothing wrong with doing it.
If your productivity is going to drop because you are trying to get into
the habit of using this trick then I would have a problem with it. I'd
consider whether buying a good lint application would be cheaper then the
lost productivity.
Additionally, I would not impose this policy on someone else. I'd suggest
it. If they didn't feel it was worth while, do I want to spend the time
(which costs the company money) arguing the point? Would it be more
economical to just buy a lint application?
By the way, my company uses a lint application that always catches when
programmers use an assignment in a conditional expression. No need for the
programmers to learn a trick to make the compiler catch it MOST of the
time when I can get an application that catches it ALL of the time.
--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to [email]vice.president (AT) whitehouse (DOT) gov[/email]
|
|
| Back to top |
|
 |
Programmer Dude Guest
|
Posted: Tue Jun 22, 2004 9:38 pm Post subject: Re: Position of test values in conditional expressions |
|
|
writes:
| Quote: | How can the tests not reveal the problem?
|
Inept compiler or no use of a lint tool?
| Quote: | (I saw a problem like this in a graphics
routine - sped up quite a bit once the problem was fixed. Had the
original programmer applied a trick or two it would have been
flagged and fixed months earlier.) -Wm
|
Just as another data point, I've never experienced the error in
over two decades of experience. Of course, my compilers always
whined about it and I never use assignment in a test expression.
(So any compiler warnings about this sort of thing ARE errors.)
--
Somewhere in the Midwest...
Chris Sonnack <Chris (AT) Sonnack (DOT) com> in Training...
http://www.Sonnack.com/
|
|
| Back to top |
|
 |
CBFalconer Guest
|
Posted: Wed Jun 23, 2004 3:21 am Post subject: Re: Position of test values in conditional expressions |
|
|
Darrell Grainger wrote:
| Quote: |
.... snip ...
By the way, my company uses a lint application that always
catches when programmers use an assignment in a conditional
expression. No need for the programmers to learn a trick to
make the compiler catch it MOST of the time when I can get
an application that catches it ALL of the time.
|
But I often *want* to embed an assignement in a conditional, and
your gang of hackers should also:
void foo(int count, char *fn)
{
FILE *fp = NULL;
bar *buf;
if (!(buf = malloc(count * sizeof *buf))) err("No mem");
else if (!(fp = fopen(fn, "r")) err("can't open");
else {
/* do my thing with fp and buf */
}
if (fp) fclose(fp);
free(buf);
}
and I can concentrate on doing my thing. The wrappers are solid,
clean, and automatic. I haven't checked fclose.
--
Chuck F (cbfalconer (AT) yahoo (DOT) com) (cbfalconer (AT) worldnet (DOT) att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|