I get inspiration for blog posts from a number of places. Sometimes from Twitter (see yesterday’s post) and sometimes for other blogs. Actually other blogs are a constant and regular source of inspirations. Today’s post was inspired by a Programming Pop Quiz posted by Rob Miles. The sample code he uses works because of what is called short circuiting. Let me show you the code and explain it in plain English (or what passes as plain English from me.)
while (startPos < input.Length && input[startPos] != '"') startPos++;
This is a while loop that first checks to see if startPos is less than the length of input. One assumes that input is a string array based on the context. The expression then check to see if the string in that location (indicated by startPos) is not equal to a double quote. Assuming that startPos is less than input.Length AND that the indicated character is not a double quote the value of startPos is incremented and the loop continues. Clear?
Now here is where the short circuit comes in. If the value of startPos is greater than or equal to the length of input and the computer were to execute the second comparison there would be an error. Why? Because the program would be attempting to access a location that is not defined and that is outside the range of the array. This is what we call “a bad thing.” However, and good for us, if the first comparison evaluates to false the computer “knows” that it does not need to do the second comparison and so it doesn’t. This is a good time to review Truth tables if you are confused about why we don’t need the second comparison if the first is false.
In Visual Basic this gets a little interesting though. The C# code (or is it C++ or Java or some other C-family language? Does it matter?) converts to the following in Visual Basic .NET.
While startPos < input.Length AndAlso input(startPos) <> """"C
startPos += 1
You’ll notice that rather than And the operator that is used is AndAlso. What’s up you may be asking. Doesn’t VB have a keyword And? Yes it does. AndAlso is new though. In Visual Basic 6.0 and earlier there was only the And operator. The And operator did not short circuit. What that means is that the code above would not work the same way as the C#/C++/Java example if the And operator was used. You could (probably would) run into the unwanted exception error. Initially in the first beta versions of Visual Basic .NET the And operator was changed to short circuit just as && does. It seemed like a good idea. And in theory it was. The problem was that there was a lot of old code out there that depended on And not doing a short circuit. Ultimately it was decided to add the AndAlso command to do short circuit operations. By the way this is also why there is an Or and OrElse operator in VB .NET. Paul Vick of the Visual Basic design team explains their reasoning on his blog. He also explains how C-family languages had two operators where VB had one.
This is one of those issues that often comes up when there is an installed base of users. Its not always an easy answer. What do you think? Or your students think. Did the VB team make the correct decision? Why or why not?