By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,352 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,352 IT Pros & Developers. It's quick & easy.

Is Short-circuiting always a good idea?

P: n/a
I'm just (as a matter of course) using AndAlso and OrElse statements to
short circuit checking, even for nominal things like Boolean comparisons.
I'm wondering, is that a good thing to do (for performance reasons) or is
there some hidden down-side to it.

I can see why it's good for say not checking an object ref if it doesn't
exist - I'm just wondering if it's a good habit to get into for everything.

Thanks for any insights.
===
Phil
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 2004-12-09, Phil Jones <ph************@hotmail.com> wrote:
I'm just (as a matter of course) using AndAlso and OrElse statements to
short circuit checking, even for nominal things like Boolean comparisons.
I'm wondering, is that a good thing to do (for performance reasons) or is
there some hidden down-side to it.

I can see why it's good for say not checking an object ref if it doesn't
exist - I'm just wondering if it's a good habit to get into for everything.


IMHO, it's a good habit for everything boolean. There's no downside. I
tend to think of AndAlso/OrElse as the boolean operators, while And/Or
are only for bitwise operations.

In fact, I can't remember the last time I used And/Or in a piece of
code.

Nov 21 '05 #2

P: n/a
Cool - that's good to know. Appreciate the advice David.
===
Phil
Nov 21 '05 #3

P: n/a

"Phil Jones" <ph************@hotmail.com> wrote
I'm just (as a matter of course) using AndAlso and OrElse statements to
short circuit checking, even for nominal things like Boolean comparisons.
I'm wondering, is that a good thing to do (for performance reasons) or is
there some hidden down-side to it.

I can see why it's good for say not checking an object ref if it doesn't
exist - I'm just wondering if it's a good habit to get into for everything.


Not only should you look into using short circuiting when you can, but
you must also place the operands in the proper order to gain the full
benefits from using short circuit evaluation.

Consider running this test every hour: (pseudo code)

If Now.Time = Noon AndAlso Now.Day = LeapDay Then ...

LeapDay (Feb 29) happens once every 4 years, but Noon happens once
each day. That means the second part (LeapDay) is tested at noon
every day, 365 days per year. for 4 years before the entire condition is
True.

But the reverse gains more benefit from the short circuit operator:

If Now.Day = LeapDay AndAlso Now.Time = Noon Then ...

In that siuation the second part is only tested 24 times, when LeapDay
has arrived.

That is an exaggerated situation, but it helps in pointing out that the
less frequent condition(s) need to be tested first, for optimal performance.

LFS
Nov 21 '05 #4

P: n/a
Larry,

With a leap month it is even more in some cultures

http://www.hermetic.ch/chcal/leap_months.htm

:-)

Cor
Nov 21 '05 #5

P: n/a
Phil,
There was a long discussion on short circuiting in this newsgroup a number
of months ago.

See the following thread.
http://groups-beta.google.com/group/...86fda008e54916

Hope this helps
Jay

"Phil Jones" <ph************@hotmail.com> wrote in message
news:eI**************@tk2msftngp13.phx.gbl...
I'm just (as a matter of course) using AndAlso and OrElse statements to
short circuit checking, even for nominal things like Boolean comparisons.
I'm wondering, is that a good thing to do (for performance reasons) or is
there some hidden down-side to it.

I can see why it's good for say not checking an object ref if it doesn't
exist - I'm just wondering if it's a good habit to get into for
everything.

Thanks for any insights.
===
Phil

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.