Snippet Tutorial - Short Circuiting

Skill

Snippet Tutorial - Short Circuiting

Posted in:

Typically here at Switch On The Code, we cover something that is language specific, focusing on syntax solutions that help you with your projects. Today we are going to step back a bit and talk about a feature of programming that I think is neat and worthy of talking about. This feature is known as Short-Circuiting, and it may elude some of you beginners out there.

Perhaps we should begin with an explanation. Short-Circuiting is a term used to describe logical operators that only evaluate if the first does not. For example, lets take the following PHP snippet:

$one = true;
$two = false;

if($one == null && !$two)
  echo "GO IF!";

In the context of short-circuiting, the second logical test would never get evaluated because the first one in the line already failed. Variable $two doesn't even have to exist in this example, because it is technically never used.

You may be asking yourself "Why would I need this?" and in some languages don't even offer this feature. Trust me though, it is extremely useful when you are dealing with large systems, with highly dynamic data. In the example above, it is clear that if you have short-circuiting available, it can be used when one variable relies on another. My favorite example of this is when you are using a Dataset, because it is very clear why you would use something like this:

DataSet data = GetSomeData();

if(data.tables.count > 0 && data.tables(0).rows.count > 0)
{
  //Do Something with data...
}

Normally you would never see something like this, mainly because if there are no tables, then there is certainly no rows in table 0. However, since we used short-circuiting operators, we can make both tests in one if statement. If there are no tables in the dataset, then it will "short-circuit" and break from the if statement. This way we can order our logical tests so that one can depend on the other evaluating.

It is an extremely useful feature that comes in handy more than it seems like at first. Now the only thing we need to know is what languages support short-circuiting. This is quickly remedied from a quick look at the wikipedia page provided in the references below.

One language that makes is quite obvious that is supports short-circuiting is the classic vb. The normal logical operators are And and Or, while the short-circuiting operators are AndAlso and OrElse. Most other languages use && and || as their short-circuiting operators. In the end you may just have to do some research to find out if your preferred language supports it.

So this is going to wrap it up for this tutorial. I hope you have been enlightened, and are learning about it before you need it. Just remember, when you need coding help, all you have to do is Switch On The Code.

Dev Net
08/11/2011 - 23:34

Thanks, I was looking at some of my code when I saw a few of these and then changed that to use short-circuiting (in c#)

bool display = sections != null ? sections.Display : false;

to
bool display = sections != null && sections.Display;

reply

The Hairiest
08/12/2011 - 10:30

Yeah, short-circuiting has been extremely valuable in my day-to-day programming. It not only saves on a few extra lines, but it makes you step back and think about the order in which you do things.

reply

The Fattest
08/12/2011 - 10:30

Awesome glad we could help.

reply

The Fattest
08/12/2011 - 10:25

Cool post.

reply

KG
08/18/2011 - 04:32

Note that if language allows short-circuiting, it probably provide a way to not use short-circuiting (it may be useful if you want all operators to be evaluated, whatever the result of previous tests is).

In C#, if you don't want to use the short-circuits, use the "single" version of the operator (& instead of &&, | instead of ||, etc.)

reply

Tj3
11/06/2011 - 09:40

Awesome! I've actually always used the && operator in C#, but now that I realized how short-circuiting works, I've noticed I've been using without even thinking about it on a day-to-day basis!

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.