IIF Statements...your opinions appreciated.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have always heard that using IIF statements is not recommended because they
are slower than using If..Then statements, and not as "clean".

I would appreciate others opinions and why/why not use IIF statements.
 
Pros:
- Can return a value, where as an If statement just executes conditional
code. This is a little different
- Convenience, slightly less to type

Cons
- Always returns Object. It is just a function not a language construct, and
so you end up having to cast everything from Object to what it actually is,
even though if it was a compile time check it would be obvious
- Potential performance issues - though i don't know too much about it.

Overall, I would say, unless you have mission critical code, the performance
hit (if any) you will get from IIF is not going to be noticeable in your app
under average usage. It is annoying that the return type as Object, and this
makes IIF a lot less useful then it could have been.

You could always right your own strongly typed IIF, and make it use an If
statemnet (if you are worried about performance).

Public Function IIF(expression as Boolean, truePart as String, falsePart as
String) As String
If expression Then
Return truePart
Else
Return falsePart
End Function

That way you get the convenience of IIF, but improved usability.
You could do this for every type that you commonly use (Integer, etc.).
 
IIF is a function call, not a statement like If..Else..

The problem with IIF is that both the true and false values are
evaluated, so

IIF(something, trueThing(), falseThing())

calls both trueThing and falseString functions. An IF statement
wouldn't do that.

C# in contrast actually has an equivalent statement (conditional
operator)...

something ? trueThing() : falseThing()

Unfortunately VB doesn't have this. Shame.

HTH,

Sam
 
Good point raised on the evaluation of both parts.

If the true and false parts are not trivial to evaluate, then one would
definitely be better off going with a regular If statement.
 
I believe an IIF has to evaluate both conditions (no short-circuit). That
could be a negative.

Greg
 
Samuel R. Neff said:
something ? trueThing() : falseThing()

Unfortunately VB doesn't have this. Shame.

I am happy that VB doesn't have this (syntax).

SCNR
 
Just for clarification, I know how the IIF function (yes, my apologies for
calling it a statement) works, my question is directed toward what others
thought of it and if they use it or not versus the If..else...

I am trying to limit the amount of code written for a particular situation
and was weighing using an IIF function versus the If..Else...

So, right now I have:

bizOrganization.DistrictNumber = CType(IIf(Me.txtDistrictNumber.Text = "",
0, Me.txtDistrictNumber.Text), Integer)

Which gets me around Marina's issue. It works and I have not seen a
performance hit.

Just wondering what you all thought of using IIF...

Thanks for the feedback.
 
SQLScott said:
Just wondering what you all thought of using IIF...

I don't have to add anything to the technical facts of 'IIf'. Personally, I
like 'IIf' and I sometimes use it when concatenating strings containing
conditional parts:

\\\
Dim s As String = _
"Bla " & DirectCast(IIf(..., "Bar", "Baz")) & " Bas!"
///

With 'Option Strict Off' you could get rid of the 'DirectCast' and the
result would be better readable (but I don't recommend to turn this option
of... ;-)). 'IIf' is a valid part of Visual Basic, it's not a deprecated
feature, so it's personal preference whether to use it or not.
 
Scott,

The advantage from VBNet abobe C derived languages as C# and Java beside all
technical points is, that it is more readable.

The IIF is in contradiction to that.

Just my opinion.

Cor
 
Scott,
In addition to the other comments:
I rarely use IIF as it requires the cast from object back to my type.

A Con I would add is that IIF only accepts Object as parameters, so if you
use it with Value Types, the values will be boxed, this boxing can add extra
overhead to the work the GC needs to do later...

Where I've wanted to use IIf to simplify code, I've written type save
versions of IIF as Marina suggests. VB.NET 2005 (aka Whidbey, due out later
in 2005) makes creating a type save version of IIf easier with Generics. You
can write a single IIf(Of T) function instead of writing multiple overloaded
IIf functions for each type needed. The generic version automatically
creates an overloaded version when needed...

' VS.NET 2005 syntax
Public Function IIf(Of T)(ByVal expression As Boolean, _
ByVal truePart As T, ByVal falsePart As T) As T
If expression Then
Return truePart
Else
Return falsePart
End If
End Function

You can use the above IIf in Whidbey with Option strict On, without needing
to cast the return value.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top