Table of Contents

RDC11039: Function called whatever the IIf condition

Severity: Warning  ·  Family: RDC (Rubberduck Core analyzers)  ·  Raised by: IIfSideEffectAnalyzer

A Function or a Property Get is called in the TruePart or the FalsePart of IIf.

IIf is a function of the VBA library (MS-VBAL §6.1.2.8.1.11 IIf). Like the arguments of any call, its three arguments are evaluated before it is called. Both the TruePart and the FalsePart are evaluated, whatever the condition: a function called in either of them runs every time, with whatever it changes and whatever error it raises.

Calls to the functions of the VBA library that change nothing and raise no error for an argument of the type they declare are not reported: Len, Trim, UCase, IsNumeric, TypeName, Now and the like.

A member of an object declared As Object or As Variant is reported too. Which member it is gets decided only when the code runs, and it can be any code of the object's class.

The finding is the name of the function or member called.

How it is raised

Situation Example Resolution
A function of the project in the TruePart or FalsePart. x = IIf(divisor <> 0, Quotient(a, divisor), 0) Use an If statement, which runs only the branch the condition chooses.
A function of a library that can raise an error or has an effect. IIf(ok, CLng(text), MsgBox("Not a number")) Same: an If statement.
A function in a part given as a named argument. IIf(c, FalsePart:=Count(), TruePart:=0) Same.
A member of an Object or a Variant in a part. IIf(ready, calc.Compute(1), 0), calc declared As Object Same.

See also


ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca