Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET - IIF(,,) - Both "sides" are evaluated. What situations should I watch out for?

Tags:

vb.net

I recently learned of the IIF(A,B,C) function. I'm a long time VB/VB.NET Coder who recently spent a lot of time coming up to speed in SQL coding.

One (obvious) common thing to do in SQL is something like the following:

select (case where @var = 0 then MyTable.Val1 else MyTable.Val2 end) from MyTable 

IIF(A,B,C) will allow me to do this in VB.NET... all on one line.

However, I have read that both B and C are evaluated no matter what A evaluates to.

I can think of some obvious situations where this is a bad thing such as:

Dim X as integer = IIF(SomeBoolean = true, ExpensiveFunction1(), ExpensiveFunction2()) 

As I will be including this in my repertoire, are there any more subtle situations where I may get myself into trouble using IIF?

It's a pretty big departure in some situations from using the old fashioned:

Dim X as integer if SomeBoolean = true then   X = ExpensiveFunction1() else   X = ExpensiveFunction2() end if 

I'm hoping to save myself some annoying performance issues and/or bugs in the future.

Update 2016

For the past few years, a new VB.NET feature exists which eliminates the need to use the IIF() function.

if(Something = true, ExecuteA(), ExecuteB()) 

Only ExecuteA() OR ExecuteB() are executed. Finally, inline IF with shortcircuiting.

So, if you are using later versions of VB.NET (as of 2016), use this instead if you can.

like image 994
Brian Webster Avatar asked Aug 03 '09 01:08

Brian Webster


People also ask

What is the use of IIF in VB net?

IIf always evaluates both truepart and falsepart, even though it returns only one of them. Because of this, you should watch for undesirable side effects. For example, if evaluating falsepart results in a division by zero error, an error occurs even if expr is True.

What is the difference between IF and IIF?

The critical difference between IIF (available from VS 2002 forward) and IF (available in VS 2005 forward) is that IIF is a function and evaluates all of its arguments prior to returning a value, while IF is an operator that executes like a short-circuiting conditional, only evaluating the true or false argument ...


2 Answers

Here is the most common gotcha.

Z = iif(y=0, 0, x/y)  'Throws a divide by zero exception when y is 0 

Don't use it to avoid division by zero errors.

Another possible logic bug is when one side of the iif or the other calls a method that modifies the system state or has output parameters.

Z = iif(FunctionA(InputOutputParam), FunctionB(InputOutputParam)) 'InputOutputParam is indeterminate or at least ambiguous here. 

There really is no good reason to use IIF in my experience. Mostly it is just used to abbreviate code and given the problems it can cause, it just isn't worth it. Plus, I think it makes the code harder to read.

The other thing that bites is that it returns a boxed value (ie an Object data type) which you have to cast back to the desired type.

like image 77
JohnFx Avatar answered Oct 10 '22 03:10

JohnFx


[IIF, not IFF]

The most common case we've seen is that one side or the other evaluates to Nothing.

Your code might be expecting to use IIF as guard to keep from getting a NullReferenceException, like this:

IIF(something Is Nothing, "nothing", something.Value) 

But that won't work, because both sides are always evaluated. This happens a lot in code written by people who come from a C/C++/C#/Java background, since in those languages the ternary operator ?: does short-circuit evaluation.

And the fact that the VS 2005 IIF() documentation states that IIF is just like ?: doesn't help:

The IIf function provides a counterpart for the ternary Conditional Operator: ? : in Visual C++.

Nowhere on that reference page does it state that both sides are evaluated.

like image 40
lavinio Avatar answered Oct 10 '22 01:10

lavinio