My discovery of short-circuit logic . . . Or ‘how can a C# programmer screw up the obivous?’D

After I was laid off from Omnicell, I took a job as a contractor at Hess (Thanks Guys!) doing Visual Basic and Silverlight development. I found I rather enjoyed working with, and that for some mysterious reason they had difficulty finding developers that could do the work. Some of it had to do with a misplaced conceit regarding visual basic, c# and some notion of inferiority! I found that is very expressive, and the version that runs against 4.0 CLR is every bit as good as C#. This was MS’s desire, to ensure that the language enjoyed parity with it’s C# sibling. I think they’ve done a great job with it . . . but doesn’t mean that all is hunky dory with for c# programmers – for sure! I (re-)discovered the horrors of eager vs short-circuit expression operators first hand!

My work involved working on creating new code, and rewriting much of their old database I/O code one an application that interfaced with their Documentum system. Since it had been a while since I created solutions using basic or pascal, I went my merry way believe that ‘and’ is ‘&&’ and ‘or’ is ‘||’ . . . not so! So what is also blather about short-circuit logic? Here’s an example:

if cheap_to_check() = true and expensive_to_check() = true then
end if


Well let’s see that should be like this in C# – right?

if(cheap_to_check() == True && expensive_to_check() == True)

(there might be a syntax error or two here . . . but you get the point!)

These two if conditional statements are NOT semantically equivalent! I was attempting to code an often used pattern that would use an inexpensive operation to determine if a more expensive operation would be executed.

I use to expect that with both of these statements, if the cheap_to_check() returned false, the conditional code would not execute no matter what. This is not the case. C#, C, C++ and other languages of that ilk exhibit by default the notion of short-circuit operator logic. Its is as if if that statement reads ‘hey lets not bother with doing that expensive operation because we already know that the cheap operation returned false . . . let’s just move on!” . . . Unfortunately with the Visual Basic Dot Net the execution of the function ‘expensive_to_check()’ will occur no matter what. Visual Basic Dot Net and it’s brethren use eager operators – all expressions are evaluated on that if statement line.

We now introduce the short-circuit and/or operators for visual basic – AndAlso and OrElse . . these are semantically equivalent operators to the C languages ‘&&’ and ‘||’. So rewriting the above expresions like this does the trick:

if cheap_to_check() = true AndAlso expensive_to_check() = true then

end if

The same is to be the said for the Or (eager operator) and the OrElse (short circuit operator). No matter what, all expressions that are connected by an ‘or’operator will be evaluated (unless of course, an exception occurs within one of the expressions).

Well . . .that’s all for now folks . . . next time (whenever that is!) I’ll explore the deep mysteries of the VB.Net ‘Not’, ‘IsNot’, and ‘Nothing’ operators . . . its not a not like we C programmers know, and ‘IsNot Nothing’ is not just metaphysical . . . it’s the real deal!



Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


No comments yet.

Leave a comment