Skip to content

Code Inspections

Christopher McClellan edited this page Jan 21, 2015 · 15 revisions

Summary

Rubberduck provide code analysis via the Code Inspections functionality. The code in all open VBA Projects is parsed and Rubberduck suggests improvements. Many of these can be automatically corrected by the add in by using the "Fix" button.

All Code Inspections' severity level can be configured to any of the following severity levels.

  • Do Not Show
  • Hint
  • Suggestion
  • Warning
  • Error

Implemented Inspections

Code Quality Issues

Use Option Explicit

It's a best practice in VBA to always declare Option Explicit at the beginning of every code module.

This Inspection has a default severity of Warning.

Variable Type is Implicitly Variant

When a variable is not explicitly declared with a type, it is implicitly declared as a Variant type. Variants allow anything to be stored in them. It's best practice to explicitly declare all types.

This Inspection has a default severity of Warning.

Function Implicitly Returns a Variant

When a Function's return type is not specified, it will return a Variant type. It's best practice to explicitly declare a return type.

This Inspection has a default severity of Suggestion.

Parameter is Implicitly Passed By Reference

If not specified, all arguments are passed ByRef. It's best practice to pass all arguments ByVal unless it's truly necessary to pass by reference. At the least, all parameters should have this option explicitly specified. Rubberduck has quick fix options for both situations.

This Inspection has a default severity of Warning.

Instruction Contains Multiple Declarations

It's best practice to give each variable declaration its own line and Dim statement.

This:

Dim foo As String, bar As Integer, baz As Long

Can be written as:

Dim foo As String
Dim bar As Integer
Dim baz As Long

This helps avoid making the mistake of not declaring a type for each variable. For example, in this code:

Dim foo, bar, baz As Long

only baz is declared as Long. foo and bar are implicitly declared as Variant.

This Inspection has a default severity of Suggestion.

Maintainability/Readability Issues

Use of Obsolete REM Comment Syntax

VBA supports the archaic and obsolete BASIC REM syntax for comments. REM should be replaced with a single quote (').

This Inspection has a default severity of Suggestion.

Roadmap

For a roadmap of Code Inspections to be implemented in the future, please see the Open Issues in the Code Inspections Milestone. If you think of something you'd like to see added to the list of Code Inspections, please Submit your feature request as an issue.

Configuration

Code Inspections are configurable and stored in the Rubberduck.config file that is installed along with the add-in to the C:\Users\Username\AppData\Roaming\Rubberduck\ directory.

Severity levels can be set through the Settings Dialog by going to Rubberduck >> Options.

menu bar

All Code Inspections' severity level can be configured to any of the following severity levels.

  • Do Not Show
  • Hint
  • Suggestion
  • Warning
  • Error

Advanced users may also directly modify the rubberduck.config file.

  <CodeInspections>
    <CodeInspection Name="Parameter is passed ByRef implicitly" Severity="Warning" InspectionType="CodeQualityIssues" />
    <CodeInspection Name="Function returns an implicit Variant" Severity="DoNotShow" InspectionType="CodeQualityIssues" />
    <CodeInspection Name="Instruction contains multiple declarations" Severity="Hint" InspectionType="CodeQualityIssues" />
    <CodeInspection Name="Use of obsolete Rem comment syntax" Severity="Suggestion" InspectionType="MaintainabilityAndReadabilityIssues" />
    <CodeInspection Name="Option Explicit is not specified" Severity="Error" InspectionType="CodeQualityIssues" />
    <CodeInspection Name="Variable type is implicitly Variant" Severity="Warning" InspectionType="CodeQualityIssues" />
  </CodeInspections>
Clone this wiki locally