Static code analysis can find hundreds of opportunities in VBA code.
Rubberduck builds its own internal representation of the code, and then proceeds to analyze it. Each individual inspection can easily be disabled, or configured to issue inspection results at different severity levels ranging from Hint
to Error
.
Use the Inspection Results toolwindow to review Rubberduck’s findings, search, filter, regroup results by inspection, location, type, or severity. Each inspection result comes with a detailed description of what’s being flagged and why, so you can make an enlightened decision.
Unless configured otherwise, Rubberduck automatically runs inspections after the a parser/resolver cycle completes (regardless of whether the inspection results toolwindow is displayed or not).
For the best experience, it would be recommended to first try Rubberduck with an empty project, add a new module, and write, say, a loop that counts 1 to 10 and outputs to the debug pane - then to parse that and review the inspection results; carefully review the inspection settings, and consider disabling the inspections that irreconcilably clash with your preferences: use meaningful names alone can easily produce hundreds upon hundreds of results if you’re not that much into using vowels, or if you, say, prefix all your variable names; these inspections can be re-enabled anytime you’re ready!
These inspections look for a wide range of potential issues, from missing Option Explicit
to undeclared or unused variables.
Locates procedures that are never invoked from user code.
Default severity: Warning
Unused procedures are dead code that should probably be removed. Note, a procedure may be effectively "not used" in code, but attached to some Shape object in the host document: in such cases the inspection result should be ignored.
Locates places in which a procedure needs to be called but an object variables has been provided that does not have a suitable default member.
Default severity: Warning
The VBA compiler does not check whether the necessary default member is present. Instead there is a runtime error whenever the runtime type fails to have the default member.
Identifies redundant ByRef modifiers.
Default severity: DoNotShow
This inspection will not run by default, it must be manually enabled in Code Inspections configuration settings.
Out of convention or preference, explicit ByRef modifiers could be considered redundant since they are the implicit default. This inspection can ensure the consistency of the convention.
Identifies auto-assigned object declarations.
Default severity: Suggestion
Auto-assigned objects are automatically re-created as soon as they are referenced. It is therefore impossible to set one such reference to 'Nothing' and then verifying whether the object 'Is Nothing': it will never be. This behavior is potentially confusing and bug-prone.
Locates assignments to object variables for which the RHS does not have a compatible declared type.
Default severity: Warning
The VBA compiler does not check whether different object types are compatible. Instead there is a runtime error whenever the types are incompatible.
Identifies identifiers that hide/"shadow" other identifiers otherwise accessible in that scope.
Default severity: DoNotShow
This inspection will not run by default, it must be manually enabled in Code Inspections configuration settings.
Global namespace contains a number of perfectly legal identifier names that user code can use. But using these names in user code effectively "hides" the global ones. In general, avoid shadowing global-scope identifiers if possible.
Locates 'Stop' instructions in user code.
Default severity: Suggestion
While a great debugging tool, 'Stop' instructions should not be reachable in production code; this inspection makes it easy to locate them all.
Warns about Rubberduck annotations with more arguments than allowed.
Default severity: Warning
Most annotations only process a limited number of arguments; superfluous arguments are ignored.
Identifies assignments without Set for which both sides are objects.
Default severity: Warning
Whenever both sides of an assignment without Set are objects, there is an assignment from the default member of the RHS to the one on the LHS. Although this might be intentional, in many situations it will just mask an erroneously forgotten Set.
Warns when a variable is referenced prior to being assigned.
Default severity: Error
An uninitialized variable is being read, but since it's never assigned, the only value ever read would be the data type's default initial value. Reading a variable that was never written to in any code path (especially if Option Explicit isn't specified), is likely to be a bug.
Warns about implicit local variables that are used but never declared.
Default severity: Error
If this code compiles, then Option Explicit is omitted and compile-time validation is easily forfeited, even accidentally (e.g. typos).
Warns about public class members with an underscore in their names.
Default severity: Warning
The public interface of any class module can be implemented by any other class module; if the public interface contains names with underscores, other classes cannot implement it - the code will not compile. Avoid underscores; prefer PascalCase names.
Finds instances of 'On Error Resume Next' that don't have a corresponding 'On Error GoTo 0' to restore error handling.
Default severity: Warning
'On Error Resume Next' should be constrained to a limited number of instructions, otherwise it supresses error handling for the rest of the procedure; 'On Error GoTo 0' reinstates error handling. This inspection helps treating 'Resume Next' and 'GoTo 0' as a code block (similar to 'With...End With'), essentially.
Flags 'Case' blocks that will never execute.
Default severity: Warning
Unreachable code is certainly unintended, and is probably either redundant, or a bug.
Identifies the use of bang notation, formally known as dictionary access expression.
Default severity: Warning
A dictionary access expression looks like a strongly typed call, but it actually is a stringly typed access to the parameterized default member of the object.
Identifies the use of bang notation, formally known as dictionary access expression, for which a recursive default member resolution is necessary.
Default severity: Warning
A dictionary access expression looks like a strongly typed call, but it actually is a stringly typed access to the parameterized default member of the object. This is especially misleading if the parameterized default member is not on the object itself and can only be reached by calling the parameterless default member first.
Identifies the use of bang notation, formally known as dictionary access expression, for which the default member is not known at compile time.
Default severity: Warning
A dictionary access expression looks like a strongly typed call, but it actually is a stringly typed access to the parameterized default member of the object. This is especially misleading the default member cannot be determined at compile time.
Locates places in which a value needs to be accessed but an object variables has been provided that does not have a suitable default member.
Default severity: Warning
The VBA compiler does not check whether the necessary default member is present. Instead there is a runtime error whenever the runtime type fails to have the default member.
Warns about variables that are never assigned.
Default severity: Warning
A variable that is never assigned is probably a sign of a bug. This inspection may yield false positives if the variable is assigned through a ByRef parameter assignment, or if UserForm controls fail to resolve, references to these controls in code-behind can be flagged as unassigned and undeclared variables.
Warns about variables that are never referenced.
Default severity: Warning
A variable can be declared and even assigned, but if its value is never referenced, it's effectively an unused variable.