Short circuiting and a short temper

I’ve been working with Visual Basic for Applications for a few days again, picking up where I left off last summer. I just remembered one of the things I particularly hate about VB6/A: it doesn’t allow short circuit evaluation.

Short circuiting is when a code flow conditional (Conditional of an If, WHILE, etc.) is only evaluated as much as it needs to be to determine truth. The main thing to remember is that expressions can contain a function call.

Ex1 AND Ex2: if Ex1 is false, won’t evaluate Ex2
Ex1 OR Ex2: if Ex1 is true, won’t evaluate Ex2

It doesn’t sound particularly fantastic when stated so simply, but most other languages do implement it by default on their control structures, or otherwise have a method to do so. VB does not (although I believe .NET does).

My main issue is one of performance. Working with Excel through VBA means you are manipulating a lot of a data in a suboptimal way, especially converting to and from Strings. There is a lot of overhead involved here! In many cases it isn’t a problem and the user won’t notice the inefficiencies (although the “programmer” – me – will, but that’s just the OCD talking) but in some instances those small overheads accumulate – for example, when you’re crunching through several thousand rows of input. It’s mostly bothersome for validating input on the fly – an example would be this ugly IF:

If IsNumeric(latCell.value) And IsNumeric(latCell.Offset(0, 1).value) _
And Not IsEmpty(latCell) And Not IsEmpty(latCell.Offset(0, 1)) Then
     '---Calculate the value
End If

The above makes sure that two inputs aren’t strings or blank cells (blank cells in VBA are “numeric” when their value is taken, which is a whole other problem that I won’t get in to). Ignoring my failure to use variables, do you see a problem? Even if the first cell isn’t numeric (and therefore we won’t be able to do the calculation) the language still evaluates every other expression, wasting time it could use to do something actually useful.
Another point that comes up often is testing properties or objects. When VB tries to test an object that hasn’t been initialized, it throws a (fatal, by default) error. It will also error when an object of type Variant doesn’t currently have the specified property.


Here is an example that I’ve encountered: verifying that Selection contains valid data. I want to make sure that Selection is an object of type Range and also that, if it is a Range, it has only two columns.

If Not TypeName(Selection) = "Range" or Selection.Columns.Count = 2 Then
     MsgBox ("Please select a valid range of pairs")
     Exit Sub
End If

The above will throw an error if the selection isn’t a Range because it will always attempt to count columns. In a short-circuiting language this wouldn’t be an issue. The workaround in VB is to have two separate ifs, or use On Error Resume Next to ignore the error.

In other languages you can actually optimize execution by ordering the expressions in order of likelihood. In VB it doesn’t matter at all – every expression will be evaluated.

So why would you not use short circuiting? It seems pretty obvious that it is beneficial. Well, I haven’t been totally impartial in my presentation. There is a reason why a language wouldn’t implement short circuiting: consistency and (pseudo)determinism.

It is guaranteed that the code will behave exactly the same way every time regardless of the input. People will, either in ignorance or in an attempt to be clever or because of a hatred of fellow programmers*, insert function calls that have side effects into conditionals. When a language short circuits its conditionals it is trickier to track or predict what functions will be called. VB is designed to be used by people with little traditional programming experience and makes it clear its goal is to simplify programming.

In my use of languages, short circuiting is far more beneficial than not and it is frustrating to not have access to it.

*inserting side-effect containing functions into conditionals (especially short-circuited ones) is a form of code obfuscation. Its practice is frowned upon and is definitely not kosher.

0 things about

Short circuiting and a short temper

Post a Comment

Copyright 2012 Phile not Found. See About
Powered by Blogger

"Whenever you find that you are on the side of the majority, it is time to pause and reflect."