• RSS Feed

Extract Method

Refactor with Extract Method when common blocks of code appear in multiple places, or when an existing method would be easier to understand if divided into pieces. Extract Method creates a new method from a block of code, and changes the original code to invoke the new method.


Place the caret on a block of code—multiple lines or a portion of one—and select Extract Method via the Quick Refactoring Menu (Shift+Alt+Q), or via the context menu of the text editor.

When the dialog opens, enter the name for your new method.

Visual Assist infers the method signature from the surrounding context and the block to be extracted. In the example, the new method will not have parameters because PI and m_radius are accessible within the Cylinder class. Visual Assist also determines the return type, in this case a double.

After accepting the name and signature, the extracted code is replaced with a call to the new method.

If "Extract to Source" was left unchecked, the new method is added to the appropriate header file, where it can remain or can be moved subsequently with Move Implementation to Source File

If "Extract to Source" was checked, the header file is updated only with a declaration.

And, the implementation is placed in a source file.

Extract from an Existing Free Function

In C/C++, if your selection is part of an existing free function, Visual Assist will insert a function prototype–or the extracted function itself–above the call site. 

In the following example, Extract Method will replace an expression with another free function.

Enter the name of the new function in the dialog.

The new function is placed in the source before the original, and the extracted expression is replaced with a call to the function.

After Extract Method, use Create Declaration on the new function if you need to declare it in a header file.

Extract as Free Function

In C++, if you are extracting a method from code within a class, you have the option of creating a free function instead of creating another method in the class.

After the extract, your function is created above the existing method or function.


Press UNDO once to revert all changes made by Extract Method.

Visual C++ 6.0

Press UNDO multiple times.

Parameter List

Visual Assist infers the parameter list of an extracted method from surrounding context, the code to be extracted, and how variables are used within the block. The process of determining the signature is multi-faceted:

  • Symbols available globally, or within the class of the original and new methods, are not passed via a parameter list
  • Symbols used only in the right side of expressions, i.e. not modified, are passed by value
  • Symbols used in the left side of expressions, i.e. are assigned, are passed by reference
  • Symbol referenced with dot notation within the extracted code, e.g. classes and structs, are always passed by reference
  • When a newly created method assigns a value to only one symbol, that symbol is passed by value and the assignment is done in the original method via return value
  • Symbols local to extracted code become local to the new method; they are not passed

In the following example, strWndText is defined and referenced only within these statements. 

If you select all of the example lines and run Extract Method, only str appears in the parameter list of the new method. strWndText will become local to the new method.

Because str is not used in the left side of an expression within the new method, it is passed by value.

If you do not like the signature of a new method, cancel the dialog, rearrange or modify your selection, and try again.

Local Variables

Use care when including definitions of local variables in code to be extracted. Because the definition is moved to the extracted method, non-extracted references to the variable in the original code will result in compile errors.

Location and Format of New Method

In C++, code in a source file is extracted to the same source file. Code extracted from an inline declaration in a header file is extracted to the header file unless you request the extracted method be placed in the appropriate source file.

In C++, new methods are formatted using the VA Snippet for Extract Method. If you extract from a header to a source, a second format is applied using the VA Snippet for Create Implementation. If you extract from a header file to a header file, you can subsequently move the implementation using Move Implementation to Source File.

In C, code extracted to the same source file is formatted using the VA Snippet for Create Implementation. If you wish to have a declaration for the new method, follow Extract Method with Create Declaration.

In C, code extracted from an inline header file to the same header file is formatted using Extract Method. If you extract from a header file to a source file, a second format is applied using the VA Snippet for Create Implementation

Calls to New Method

There is no automated process by which similar segments of code can be replaced with calls to an extracted method. After using Extract Method to replace one segment with a call to a new method, you must manually locate and modify common segments that might benefit from the new method.