Move Field Closer to Usage

Locates module-level fields that can be moved to a smaller scope.

Reasoning

Module-level variables that are only used in a single procedure can often be declared in that procedure's scope. Declaring variables closer to where they are used generally makes the code easier to follow.

Default severity

Hint

Inspection type

NamingAndConventionsIssues

Examples

This example should trigger a result

MyModule (StandardModule)
Option Explicit Private foo As Long Public Sub DoSomething() foo = 42 Debug.Print foo ' module variable is only used in this scope End Sub

This example should NOT trigger a result

MyModule (StandardModule)
Option Explicit Public Sub DoSomething() Dim foo As Long ' local variable only used in this scope foo = 42 Debug.Print foo End Sub

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