Why Option Strict?Pseudo System Modal Dialog

AndAlso and OrElse

May 10th, 2007

Short circuits are good in VB.NETVB.NET has two new conditional operators, AndAlso and OrElse, that implement short-circuiting. Short-circuiting optimizes performance by stopping the processing of a complex comparison without having to evaluate every condition in the list. How can we use it to improve the performance of our applications?

As you may know, if you have a line of code like this

If CheckCondition1() And CheckCondition2() Then

Both of the functions are called, even if this isn’t necessary. The same is true with OR

If CheckCondition1() Or CheckCondition2() Then

Both functions are called although if the first one returns True there is no need to call the second one.

The short-circuit operators allow us to avoid calling unnecessary functions when we’re calling functions or testing conditions. For example, lets assume that we want a True from both our conditional checks but if one fails we don’t want to execute the code in the If block. The code using AndAlso would look like this:

If CheckCondition1() AndAlso CheckCondition2() Then

If the CheckCondition1 function returned a False CheckCondition2 would never be called and we would not have the additional overhead of calling a second function. Likewise, if we wanted to continue into the If block if one or both of the conditions were True, we could code it like this:

If CheckCondition1() OrElse CheckCondition2() Then

In this case a True from either function allows the code to execute but if the first condition passes then we don’t care about the result of the second check and, therefore, we don’t need to execute it.

One area where this kind of checking can come in handy is when we want to check for the existence of an object and then check a property on that object before continuing. If we wrote the code this way:

If MyObject IsNot Nothing And MyObject.Test = True Then

We would get an exception if MyObject was Nothing since the first part of the condition would execute but the second part wouldn’t work against the non-existent object. Instead we could write it this way:

If MyObject IsNot Nothing AndAlso MyObject.Test = True Then

In this case if the Object was Nothing, the first condition would return a False so the rest of the conditions on the line wouldn’t execute against a Nothing object thus causing an exception.

Likewise, OrElse can be used in situations where we only care if one item is True. For example, we may want to execute the code if one property out of several is true, as in this example:

If Test.Length > 4 OrElse Test.EndsWith(".") OrElse Test.Contains("test") OrElse Test.StartsWith("|") Then
.
.
.

In this case we’re only concerned about the string variable ‘Test’ passing one of the tests. So, we start with the most commonly passed test, which is length in our scenario, and work our way down to the least common, starting with a pipe character, to do our conditional tests.

Of course, our old friends And and Or still have a place when we want both conditions to be executed. But, if you don’t need to execute both conditions, improve the performance of your application by using AndAlso and OrElse.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (No Ratings Yet)
Loading ... Loading ...

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives