VBA doesn't short-circuit
VBA does not support short-circuiting - apparently because it only has bitwise And/Or/Not etc operations. From the VBA language specification: "Logical operators are simple data operators that perform bitwise computations on their operands." In this light, it makes sense that VBA was designed with true = &H1111
and false = &H0000
: this way logical statements can be evaluated as bitwise operations.
The lack of short-circuiting can cause problems
Performance: the
ReallyExpensiveFunction()
will always be run when this statement is evaluated, even if it is not necessary by the result of the left hand side of the conditionIf IsNecessary() And ReallyExpensiveFunction() Then '... End If
Errors: if MyObj is Nothing, this conditional statment will result in a runtime error because VBA will still try to check the value of
Property
If Not MyObj Is Nothing And MyObj.Property = 5 Then '... End If
The solution I've used to implement short-cirtcuiting behavior is nested If
s
If cond1 And cond2 Then
'...
End If
Becomes
If cond1 Then
If cond2 Then
'...
End If
End If
This way the If statements give the short-circuit-like behavior of not bothering to evaluate cond2
if cond1
is False
.
If there is an Else clause, this creates duplicate code blocks
If Not MyObj Is Nothing And MyObj.Property = 5 Then
MsgBox "YAY"
Else
MsgBox "BOO"
End If
Becomes
If Not MyObj Is Nothing Then
If MyObj.Property = 5 Then
MsgBox "YAY"
Else
MsgBox "BOO" 'Duplicate
End If
Else
MsgBox "BOO" 'Duplicate
End If
Is there a way to rewrite If
statements to preserve the short-circuit behavior, but avoid duplication of code?
Perhaps with another branching statement like Select Case
?
To add context to the question, here is the specific case I'm looking at. I'm implementing a hash table that handles collisions by chaining them in a linked list. The underlying array size is enforced to be a power of two and the hashes are distributed into the current array size by truncating them to the appropriate length.
For example, suppose the array length is 16 (binary 10000). If I have a key that hashes to 27 (binary 11011), I can store it in my 16 slot array by keeping only the bits within the limit of that array size. The index where this item would be stored is (hash value) And (length of array - 1)
which in this case is (binary 11011) And (1111)
which is 1011
which is 11. The actual hash code is stored along with the key in the slot.
When looking up an item in the hash table in a chain, both the hash and the key must be checked to determine that the correct item has been found. However, if the hash doesn't match, then there is no reason to check the key. I was hoping to gain some tiny intangible amount of performance by nesting the Ifs to get the short-circuit behavior:
While Not e Is Nothing
If keyhash = e.hash Then
If Key = e.Key Then
e.Value = Value
Exit Property
Else
Set e = e.nextEntry
End If
Else
Set e = e.nextEntry
End If
Wend
You can see the Set...
is duplicated, and thus this question.
SET
line outside the ifs (and inside the while before the bottom line)? You don'tSET
only when you exit, otherwise you do set. Good question by the way! – Vizierate