Rails, Firefox, Anime, Mac
In: Programming
25 Jan 2004Having 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:
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 Responses to VB.NET boolean operators and short-circuit evaluation
Dennis Pallett
January 28th, 2004 at 3am
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! :)
Chadom
January 29th, 2004 at 4am
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.
Cheah Chu Yeow
January 28th, 2004 at 12am
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.
Stephan Segraves
January 27th, 2004 at 11pm
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…
Vinnie
January 27th, 2004 at 11pm
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.
Cheah Chu Yeow
January 27th, 2004 at 10pm
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?
Dennis Pallett
January 27th, 2004 at 6am
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)
Vinnie
January 27th, 2004 at 2am
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.
Danny
November 8th, 2006 at 2am
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.
sonali
March 26th, 2007 at 8pm
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
Dot Net Books
April 15th, 2007 at 9pm
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?
David Tan
December 17th, 2007 at 9am
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 :)
dean
February 27th, 2008 at 6pm
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