title: VBA Short-Circuit `And` Alternatives
body: <p><strong>VBA doesn't short-circuit</strong></p>
  
  <p>VBA does not support short-circuiting - apparently because it only has bitwise And/Or/Not etc operations.  From the <a href="http://msdn.microsoft.com/en-us/library/ee156946.aspx">VBA language specification</a>: "Logical operators are simple data operators that perform bitwise computations on their operands."  In this light, it makes sense that VBA was designed with <code>true = &amp;H1111</code> and <code>false = &amp;H0000</code>: this way logical statements can be evaluated as bitwise operations.</p>
  
  <p><strong>The lack of short-circuiting can cause problems</strong></p>
  
  <ol>
  <li><p>Performance: the <code>ReallyExpensiveFunction()</code> 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 condition</p>
  
  <p><code>If IsNecessary() And ReallyExpensiveFunction() Then
      '...
  End If</code></p></li>
  <li><p>Errors: if MyObj is Nothing, this conditional statment will result in a runtime error because VBA will still try to check the value of <code>Property</code></p>
  
  <p><code>If Not MyObj Is Nothing And MyObj.Property = 5 Then
      '...
  End If</code></p></li>
  </ol>
  
  <p><strong>The solution I've used to implement short-cirtcuiting behavior is nested <code>If</code>s</strong></p>
  
  <pre><code>If cond1 And cond2 Then
      '...
  End If
  </code></pre>
  
  <p>Becomes</p>
  
  <pre><code>If cond1 Then
      If cond2 Then
          '...
      End If
  End If
  </code></pre>
  
  <p>This way the If statements give the short-circuit-like behavior of not bothering to evaluate <code>cond2</code> if <code>cond1</code> is <code>False</code>.</p>
  
  <p><strong>If there is an Else clause, this creates duplicate code blocks</strong></p>
  
  <pre><code>If Not MyObj Is Nothing And MyObj.Property = 5 Then
      MsgBox "YAY"
  Else
      MsgBox "BOO"
  End If
  </code></pre>
  
  <p>Becomes</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>Is there a way to rewrite <code>If</code> statements to preserve the short-circuit behavior, but avoid duplication of code?</strong></p>
  
  <p>Perhaps with another branching statement like <code>Select Case</code>?</p>
  
  <hr>
  
  <p>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.  </p>
  
  <p>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 <code>(hash value) And (length of array - 1)</code> which in this case is <code>(binary 11011) And (1111)</code> which is <code>1011</code> which is 11.  The actual hash code is stored along with the key in the slot.  </p>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>You can see the <code>Set...</code> is duplicated, and thus this question.</p>