Skip to content

Refactorings

Mathieu Guindon edited this page Apr 12, 2019 · 2 revisions

Refactoring is all about improving your code, without modifying its functionality. Rubberduck refactorings are designed to avoid breaking things, but before modifying a piece of working code, it's recommended to have unit tests that cover the affected execution paths, so that a refactoring can demonstrably not affect functionality.

Most refactoring commands are context-sensitive and will only be enabled given a valid selection and a Ready parser state, without any modules modified since the last parse.

Some of the current refactoring tools available are:

Extract Method

this refactoring is currently undergoing a rewrite, and is disabled in release builds.

Select the lines of code to extract to a new procedure; this refactoring replaces the selected code with a call to the extracted method, passing in any parameters, and cleaning up any local variable declarations that are no longer needed; Rubberduck will determine whether to extract a Sub or a Function, preview the result and allow you to rename, reorder, or remove parameters, and determine/modify what to return.

Rename

Select an identifier - be it a variable, a constant, a procedure, an enum, a UDT member, a line label, anything with a name, including a control on a UserForm or a class or standard module, enter a new name for it, and Rubberduck automatically updates all early-bound references.

Keep in mind that Rubberduck cannot resolve late-bound references, e.g. member calls made against Object or Variant.

This refactoring is also available from the Code Explorer.

Introduce Parameter

Select a local variable to promote to a parameter. Call sites for the containing procedure will be updated with a TODO argument, which must then be specified for all call sites.

Introduce Field

Select a local variable to promote to module scope.

Encapsulate Field

Select a module variable (aka "field") to make Private and expose a Property Get and Property Let/Set accessors for.

Move Closer to Usage

Moves a field that's only used in 1 procedure, into that procedure. Or moves a local variable immediately above its first use.

Extract Interface

Pick what class members to extract into an interface; the feature creates a new class modules with stubs for the selected members, and makes the original class implement the extracted interface.

Implement Interface

Creates stubs for all members of an unimplemented interface, so you don't need to create them manually by selecting them one by one in the code pane dropdown.

Clone this wiki locally