 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ask Guest
|
Posted: Wed Dec 21, 2005 1:07 pm Post subject: coding style question |
|
|
This is a question related to style. Which one is preferrable between
the two below -
if ( var == 0) -----(1)
if ( 0 == var) -----(2)
Does the 2nd one look awkward ?
I prefer the 2nd one in which all constants, literals are kept at the
left side of the operator "==". This is because if somebody accidently
types "=" instead of "==",
the code won't compile.
if (var=0) - compiles
if (0=var) - doesn't compile.
So the second one is safer. Your comments ?
Thanks
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Roland Csaszar Guest
|
Posted: Wed Dec 21, 2005 5:08 pm Post subject: Re: coding style question |
|
|
Hi,
At 21 Dec 2005 08:07:11 -0500,
Ask wrote:
| Quote: | I prefer the 2nd one in which all constants, literals are kept at the
left side of the operator "==". This is because if somebody accidently
types "=" instead of "==",
the code won't compile.
if (var=0) - compiles
if (0=var) - doesn't compile.
So the second one is safer. Your comments ?
|
Any compiler should (and does, at least all i know) throw a warning or info on such constructs.
GCC for example says "warning: suggest parentheses around assignment used as truth value" (when compiled with -Wall).
Actually, at least for me, the second one is harder to comprehend, in everyday life you would say something like "if the box is empty" and not
"if empty is the box" and i tend to write things down that way.
Regards,
Roland
--
Roland Csaszar ----------- \ /// -------------- +43 316 495 2129
Software Development ------ \ /// ----------- http://www.knapp.com
KNAPP Logistics Automation - \V// - mailto:roland.csaszar (AT) knapp (DOT) com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dervish Guest
|
Posted: Wed Dec 21, 2005 5:09 pm Post subject: Re: coding style question |
|
|
| Quote: | if ( var == 0) -----(1)
if ( 0 == var) -----(2)
|
In this specific example one should write
if ( !var) -----(3)
If there is a comparison with non zero value e.g. if ( var == 3) both
variants acceptable.
However if one writes if ( var = 3) reasonable compiler warns about
possible error. It does not stop compilation, but can help developer to
find real error.
Nevertheless for my mind if ( 3 == var) construction is hardly
readable.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jim Chaney Guest
|
Posted: Wed Dec 21, 2005 5:12 pm Post subject: Re: coding style question |
|
|
Ask wrote:
| Quote: | This is a question related to style. Which one is preferrable between
the two below -
if ( var == 0) -----(1)
if ( 0 == var) -----(2)
Does the 2nd one look awkward ?
|
Yes.
| Quote: | I prefer the 2nd one in which all constants, literals are kept at the
left side of the operator "==". This is because if somebody accidently
types "=" instead of "==",
the code won't compile.
if (var=0) - compiles
if (0=var) - doesn't compile.
So the second one is safer. Your comments ?
|
Almost all compilers will issue a warning about the above example (eg.
assignment in conditional expression) at their most pedantic settings.
I would recommend you enable these options and turn on warnings as
errors also.
IMO you shouldnt give up code readability for something the compiler
can easily catch.
Jim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Niek Sanders Guest
|
Posted: Wed Dec 21, 2005 5:13 pm Post subject: Re: coding style question |
|
|
Ask wrote:
| Quote: | This is a question related to style. Which one is preferrable between
the two below -
if ( var == 0) -----(1)
if ( 0 == var) -----(2)
Does the 2nd one look awkward ?
|
I find that the second variant looks very funky, though I've certainly
seen people use the style before. I use the first style; it comes
closer to the way I think when it comes to both programming and
mathematics in general. The voice inside my head says "if x is 2...",
not "if 2 is x...". Heck, the latter even carries a connotation that 2
is somehow not variant.
Yes, I could retrain myself to think in the latter style, but I've
never been bitten by this issue badly enough for me to care. So you
can keep asking if "black is the apple?", while I'll stick with "is the
apple black?"
Cheers,
Niek Sanders
http://www.cis.rit.edu/~njs8030/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Robert Kindred Guest
|
Posted: Wed Dec 21, 2005 5:14 pm Post subject: Re: coding style question |
|
|
"Ask" <ask.q (AT) indiatimes (DOT) com> wrote
| Quote: | This is a question related to style. Which one is preferrable between
the two below -
if ( var == 0) -----(1)
if ( 0 == var) -----(2)
[]
I prefer the 2nd one in which all constants, literals are kept at the
left side of the operator "==". This is because if somebody accidently
types "=" instead of "==",
the code won't compile.
|
I like your safety precaution, but it does not allow the operator== to be
overloaded if var is a class.
Robert Kindred
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Thu Dec 22, 2005 3:07 am Post subject: Re: coding style question |
|
|
In article <1135145437.686421.301420 (AT) g14g2000cwa (DOT) googlegroups.com>, Ask
<ask.q (AT) indiatimes (DOT) com> writes
| Quote: | This is a question related to style. Which one is preferrable between
the two below -
if ( var == 0) -----(1)
if ( 0 == var) -----(2)
Does the 2nd one look awkward ?
I prefer the 2nd one in which all constants, literals are kept at the
left side of the operator "==". This is because if somebody accidently
types "=" instead of "==",
the code won't compile.
if (var=0) - compiles
if (0=var) - doesn't compile.
So the second one is safer. Your comments ?
|
Most compilers raise warnings for assignment in conditionals at least
for a sufficiently high setting of warnings. And how would you deal with
miss-typing
i == 0;
for
i=0;
Quite possible, particularly on keyboards that are getting old.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Braden McDaniel Guest
|
Posted: Thu Dec 22, 2005 3:13 am Post subject: Re: coding style question |
|
|
On Wed, 2005-12-21 at 08:07 -0500, Ask wrote:
| Quote: | This is a question related to style. Which one is preferrable between
the two below -
if ( var == 0) -----(1)
if ( 0 == var) -----(2)
Does the 2nd one look awkward ?
|
Yes.
| Quote: | I prefer the 2nd one in which all constants, literals are kept at the
left side of the operator "==". This is because if somebody accidently
types "=" instead of "==",
the code won't compile.
if (var=0) - compiles
if (0=var) - doesn't compile.
So the second one is safer. Your comments ?
|
I'm skeptical of the value of any convention that makes code harder to
read. IME, this error doesn't come up frequently enough to justify the
mental inversion imposed by the convention you suggest. If one is
sufficiently conscientious to apply such a convention consistently, one
is probably capable of applying similar discipline to avoid the problem
in the first place.
--
Braden McDaniel e-mail: <braden (AT) endoframe (DOT) com>
<http://endoframe.com> Jabber: <braden (AT) jabber (DOT) org>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Thu Dec 22, 2005 3:13 am Post subject: Re: coding style question |
|
|
On 21 Dec 2005 08:07:11 -0500, "Ask" <ask.q (AT) indiatimes (DOT) com> wrote:
| Quote: | This is a question related to style. Which one is preferrable between
the two below -
if ( var == 0) -----(1)
if ( 0 == var) -----(2)
Does the 2nd one look awkward ?
I prefer the 2nd one in which all constants, literals are kept at the
left side of the operator "==". This is because if somebody accidently
types "=" instead of "==",
the code won't compile.
if (var=0) - compiles
if (0=var) - doesn't compile.
So the second one is safer. Your comments ?
|
Safest is IMHO:
if (!var) { /*...*/}
Of course, when comparing to any other value, number 2 above has
strong merits, as you have illustrated.
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Thu Dec 22, 2005 3:16 am Post subject: Re: coding style question |
|
|
On 21 Dec 2005 12:14:01 -0500, "Robert Kindred" <RKindred (AT) SwRI (DOT) edu>
wrote:
| Quote: |
"Ask" <ask.q (AT) indiatimes (DOT) com> wrote in message
news:1135145437.686421.301420 (AT) g14g2000cwa (DOT) googlegroups.com...
This is a question related to style. Which one is preferrable between
the two below -
if ( var == 0) -----(1)
if ( 0 == var) -----(2)
[]
I prefer the 2nd one in which all constants, literals are kept at the
left side of the operator "==". This is because if somebody accidently
types "=" instead of "==",
the code won't compile.
I like your safety precaution, but it does not allow the operator== to be
overloaded if var is a class.
|
It's possible to overload operator== as a non-member operator taking
arguments of (int, class_type const &) so that the comparison with the
literal on the left would work (assuming that class_type is the type
of "var" in the above example).
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
wizofaus@hotmail.com Guest
|
Posted: Thu Dec 22, 2005 12:11 pm Post subject: Re: coding style question |
|
|
Francis Glassborow wrote:
| Quote: |
Most compilers raise warnings for assignment in conditionals at least
for a sufficiently high setting of warnings. And how would you deal with
miss-typing
i == 0;
for
i=0;
Quite possible, particularly on keyboards that are getting old.
Happens with brand new keyboards too - it's called "hypercorrection". |
Like using "whom" where it actually should be "who" etc. etc. I've
done it more than once, and compilers often don't even bother warning
about it.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ask Guest
|
Posted: Thu Dec 22, 2005 12:13 pm Post subject: Re: coding style question |
|
|
| Quote: | Does the 2nd one look awkward ?
Yes.
So the second one is safer. Your comments ?
I'm skeptical of the value of any convention that makes code harder to
read. IME, this error doesn't come up frequently enough to justify the
mental inversion imposed by the convention you suggest. If one is
sufficiently conscientious to apply such a convention consistently, one
is probably capable of applying similar discipline to avoid the problem
in the first place.
|
"Condition Format" at the following site mentions this -
http://www.possibility.com/Cpp/CppCodingStandard.html
Also, compiler doesn't throw warning in this case
y = (x = 4) ? 10 : 5;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Guest
|
Posted: Thu Dec 22, 2005 12:13 pm Post subject: Re: coding style question |
|
|
I prefer the first one but sometimes use the second one when the left
expression is very long.
instead of
if (static_cast<int>(longvariable.longmember()) == 2)
I write
if (2 == static_cast<int>(longvariable.longmember()))
Often I understand from the context or comments what the expression
does but don't always remember which operator is used in the
comparison.
With the first version I might need to scroll the display to see the
operator.
For the same reason I sometimes put && and || first on second row
instead of last on the first.
if (longexpression1
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Thu Dec 22, 2005 12:14 pm Post subject: Re: coding style question |
|
|
Niek Sanders wrote:
| Quote: | Ask wrote:
This is a question related to style. Which one is preferrable between
the two below -
if ( var == 0) -----(1)
if ( 0 == var) -----(2)
Does the 2nd one look awkward ?
Yes, I could retrain myself to think in the latter style, but I've
never been bitten by this issue badly enough for me to care. So you
can keep asking if "black is the apple?", while I'll stick with "is the
apple black?"
|
Nice example!
However, there are situations where we are forced to "phrase"
things in an awkward way:
if (! is_empty()) // this could be used as an argument against
// the "is_" as part of the name
if (a == 1 || > 10) // that's how we say it in English -- but it
// doesn't work in most programming languages...
One example where IMO the constant looks much nicer on the left is:
if (0 < a && a < 10)
(suggests the standard math notation 0 < a < 10 )
Cheers,
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mateusz Łoskot Guest
|
Posted: Thu Dec 22, 2005 12:15 pm Post subject: Re: coding style question |
|
|
Ask wrote:
| Quote: | This is a question related to style. Which one is preferrable between
the two below -
if ( var == 0) -----(1)
if ( 0 == var) -----(2)
Does the 2nd one look awkward ?
|
No.
One can get familiar easily with the second style as fast as with the
first one.
IMHO the second is safer, because that simple rule will prevent you from
possbile typos.
Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| 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
|
|