Why a little bit of VB is a bad bad thing

Scott Hanselman posted about VB6 and showed a snippet of code
:

Public Function Fib(ByVal n As Integer) As Integer
    Fib = IIf (n < 2, n, Fib(n-1) + Fib(n-2))
End Function

What Scott probably didn’t realize that the IIF function in VB (both VB6 and later) is a Function not an operator. That means all arguments passed to the function are evaluated.

Can you see the adverse side effect Scott’s code would have ?

 

NB. In VB.NET if you wrote the same code as Scott posted, and you have Strict On, the compiler will give you a type casting error because the IIF function is not generic.  So that might help with the unintended bad usage. The better news is VB.NET has an If operator (ternary operator) So the above code is written as :

 

Public Function Fib(ByVal n As Integer) As Integer
     Fib = If (n < 2, n, Fib(n-1) + Fib(n-2))
End Function

 

Generally IIF should be avoided regardless of what version of VB you use.