Short Circuit Logic

Cerin

New member
Joined
Oct 6, 2008
Messages
2
Programming Experience
Beginner
Hi,

I'm new to VB.Net, and I was surprised to find that the default AND/OR operators don't use short circuited logic. I noticed that true short circuit operators were recently added to the language, but does anyone know why these weren't used initially? Is there any benefit to computing parts of an expression that are logically irrelevant?
 
Is there any benefit to computing parts of an expression that are logically irrelevant?
No, but how can the computer know what is irrelevant if you don't tell it what is what? Computers doesn't do what you would like them to do by wishful thinking or follow some rational thought of intention, they do only what you explicitly tell them to. The logical operators like AND/OR only combine the Boolean values that is the result of each expression, they don't and can't evaluate if part of a previous expression could deem the next expression redundant. Simply True And True = True, True And False = False. There is no way for compiler to deduct from the first "True" that the underlying expression for example contains a string that would lead the next expression to fail or be unnecessary to evaluate. The explicit short circuit operators is how you tell the computer in which cases it should evaluate the various expression parts.
anyone know why these weren't used initially?
Why didn't MS released .Net 3.5 right away in 2002? Why did they have to use all this time to build up the current libraries and current language features?
 
No, but how can the computer know what is irrelevant if you don't tell it what is what?

It's actually quite simple. Consider the expression:

goToThePark = notRaining() AND weHaveShoes() AND noWork()

If notRaining() returns False, it doesn't matter what the other values equate to. False ANDed with anything is still false. With "short circuit" logic, this first False would stop the computer from wasting time on the remaining irrelevant computations.
 
It's simply not what the AND operator does :) To do what you are asking you have to use the ANDALSO operator. What if you did this:

goToThePark = NOT (IsRaining AND IsBusy)

If that short circuited the expression would fail its intension in two out of three cases. (if you don't go to park when only you're busy and it's raining)
 
Hi,

I'm new to VB.Net, and I was surprised to find that the default AND/OR operators don't use short circuited logic. I noticed that true short circuit operators were recently added to the language, but does anyone know why these weren't used initially? Is there any benefit to computing parts of an expression that are logically irrelevant?
I'll just jump in from the beginning post. MS did include a short circuited AND from the beginning (back in 2002) it's the 'AndAlso' keyword. The reason for this is because AND in vb6 and older (all the way back to BASIC back in the 1960's/1970's) was already well known and since there's a lot of people coming into .Net from vb6 MS included the AndAlso for short circuiting so that the class vb AND would still work the same (not open the possibility of code breaking)

In short: yes MS included a short circuit AND from the beginning, you just didn't know about it till now.
 
Back
Top