Or versus OrElse

Viewed 72627

What's the difference between or and OrElse?

if temp is dbnull.value or temp = 0

produces the error:

Operator '=' is not defined for type 'DBNull' and type 'Integer'.

while this one works like a charm!?

if temp is dbnull.value OrElse temp = 0
8 Answers

OrElse is a short-circuiting operator, Or is not.

By the definition of the boolean 'or' operator, if the first term is True then the whole is definitely true - so we don't need to evaluate the second term.

OrElse knows this, so doesn't try and evaluate temp = 0 once it's established that temp Is DBNull.Value

Or doesn't know this, and will always attempt to evaluate both terms. When temp Is DBNull.Value, it can't be compared to zero, so it falls over.

You should use... well, whichever one makes sense.

OrElse is short circuited, this means that only one side of the expression will be tested if the first side is a match.

Just like AndAlso will only test one side of the expression if the first half is a fail.

(I've looked at other answers and realized I was terribly wrong)

The OrElse operator "performs short-circuiting logical disjunction on two expressions", that is to say: if the left operand is true and so the entire expression is guaranteed to be true the right operand won't even be evaluated (this is useful in cases like:

string a;
//...
if (a is null) or (a = "Hi") //...

to avoid a NullReferenceException throw by the right-hand operand.

I'm sincerely astonished that this (lazy evaluation) isn't the default behaviour of or and and as it is in C/C++ and C# (and many other languages...)

The reason the compilation fails in the example is the order of operations.

The expression parser is trying to evaluate "dbnull.value or temp" first.

if temp is (dbnull.value or temp) = 0

The error is here, because you can't do a bitwise OR between an integer (temp) and dbnull.value.

OrElse fixes this, not because it's short-circuited, but because it's lower on the order of operations, and so "temp is dbnull.value" and "3=0" are being evaluated first, rather than the parser trying to compare dbNull and temp.

So the evaluation with OrElse works like you're expecting: (assume temp=3)

if temp is dbnull.value OrElse temp = 0 then
if 3 is dbnull.value OrElse 3 = 0 then
if false OrElse 3=0 then
if false OrElse false then
if false then

This was actually on an entry exam at a software company I used to work for, and it was a common problem I used to encounter in VB6. So it's a good idea to parenthesize your sub-expressions when using boolean operators:

This would have compiled properly:

if (temp is dbnull.value) Or (temp = 0) then 

Although, as everyone has already pointed out, OrElse and AndAlso are really the correct operators to use in this context.

Unless your code logic requires the short-circuiting behavior OrElse provides, I would lean toward using the Or operator because:

  • Using "Or" is simple and requires less typing.
  • The computational time savings of using OrElse is negligible in most cases.
  • Most importantly, using OrElse can hide errors in later clauses that may not be initially revealed until those conditions would eventually be met by the program logic.
Related