468,309 Members | 1,100 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,309 developers. It's quick & easy.

Is Short-circuiting always a good idea?

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
5 1103
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
Cool - that's good to know. Appreciate the advice David.
===
Phil
Nov 21 '05 #3

"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
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
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.

Similar topics

8 posts views Thread by Ken Dopierala Jr. | last post: by
7 posts views Thread by LaMoRt | last post: by
15 posts views Thread by Steffen Loringer | last post: by
4 posts views Thread by slougheed | last post: by
10 posts views Thread by Jim Langston | last post: by
17 posts views Thread by spasmous | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.