From: Tom Shelton on
Paul Clement was thinking very hard :
> On Mon, 19 Jul 2010 21:28:37 -0400, "Kevin Provance" <k(a)p.c> wrote:
>
> � Hey C++ guys, what is the advantage or purpose of this?
> �
> � If (FALSE == SomeFunction())
> � {
> � ...
> � }
> �
> � Why put FALSE == instead of the function first, like we usually do?
>
> Looks like the Yoda School of Programming to me. Coded properly, the
> comparison to FALSE isn't even necessary.
>

It's C++ code.... Older C++ didn't define a bool type or have any
concept of a boolean value. That mades these types of comparisons
necessary, and the reverse test was to prevent the old bug of
accidently doing an assignment in the test.

--
Tom Shelton


From: DanS on
>> � Why put FALSE == instead of the function first, like we
>> usually do?
>>
>> Looks like the Yoda School of Programming to me. Coded
>> properly, the comparison to FALSE isn't even necessary.
>>
>
> It's C++ code.... Older C++ didn't define a bool type or
> have any concept of a boolean value. That mades these
> types of comparisons necessary, and the reverse test was to
> prevent the old bug of accidently doing an assignment in
> the test.

Since when is a programmer's own deficiency a bug ?

I've made that error in C, and never blamed anyone but myself.

From: Tom Shelton on
DanS formulated on Tuesday :
>>> � Why put FALSE == instead of the function first, like we
>>> usually do?
>>>
>>> Looks like the Yoda School of Programming to me. Coded
>>> properly, the comparison to FALSE isn't even necessary.
>>>
>>
>> It's C++ code.... Older C++ didn't define a bool type or
>> have any concept of a boolean value. That mades these
>> types of comparisons necessary, and the reverse test was to
>> prevent the old bug of accidently doing an assignment in
>> the test.
>
> Since when is a programmer's own deficiency a bug ?
>

A bug is any incorrect behavior in the program - which in this old case
maybe the result of a typo or as you say, programmer deficiency. What
does this actualy have to do with my response?

> I've made that error in C, and never blamed anyone but myself.

As have I. The idea of the reverse test, and why it is actually a
common C/C++ idiom is because it helps prevent bugs. It's called
defensive coding... It turns a simple typo into a compilation error.

More modern languages usually design there conditional statements to
only work on boolean type values - so an accidental assignment is
always caught by the compiler.

--
Tom Shelton


From: Karl E. Peterson on
Kevin Provance explained on 7/19/2010 :
> "Karl E. Peterson" <karl(a)exmvps.org> wrote in message
> news:i22va8$qjs$1(a)news.eternal-september.org...
>>
>> Are you testing for true or false? If you put the False up front, it's
>> really clear when you make an exception to the normal situation.
>
> I'm converting some C++ code to VB (specifically dealing with CAB files) and
> I noticed the call was...reversed. I was just curious if there was a
> specific reason why. Tis all. :-)

Heh, actually, that question was directed at the code snippet that you
snipped. <g> Rewinding...

"Karl E. Peterson" <karl(a)exmvps.org> wrote in message
> But what if the function has eight or nine parameters, and you end up with:
>
> If SomeFunction(Byval aLongVarName, ByVal SomeMoreData, ByVal Blah,
> By<clipped>
>
> Are you testing for true or false? If you put the False up front, it's
> really clear when you make an exception to the normal situation.

The <clipped> there was supposed to represent the right-edge of the
code window. IOW, you are unable to read the entire line of code,
because it's wider than your window.

In *most* cases, you'd simply be testing for a non-zero (true) return,
so there'd be no need to read it all. But what if you want to test for
a 0 (false) return?

If you put the =0 or =False at the end of the line, it could easily be
out of view and not intuitively noticeable. That would be a
justification for putting this less common test up front, IMO.

YMMV, of course. :-)

--
..NET: It's About Trust!
http://vfred.mvps.org


From: Paul Clement on
On Tue, 20 Jul 2010 09:02:19 -0600, Tom Shelton <tom_shelton(a)comcast.invalid> wrote:

� Paul Clement was thinking very hard :
� > On Mon, 19 Jul 2010 21:28:37 -0400, "Kevin Provance" <k(a)p.c> wrote:
� >
� > � Hey C++ guys, what is the advantage or purpose of this?
� > �
� > � If (FALSE == SomeFunction())
� > � {
� > � ...
� > � }
� > �
� > � Why put FALSE == instead of the function first, like we usually do?
� >
� > Looks like the Yoda School of Programming to me. Coded properly, the
� > comparison to FALSE isn't even necessary.
� >

� It's C++ code.... Older C++ didn't define a bool type or have any
� concept of a boolean value. That mades these types of comparisons
� necessary, and the reverse test was to prevent the old bug of
� accidently doing an assignment in the test.

So let me get this straight...

"If (FALSE)" in C++ evaluates to True or is it an invalid statement? I haven't work with an older
C-language in over 25 years so I can't recall how this functions.

It's still backwards with respect to readability. ;-)


Paul
~~~~
Microsoft MVP (Visual Basic)
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13
Prev: Displaying property menu at runtime
Next: Delay Methods