RDC00214: Procedure can be written as a function
Severity: Information · Family: RDC (Rubberduck Core analyzers) ·
Raised by: ProcedureCanBeWrittenAsFunctionAnalyzer
A Sub gives its result back through a parameter: it has one ByRef parameter, and its body assigns it.
A ByRef parameter lets the procedure assign the caller's variable (MS-VBAL §5.3.1.5 Parameter Lists).
A Function returns its result instead (MS-VBAL §5.3.1.6 Subroutine and Function Declarations),
and the call says where the result goes: total = Sum(a, b) reads more plainly than Sum a, b, total.
A procedure whose signature is not its own to choose is not reported: a Declare, an implementation of an interface member, a handler of an event, and a
Public member of a class module, which may be a member of an interface. A ParamArray is not counted as the ByRef parameter.
The finding is the name of the procedure.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
A Sub whose only ByRef parameter is assigned. |
Sub Sum(ByVal a As Long, ByVal b As Long, total As Long) total = a + b |
Make it a Function that returns the result: Function Sum(ByVal a As Long, ByVal b As Long) As Long. |
See also
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca