Hi All,

I have seen if tests written both of the ways shown below. Is
this just considered a matter of personal style or do you feel
there's some inherent advantage of doing it one way or the other?
I typically do it the second way.

if ( 0 == MyFunc( stuff) )
{
}

if ( MyFunc( stuff ) == 0 )
{
}

- Arnie

Re: A matter of style? by Igor

Igor
Thu Jul 20 11:18:52 CDT 2006

Arnie <none> wrote:
> I have seen if tests written both of the ways shown below. Is
> this just considered a matter of personal style or do you feel
> there's some inherent advantage of doing it one way or the other?
> I typically do it the second way.
>
> if ( 0 == MyFunc( stuff) )
> {
> }
>
> if ( MyFunc( stuff ) == 0 )
> {
> }

The argument for the first approach goes like this. Consider:

if (x == 0) {...} // (1)
if (0 == x) {...} // (2)

If you accidentally type '=' instead of '==' the compiler won't catch it
in (1) but will produce an error in (2).

Personally, I'm unconvinced by this argument and always use (1). I find
it more readable, and I don't recall ever making such a typo. Also, VC++
produces a warning for "if (x = 0)" statement. But ultimately, it's a
matter of personal preference.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: A matter of style? by Victor

Victor
Thu Jul 20 11:19:18 CDT 2006

Arnie wrote:
> I have seen if tests written both of the ways shown below. Is
> this just considered a matter of personal style or do you feel
> there's some inherent advantage of doing it one way or the other?
> I typically do it the second way.
>
> if ( 0 == MyFunc( stuff) )
> {
> }
>
> if ( MyFunc( stuff ) == 0 )
> {
> }

If the operator == used here is not some overloaded for the return
type of 'MyFunc', there is no difference (semantically or syntactially).

My personal preference is the latter -- it follows the natural reading
method: ( 'if' a call to 'MyFunc' with argument 'stuff' gives 0 ... ),
[I don't read ( 'if' 0 equals a call to ... ), do you?]

AFAICT, the former style isa an extension of a known method of preventing
accidental assignment where an equality comparison is expected:

if (i == 0) // that's what I should have written...

compare with

if (i = 0) // Oops! It still compiles and even "works"

compare with

if (0 = i) // won't compile

which might be a "normal" typo for

if (0 == i) // preferred by some - I find it difficult to read.

I find it hard to believe that if you remember to put the constant in
front of the variable you can't remember to put two '=' characters where
they need to be.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Re: A matter of style? by John

John
Thu Jul 20 11:23:57 CDT 2006

"Arnie" <none> wrote in message
news:%23A8TLbBrGHA.4492@TK2MSFTNGP05.phx.gbl
> Hi All,
>
> I have seen if tests written both of the ways shown below. Is
> this just considered a matter of personal style or do you feel
> there's some inherent advantage of doing it one way or the other?
> I typically do it the second way.
>
> if ( 0 == MyFunc( stuff) )
> {
> }
>
> if ( MyFunc( stuff ) == 0 )
> {
> }
>
> - Arnie

The standard argument in favour of the first version is that if you
accidentally use the assignment operator = rather than the equality operator
== , then the code will fail to compile, thereby alerting you to your
mistake.

In practice, VC++ will warn you if you use the assignment operator, so it
comes down to a matter of style.

--
John Carson



Re: A matter of style? by Bruno

Bruno
Thu Jul 20 13:49:20 CDT 2006

> I have seen if tests written both of the ways shown below. Is this just
> considered a matter of personal style or do you feel there's some inherent
> advantage of doing it one way or the other? I typically do it the second
> way.
>
> if ( 0 == MyFunc( stuff) )
> {
> }
>
> if ( MyFunc( stuff ) == 0 )
> {
> }

Igor, Victor and John already pointed out that it really is a matter of
style with modern C++ compilers.
I always put the contstant up front (0 == x). It is a habit, picked up in a
time when compilers did not always warn you.

Some of my customers still use older compilers (gcc 2.95, VC++6, older hp-ux
compilers that compile K&R C) These compilers are very easy going. The HP-ux
compiler for project on a legacy platform compiled virtually anything
without warnings. In cases like that, it is still a very useful habit.

But apart from that, I have been doing this for so long that I simply think
(0 == x) looks better than (x == 0).

--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"



Re: A matter of style? by Victor

Victor
Thu Jul 20 14:26:54 CDT 2006

Bruno van Dooren [MVP VC++] wrote:
>> [..]
> [..] I have been doing this for so long that I simply
> think (0 == x) looks better than (x == 0).

I know folks who have been drinking American coffee for so
long that they actually like it... :-)



Re: A matter of style? by Bruno

Bruno
Thu Jul 20 14:50:48 CDT 2006

> Bruno van Dooren [MVP VC++] wrote:
>>> [..]
>> [..] I have been doing this for so long that I simply
>> think (0 == x) looks better than (x == 0).
>
> I know folks who have been drinking American coffee for so
> long that they actually like it... :-)

Some people even think that american 'beer' is real beer.
They have obviously never visited Belgium.

--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"



Re: A matter of style? by Victor

Victor
Thu Jul 20 15:02:46 CDT 2006

Bruno van Dooren [MVP VC++] wrote:
>> Bruno van Dooren [MVP VC++] wrote:
>>>> [..]
>>> [..] I have been doing this for so long that I simply
>>> think (0 == x) looks better than (x == 0).
>>
>> I know folks who have been drinking American coffee for so
>> long that they actually like it... :-)
>
> Some people even think that american 'beer' is real beer.
> They have obviously never visited Belgium.

Don't get me started on chocolate... :-)



Re: A matter of style? by Mark

Mark
Thu Jul 20 16:02:22 CDT 2006

Doug Harrison [MVP] wrote:
> What, there's something better than Hershey's?!

(Cadburys Animal Bars > numeric_limits<float>::infinity()) == true

--
- Mark Randall
http://www.temporal-solutions.co.uk
http://www.awportals.com



Re: A matter of style? by Doug

Doug
Thu Jul 20 15:47:38 CDT 2006

On Thu, 20 Jul 2006 16:02:46 -0400, "Victor Bazarov"
<v.Abazarov@comAcast.net> wrote:

>Bruno van Dooren [MVP VC++] wrote:
>>> Bruno van Dooren [MVP VC++] wrote:
>>>>> [..]
>>>> [..] I have been doing this for so long that I simply
>>>> think (0 == x) looks better than (x == 0).
>>>
>>> I know folks who have been drinking American coffee for so
>>> long that they actually like it... :-)
>>
>> Some people even think that american 'beer' is real beer.
>> They have obviously never visited Belgium.
>
>Don't get me started on chocolate... :-)

What, there's something better than Hershey's?!

--
Doug Harrison
Visual C++ MVP

Re: A matter of style? by Bruno

Bruno
Thu Jul 20 16:28:16 CDT 2006

>>> I know folks who have been drinking American coffee for so
>>> long that they actually like it... :-)
>>
>> Some people even think that american 'beer' is real beer.
>> They have obviously never visited Belgium.
>
> Don't get me started on chocolate... :-)

Chocolate, Beer, Fries, ...
Belgium has it all.
Though I have to admit that you have to be in scotland for a good whisky.

Ah the taste of 32 year old Springbank.
Too bad the stuff costs 300 euros a bottle...
But there are loads of very good malts for a fraction of that price.
My ambition is to have had 1 bottle of each distillery in scotland by the
time I die.

--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"



Re: A matter of style? by Victor

Victor
Thu Jul 20 20:22:42 CDT 2006

Bruno van Dooren [MVP VC++] wrote:
> [..]
> My ambition is to have had 1 bottle of each distillery in scotland by
> the time I die.

Ya know matie, depending on the pace ya're going, yar liver may not be
able to sustain ya after ya've attained just a fraction of yar goal...



Re: A matter of style? by Alexander

Alexander
Thu Jul 20 22:47:59 CDT 2006


"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:gtqvb29krfud4repcgbka55qcnb2rtm81r@4ax.com...
> On Thu, 20 Jul 2006 16:02:46 -0400, "Victor Bazarov"
> <v.Abazarov@comAcast.net> wrote:
>
>>Bruno van Dooren [MVP VC++] wrote:
>>>> Bruno van Dooren [MVP VC++] wrote:
>>>>>> [..]
>>>>> [..] I have been doing this for so long that I simply
>>>>> think (0 == x) looks better than (x == 0).
>>>>
>>>> I know folks who have been drinking American coffee for so
>>>> long that they actually like it... :-)
>>>
>>> Some people even think that american 'beer' is real beer.
>>> They have obviously never visited Belgium.
>>
>>Don't get me started on chocolate... :-)
>
> What, there's something better than Hershey's?!
>

Try Dove Dark Chocolate ("Promises"). Milk one is also not bad.



Re: A matter of style? by Bruno

Bruno
Fri Jul 21 02:26:19 CDT 2006

>> My ambition is to have had 1 bottle of each distillery in scotland by
>> the time I die.
>
> Ya know matie, depending on the pace ya're going, yar liver may not be
> able to sustain ya after ya've attained just a fraction of yar goal...

It's not so bad, as long as I don't try to fullfil my ambition within a 1
year period ;-).

There are roughly between 100 and 200 distilleries (haven't actually counted
them).
I have already had bottles from 70 - 80 distilleries over the last 10 years.
(Note to self: I have to stuff this info in a database).
let's say I still need 100 or so bottles, and let's assume I live another 50
years (i'll be 80 by then)
that equates to only 2 bottles per year. Hardly a challenge.

Btw, my wife shares this hobby. Let's say that we buy & consume about 8
bottles per year.
8 bottles per year is only (roughly) 2 glasses per week per person.
With 50 years left, that is 400 potential bottles to empty.

The real challenge is that I am also interested (and very much so) in
whiskies from silent stills, i.e. distilleries that are out of business, but
still have stock left, or bottles in circulation.
As you can guess, these become increasingly harder to find, and are als more
expensive.

But it's an ambition / hobby, just like collecting stamps. Only you'd eat
the stamps as well as buying them :-)

--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"



Re: A matter of style? by Victor

Victor
Fri Jul 21 07:19:01 CDT 2006

Bruno van Dooren [MVP VC++] wrote:
> [..]
> But it's an ambition / hobby, just like collecting stamps. Only you'd
> eat the stamps as well as buying them :-)

Right, but you'd have those waxed envelops to display in your albums.
And the ones you get are much longer lasting envelops, right?