Parameter Can Be ByVal

Flags parameters that are passed by reference (ByRef), but could be passed by value (ByVal).

Remarks

For performance reasons, this inspection will not flag a parameter that is passed as an argument to a procedure that also accepts it ByRef.

Reasoning

Explicitly specifying a ByVal modifier on a parameter makes the intent explicit: this parameter is not meant to be assigned. In contrast, a parameter that is passed by reference (implicitly, or explicitly ByRef) makes it ambiguous from the calling code's standpoint, whether the procedure might re-assign these ByRef values and introduce a bug.

Default severity

Suggestion

Inspection type

NamingAndConventionsIssues

Examples

This example should trigger a result

MyModule (StandardModule)
Public Sub DoSomething(ByVal foo As Long, bar As Long) Debug.Print foo, bar End Sub

This example should NOT trigger a result

MyModule (StandardModule)
Option Explicit Public Sub DoSomething(ByVal foo As long, ByRef bar As Long) bar = foo * 2 ' ByRef parameter assignment: passing it ByVal could introduce a bug. Debug.Print foo, bar End Sub

This example should NOT trigger a result

MyModule (StandardModule)
Option Explicit Public Sub DoSomething(ByVal foo As long, ByRef bar As Long) DoSomethingElse bar ' ByRef argument will not be flagged Debug.Print foo, bar End Sub Private Sub DoSomethingElse(ByRef wouldNeedRecursiveLogic As Long) Debug.Print wouldNeedRecursiveLogic End Sub

Rubberduck.CodeAnalysis.Inspections.Concrete.ParameterCanBeByValInspection.cs (Prerelease-v2.5.9.6289)