Saturday, July 08, 2006
VB: "Or" Operand Behavior: No Short-Circuit
Now this is a very tricky thing: if you're new to using any Vb.Net version prior to VB 2005, you might have neglected to learn that the Or operand doesn't work as you might expect. Most books don't put a strong emphasis on this, which is a shame cuz it always leads to people scratching their heads in despair for a few good minutes at least when they use "Or" for the first time.
The thing is that in Vb.Net the Or operand will always read both statements no matter if the first one already evaluated to true! This could generate some nasty runtime errors.
For instance:
Dim myNumber = 1
if myNumber = 1 or myNumber = 3 then
'do something with myNumber
end if
In most languages, you would expect that the other conditions following the first one would be ignored since the first condition has been satisfied (myNumber = 1) and this is an "Or" statement, meaning only condition needs to evaluate to true. However, this is not the case in Vb.Net. It doesn't cut the "Or" statement short and even if the very first condition evaluates to true it will continue trying to evaluate the other ones.
Now you may be wondering, what's the big deal?Even if it evaluates the rest of the conditions, surely, the result is still the same since one of them is true anyway.
Well, yes, in most cases, the only consequence, if any, of this behavior would potentially be a hit on performance. HOWEVER, this can get ugly.
Consider the following example:
if row("something") is DbNull.Value or row("something") = "" then
'do something
end if
The code above is a snippet from a loop on a the rows of a datatable.
If the the first condition is true and row("something") is NULL then this code will break and we'll have a runtime error that says something like "Operator is not valid for type 'DBNull' and string "".
This is because if the value is NULL then you cannot compare a NULL value to a string, which is what ends up happening in the second expression, row("something") = "" . If the statement had been cut short then this would not be a problem since it would only get to the second expression if the first one, row("something") was not true.
thus, unfortunately, you have to take the long road and write a bit of redundant code to force the right behavior, like this:
if row("something") is DbNull.Value then
'do something
elseif row("something") = "" then
'do the same thing as above
end if
The good news is that this behavior has finally been corrected in Visual Basic 2005 and the "Or" operand now has the same short-circuit capability as most othe languages do.
Now this is a very tricky thing: if you're new to using any Vb.Net version prior to VB 2005, you might have neglected to learn that the Or operand doesn't work as you might expect. Most books don't put a strong emphasis on this, which is a shame cuz it always leads to people scratching their heads in despair for a few good minutes at least when they use "Or" for the first time.
The thing is that in Vb.Net the Or operand will always read both statements no matter if the first one already evaluated to true! This could generate some nasty runtime errors.
For instance:
Dim myNumber = 1
if myNumber = 1 or myNumber = 3 then
'do something with myNumber
end if
In most languages, you would expect that the other conditions following the first one would be ignored since the first condition has been satisfied (myNumber = 1) and this is an "Or" statement, meaning only condition needs to evaluate to true. However, this is not the case in Vb.Net. It doesn't cut the "Or" statement short and even if the very first condition evaluates to true it will continue trying to evaluate the other ones.
Now you may be wondering, what's the big deal?Even if it evaluates the rest of the conditions, surely, the result is still the same since one of them is true anyway.
Well, yes, in most cases, the only consequence, if any, of this behavior would potentially be a hit on performance. HOWEVER, this can get ugly.
Consider the following example:
if row("something") is DbNull.Value or row("something") = "" then
'do something
end if
The code above is a snippet from a loop on a the rows of a datatable.
If the the first condition is true and row("something") is NULL then this code will break and we'll have a runtime error that says something like "Operator is not valid for type 'DBNull' and string "".
This is because if the value is NULL then you cannot compare a NULL value to a string, which is what ends up happening in the second expression, row("something") = "" . If the statement had been cut short then this would not be a problem since it would only get to the second expression if the first one, row("something") was not true.
thus, unfortunately, you have to take the long road and write a bit of redundant code to force the right behavior, like this:
if row("something") is DbNull.Value then
'do something
elseif row("something") = "" then
'do the same thing as above
end if
The good news is that this behavior has finally been corrected in Visual Basic 2005 and the "Or" operand now has the same short-circuit capability as most othe languages do.
