473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

IIF Statements...your opinions appreciated.

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.

--
Thanks,

Scott
Nov 21 '05 #1
9 1614
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.).

"SQLScott" <SQ******@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
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.

--
Thanks,

Scott

Nov 21 '05 #2

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
On Thu, 6 Jan 2005 13:03:02 -0800, "SQLScott"
<SQ******@discussions.microsoft.com> wrote:
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.


Nov 21 '05 #3
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.

"Samuel R. Neff" <bl****@newsgroup.nospam> wrote in message
news:rb********************************@4ax.com...

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
On Thu, 6 Jan 2005 13:03:02 -0800, "SQLScott"
<SQ******@discussions.microsoft.com> wrote:
I have always heard that using IIF statements is not recommended because theyare slower than using If..Then statements, and not as "clean".

I would appreciate others opinions and why/why not use IIF statements.

Nov 21 '05 #4
I believe an IIF has to evaluate both conditions (no short-circuit). That
could be a negative.

Greg

"SQLScott" <SQ******@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
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.

--
Thanks,

Scott

Nov 21 '05 #5
"Samuel R. Neff" <bl****@newsgroup.nospam> schrieb:
something ? trueThing() : falseThing()

Unfortunately VB doesn't have this. Shame.


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

SCNR

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #6
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.

"Herfried K. Wagner [MVP]" wrote:
"Samuel R. Neff" <bl****@newsgroup.nospam> schrieb:
something ? trueThing() : falseThing()

Unfortunately VB doesn't have this. Shame.


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

SCNR

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #7
"SQLScott" <SQ******@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #8
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
Nov 21 '05 #9
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

"SQLScott" <SQ******@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
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.

--
Thanks,

Scott

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Scott Auge | last post by:
I am looking for comments on something that lets me abstract database updates in an object. Lemme explain what I am thinking: Lets say I have an object Person with... SetFirstName()...
11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
14
by: root | last post by:
Hi group, Apologies in advance if this has been asked somewhere before, but I haven't managed to get anything from the Google archives - I've been getting introductory guides to C++ all day...
3
by: Sergej Andrejev | last post by:
Not long ago I was asked to give PHP lections to some private IT school. I'm on second lection now, and will be teaching my :) students conditional statements (if..else and switch) and loops (while...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
4
by: christof | last post by:
I'm trying to run this T-SQL code using SqlCommand: USE master; ALTER DATABASE AdventureWorks SET SINGLE_USER; GO I did it like that: public static int ObtainExclusiveAccess(Server srv,...
9
by: horizon5 | last post by:
Hi, my collegues and I recently held a coding style review. All of the code we produced is used in house on a commerical project. One of the minor issues I raised was the common idiom of...
2
by: cormac.foley | last post by:
Hi, I'm developing a java application which uses SQL Server as the database. I use Callable Statements to connect to the database. I use JRun as the app server. When running the application I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.