Why is this an issue?

Short-circuit evaluation is an evaluation strategy for Boolean operators, that doesn’t evaluates the second argument of the operator if it is not needed to determine the result of the operation.

VB.NET provides logical operators that implement short-circuit evaluation: AndAlso and OrElse, as well as non-short-circuit versions: And and Or. Unlike short-circuit operators, non-short-circuit ones evaluate both operands and afterwards perform the logical operation.

For example False AndAlso FunctionCall always results in False, even when FunctionCall invocation would raise an exception. Instead, False & FunctionCall also evaluates FunctionCall, and results in an exception if FunctionCall raises an exception.

Similarly, True OrElse FunctionCall always results in True, no matter what the return value of FunctionCall would be.

The use of non-short-circuit logic in a boolean context is likely a mistake - one that could cause serious program errors as conditions are evaluated under the wrong circumstances.

How to fix it

Code examples

Noncompliant code example

If GetTrue() Or GetFalse() Then ' Noncompliant: both sides evaluated
End If

Compliant solution

If GetTrue() OrElse GetFalse() Then ' Compliant: short-circuit logic used
End If

Resources

Documentation

Articles & blog posts