Side Effects of VBA IIf
Another issue with
IIf
arises because it is a library function: unlike the C-derived conditional operator, both truepart and the falsepart will be evaluated regardless of which one is actually returned. Consider the following example:value = 10 result = IIf(value = 10, TrueFunction, FalseFunction)
Although TrueFunction is the function intended to be called,
IIf
will cause both TrueFunction and FalseFunction to be executed.
Also consider this one:
a = 10 b = 0 result = IIf(b <> 0, a / b, 0)
While the programmer intends to avoid raising an error by performing a division by zero, whenever b is zero the error will actually happen. This is because the code in the snippet is to be read as
a = 10
b = 0
_temp1 = a / b ' Error if b = 0
_temp2 = 0
_temp3 = b <> 0
result = IIf(_temp3, _temp1 , _temp2)
This issue makes the IIf() call less useful than the conditional operator. To solve this issue, Microsoft developers had considered converting
IIf
to an intrinsic function; had this happened, the compiler would have been able to perform type inference and short-circuiting by replacing the function call with inline code.
References-
support.microsoft.com
No comments:
Post a Comment