VB.NET boolean operators and short-circuit evaluation

… short-circuit evaluation is how the logical human brain works when confronted with chained boolean conditionals in real life

Having to pick up VB.NET for a Human Computer Interaction course this semester, I’ve been spending some time getting acquainted with the language through a couple of books. Every now and then I get somewhat bemused by how “different” VB syntax is - not that it’s a bad thing, pray. Syntax, as almost everyone agrees, is secondary to semantics.

Anyway, here’s a particularly bemusing bit of trivia you can use to taunt/jibe/provoke your VB.NET/VB programmer friends.

Did you know that VB.NET’s default boolean operators And and Or do not take advantage of short-circuit evaluation? Take for example this snippet of conditional code below:

Dim x as Integer 1
If (x < 5) Or (x > 10) Then

Now, you’d expect your run-of-the-mill compiler/interpreter to optimize by short-circuiting the 2nd boolean expression, since the 1st expression is already true (and so whether the 2nd boolean expression is true or false doesn’t really matter). For some reason, probably one steeped in history, VB.NET, or rather the JIT compiler, doesn’t short-circuit. It happily goes ahead and evaluates the 2nd boolean expression.

This is logically ludicrous - short-circuit evaluation is how the logical human brain works when confronted with chained boolean conditionals in real life. If the university says I can only get a First Class Honours degree if my CAP is greater than 4.5 AND get a minimum of A- for my thesis, I won’t really fret about not getting an A- for my thesis (and subsequently missing out on a yummy First Class Honours) when my CAP can never hit 4.5.

Now to be fair, there is an alternative OrElse operator which does perform short-circuit evaluation. It is not fair though to expect such an unorthodox operator to be something your common on-the-street (read: non-VB) programmer will expect to exist (unless he reads a VB.NET book and bothers to flip to the introductory chapters).

Come to think of it, it could be an intended design issue. Evaluating the 2nd boolean expression could be always warranted when it produces a side effect. That must be the reason why Microsoft introduced the OrElse and AndAlso operators and leaving the standard And and Or operators as non-short-circuiting to prevent confusing converting VB programmers.

So this then is actually an issue with VB. Hence, we can conclude that they deserve to be taunted/jibed/provoked. Spare the VB.NET programmers. For now.

13 Comments & TrackBacks (Add yours)

The paper doll icon that precedes each comment is an idea conceived by Vanessa Tan.

Paper doll icon
Vinnie's Gravatar

Now why won’t your teachers just let you use C#? ;)

In any case, I think Or is still around because that’s all that VB6 and earlier developers had. OrElse was just added in for VB.NET, and VB still has a lot of backward-compatible syntax that isn’t necessary with the .NET framework.

Posted by: Vinnie on January 27, 2004 2am

Paper doll icon
Dennis Pallett's Gravatar

I wish VBScript/ASP had OrElse and AndElse.

It’s slightly annoying to do:
If IsEmpty(Request.QueryString(”id”)) = False Then
If IsNumeric(Request.QueryString(”id”)) = True Then
‘The id is valid
Else
‘The id is not empty, but not numeric
End If
Else
‘The id is empty
End If

Instead of being able to do:
If IsEmpty(Request.QueryString(”id”)) = False And IsNumeric(Request.QueryString(”id”)) = True Then
‘The id is valid
Else
‘The id is either empty or not numeric
End If

(The second method would give an error of id contained letters)

Posted by: Dennis Pallett on January 27, 2004 6am

Paper doll icon
Cheah Chu Yeow's Gravatar

Vinnie: Good idea why didn’t I think of that earlier - now I’m far too committed to VB.NET because all the labs have been in VB.NET and I don’t really want to spend time on picking up C# (though it shouldn’t be too hard, I think). Besides, everyone else is using VB.NET and there’s a group project going.

Dennis: Slightly annoying? I think that is very annoying! You wouldn’t happen to know why VB was designed this way, would you?

Posted by: Cheah Chu Yeow on January 27, 2004 10pm

Paper doll icon
Vinnie's Gravatar

You’d pick up on C# fast; it’s a lot like Java, just with a different framework attached to it. I wouldn’t use it on your current project since everyone else would have no idea what’s going on in your code, but something to keep in mind in the future.

Posted by: Vinnie on January 27, 2004 11pm

Paper doll icon
Stephan Segraves's Gravatar

Wait, isn’t the whole point of .NET to allow the programmer to choose what language he wants to work with and be able to make group projects using that language? Kind of seems like your instructor is killing that idea…

Posted by: Stephan Segraves on January 27, 2004 11pm

Paper doll icon
Cheah Chu Yeow's Gravatar

Well, I can’t say we are forced to use VB.NET - I have a feeling the lecturer would be open to using C#. He has even allowed the use of Flash for the GUI project we’re doing.

Posted by: Cheah Chu Yeow on January 28, 2004 12am

Paper doll icon
Dennis Pallett's Gravatar

I haven’t got a clue why they designed it like this. I do know that I’m really enjoying PHP and its short-circuit evaluation though! :)

Posted by: Dennis Pallett on January 28, 2004 3am

Paper doll icon
Chadom's Gravatar

Agree with Dennis. In VBscript/ASP when get a value from data base, need two lines to test if it is a “valid” value :
if (not isnull(foo)) and foo>”" then

will crash if foo is null (second part is eval and null can’t be compare with a string). Very ugly.

Posted by: Chadom on January 29, 2004 4am

Paper doll icon
Danny's Gravatar

VB.NET’s non-short circuit boolean expresssions hark back partly to when user defined functions were starting to be used. If you have a boolean expression that short circuits and its made up of functions then

“if boolfooa() or boolfoob() then
….”

wont eval boolfoob() if boolfooa() returns true. I think (?) thats the main use.

Posted by: Danny on November 8, 2006 2am

Paper doll icon
sonali's Gravatar

i want to write a code in VB.NEt in 2003 that will sync my outlook events & contacts list to my application.

Kindly Suggest various solution for the same..

thanx

Posted by: sonali on March 26, 2007 8pm

Paper doll icon
Dot Net Books's Gravatar

Since most developers using VB.NET used earlier versions of VB, and most new developers will probably use C#, doesn’t it make sense to leave the way it operates?

Posted by: Dot Net Books on April 15, 2007 9pm

Paper doll icon
David Tan's Gravatar

You are definately not the first to find this a bit bizarre. Having no VB background and coming from other language makes this AndAlso / OrElse sounds a bit clumsy, and funny :)

Posted by: David Tan on December 17, 2007 9am

Paper doll icon
dean's Gravatar

its annoying, but the reason is an old one. historically logical operators in vb are also used for bitwise expressions - they should have broken this when creating vb.net, but i guess they were worried about code conversions

Posted by: dean on February 27, 2008 6pm

You can subscribe to the RSS feed for comments on this post.

Post a comment

(required)

(required, but never displayed)


You can format your comments using XHTML. Your email address will not be displayed or used for nefarious purposes.

Only following tags are allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>