9 Replies - 2949 Views - Last Post: 28 October 2014 - 03:47 PM

#1 depricated  Icon User is offline

  • RISC Architecture is going to change everything.

Reputation: 917
  • View blog
  • Posts: 2,986
  • Joined: 13-September 08

if() and iif(), when might we use the latter?

Posted 12 May 2014 - 07:46 AM

For context if you're not immediately familiar with them, if and iif are essentially ternary operators.

The only difference between them to speak of is that iif() evaluates both true and false results, while if only evaluates the returned result. This can lead to some serious bugs that might slip past using iif. For example

        Dim left, right As Integer
        left = inputLeft
        right = inputRight
        MsgBox(IIf(left <> 0 And right <> 0, left \ right, "Cannot divide by 0."))



In this example, we've declared that if either left or right are 0, to return "Cannot divide by 0." But if either are set to 0, instead we get a runtime error. This is because iif evaluates both sides of the argument, even though it only needs to evaluate one.

Microsoft provides a similar example on MSDN

number = 12

' When the divisor is not 0, both If and IIf return 4. 
Dim divisor = 3
Console.WriteLine(If(divisor <> 0, number \ divisor, 0))
Console.WriteLine(IIf(divisor <> 0, number \ divisor, 0))

' When the divisor is 0, IIf causes a run-time error, but If does not.
divisor = 0
Console.WriteLine(If(divisor <> 0, number \ divisor, 0))
' Console.WriteLine(IIf(divisor <> 0, number \ divisor, 0))


So I imagine there might be a time when this method of evaluation is worthwhile. I thought I'd bring it up for discussion, see what people thought, if you have any examples of where that functionality is taken advantage of or ideas on how one might.

This post has been edited by depricated: 12 May 2014 - 07:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: if() and iif(), when might we use the latter?

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: if() and iif(), when might we use the latter?

Posted 12 May 2014 - 08:15 AM

Just to provide some references for this discussion:

If Operator :MSDN

If() can be supplied with just two arguments; in which case, it is the equivalent to C#'s null coalescing operator (??).
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: if() and iif(), when might we use the latter?

Posted 12 May 2014 - 10:25 AM

IIF( boolExpr, trueExpr, falseExpr )

Evaluates both sides of the outcome before choice which one to follow. This is preserve compatibility with the VB6 semantic meaning.

IF( boolExpr , trueExpr, falseExpr )
Only evaluates the result after deciding which to take.
Was This Post Helpful? 0
  • +
  • -

#4 depricated  Icon User is offline

  • RISC Architecture is going to change everything.

Reputation: 917
  • View blog
  • Posts: 2,986
  • Joined: 13-September 08

Re: if() and iif(), when might we use the latter?

Posted 13 May 2014 - 11:56 AM

Hm, maybe I was unclear.

I was wondering if anyone could think of an instance where we might want that evaluation before deciding which to use, rather than after.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: if() and iif(), when might we use the latter?

Posted 13 May 2014 - 01:00 PM

IIf() is, effectively, deprecated. Besides which, we should avoid complex statements that have, or rely upon, side-effects.

So, personally, I think it is more be important to be aware of the differences, and to use If() in preference to IIf().

(Probably not the response you are looking for ;))
Was This Post Helpful? 0
  • +
  • -

#6 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: if() and iif(), when might we use the latter?

Posted 14 May 2014 - 05:33 AM

i think My example explain what you need
        Dim Rowcount As Integer
        If Table IsNot Nothing AndAlso Table.Rows.Count > 0 Then
            Rowcount = Table.Rows.Count
        Else
            Rowcount = 0
        End If
        Rowcount = IIf(Table IsNot Nothing AndAlso Table.Rows.Count > 0, Table.Rows.Count, 0)



in the above snippet look like both if and iif are doing the same but in real when you run this code
IIF will raise an error
reason
if block is executed step by step first the expression then based on the expression either true part or false part
where as IIF is executed at a single stroke,(i.e) IIF evaluate the entire expression
Was This Post Helpful? 0
  • +
  • -

#7 Pishak  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 26-March 12

Re: if() and iif(), when might we use the latter?

Posted 28 October 2014 - 01:07 PM

IIf is merely a function that simplifies the usage of IF-Then. It's not really deprecated, just not widely used.

It innards of IIF are probably something like this(except my example is an IIF(Of T)

Function IIIF(Of t)(expression As Boolean, TruePart As t, FalsePart As t) As t
If expression = True Then Return TruePart
If expression = False Then Return Falsepart
End Function

See! Skin and bones! Nothing to it at all...

This post has been edited by andrewsw: 28 October 2014 - 01:11 PM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: if() and iif(), when might we use the latter?

Posted 28 October 2014 - 01:14 PM

@Pishak There is no need to quote the entire original post, there is a Reply button further down the page.

Function IIIF(Of t)(expression As Boolean, TruePart As t, FalsePart As t) As t
If expression = True Then Return TruePart
If expression = False Then Return Falsepart
End Function

In any case, this is NOT correct. IIF (not IIIF) evaluates both terms, as has already been stated in the earlier posts.

This post has been edited by andrewsw: 28 October 2014 - 01:15 PM

Was This Post Helpful? 0
  • +
  • -

#9 Pishak  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 26-March 12

Re: if() and iif(), when might we use the latter?

Posted 28 October 2014 - 03:09 PM

Andrew,

Actually, the complete expression will be evaluated before it is assigned to the "Expression" parameter of the IIF function, the function itself contains only 2 simple lines of code. This is because once the actual expression is assigned to the expression parameter, it has already been reduced to its smallest form. I.e. The expression evaluates, and then, the result of that evaluation is assigned to the expression parameter. All boolean expressions break down into a Single boolean value, and must do so before an identifier can point to it.

I do realize I provided an IIIF(Of T)(As I already stated, but I suppose you wanted to point it out again?) Anywho, this was an example, and I like encouraging the use of generics, although it did differ on a very basic level. But since you want to split hairs with me here, here is EXACTLY what an IIF statement is on the insides:

    
    Function IIF2(Expression As Boolean, TruePart As Object, FalsePart As Object) As Object
        If Expression Then Return TruePart
        Return FalsePart
    End Function 


Posted Image
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: if() and iif(), when might we use the latter?

Posted 28 October 2014 - 03:47 PM

You are right. My mistake. I apologise.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1