13 Replies - 3991 Views - Last Post: 08 January 2015 - 11:07 AM

#1 depricated  Icon User is offline

  • Conversion Software Version 7.0


Reputation: 1225
  • View blog
  • Posts: 3,962
  • 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 online

  • I'm not here to twist your niblets
  • member icon

Reputation: 4667
  • View blog
  • Posts: 17,302
  • 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: 2290
  • View blog
  • Posts: 9,529
  • 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

  • Conversion Software Version 7.0


Reputation: 1225
  • View blog
  • Posts: 3,962
  • 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 online

  • I'm not here to twist your niblets
  • member icon

Reputation: 4667
  • View blog
  • Posts: 17,302
  • 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: 3
  • View blog
  • Posts: 7
  • 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 online

  • I'm not here to twist your niblets
  • member icon

Reputation: 4667
  • View blog
  • Posts: 17,302
  • 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: 08 January 2015 - 06:02 AM

Was This Post Helpful? 0
  • +
  • -

#9 Pishak  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • 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 online

  • I'm not here to twist your niblets
  • member icon

Reputation: 4667
  • View blog
  • Posts: 17,302
  • 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
  • +
  • -

#11 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 439
  • Joined: 26-October 06

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

Posted 20 December 2014 - 12:02 AM

Hmm, I do not see the actual intent of using IIf, which is supposed to evaluate a Boolean value then compare a dual evaluation and return a specific value depending upon the result. Say you wish to calculate the result of an employee's bonus based on a metric.

    'Calculate Bonus Based on Customer Satisfaction
    Sub CalcBonus(EmpBonus as Long, EmpPay as Long,_
                  EmpCustHappy As Int16)

        'The employee receives 24 customer reviews a
        'month, if they have more than 18 positive,
        'their bonus is greater.

      DIM eBonus as Boolean
        
        eBonus = IIf(EmpCustHappy > 18, 700, 300)
        EmpPay += EmpBonus

    End Sub





If the Reviews result in more than 18 positive
customer experiences, the bonus is 700
If it is 18 or less the bonus is 300.
If you make the IIf function very complex it will take
a long time to complete. Therefore in most cases,
an 'If-Then-Else' or 'Select Case' would accomplish the
desired result much more easily and be much more
readable to other programmers.

This post has been edited by KeyWiz: 20 December 2014 - 12:08 AM

Was This Post Helpful? 0
  • +
  • -

#12 depricated  Icon User is offline

  • Conversion Software Version 7.0


Reputation: 1225
  • View blog
  • Posts: 3,962
  • Joined: 13-September 08

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

Posted 08 January 2015 - 05:41 AM

If() just provides a ternary assignment. I read it the same as value = expression ? true : false;

What I was bringing up - I suppose this was unclear? I'm not confused about the difference between If and Iif. I was just tossing out for conjecture to see if anyone had thoughts on when we might WANT to use Iif instead of If.

Because using Iif causes BOTH sides to be evaluated. That's why it throws the error. I think Andrew was spot on about wanting to avoid complex statements.
Was This Post Helpful? 0
  • +
  • -

#13 Pishak  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 26-March 12

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

Posted 08 January 2015 - 08:41 AM

View Postdepricated, on 08 January 2015 - 05:41 AM, said:

If() just provides a ternary assignment. I read it the same as value = expression ? true : false;

What I was bringing up - I suppose this was unclear? I'm not confused about the difference between If and Iif. I was just tossing out for conjecture to see if anyone had thoughts on when we might WANT to use Iif instead of If.

Because using Iif causes BOTH sides to be evaluated. That's why it throws the error. I think Andrew was spot on about wanting to avoid complex statements.

IIf mainly for code reduction(Aka short circuit function). You might have a scenario where there are several if-then statements following each other, these can pile up fast. You can also do If-Then in a single line, which is usually my preference:

ex:
'Expression TruePart  FalsePart
If A = 1 Then B = 2 Else B = 3


Was This Post Helpful? 0
  • +
  • -

#14 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 515
  • View blog
  • Posts: 3,455
  • Joined: 12-January 10

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

Posted 08 January 2015 - 11:07 AM

personally i dont like combining if's-----

i also dont like c#'s if ?

i like to keep them separate for easier debugging
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1