Refactoring with Extract Method
is useful when a method is too long to understand, or when common blocks of code appear in multiple places. Code is extracted to create a separate method, and the original method is changed to invoke the new method.
In this trivial example, a portion of a calculation is extracted to its own method.
After making the selection and invoking Extract Method
from the refactoring menu, enter the name of the new method. The signature is inferred from the surrounding context. In this example Visual Assist X
creates a method with no parameters, since both
are accessible within the
class, and which returns type
The extracted code is replaced by a call to the new method.
For C/C++, the extracted method is placed in an appropriate header and can remain there for inline execution.
If you prefer the implementation to be in a source file, check the Extract to Source box in the Extract Method dialog.
Visual Assist X
infers the parameter list for a new method from the code being extracted. Symbols available globally, or within the class of the original and new methods, do not need to be 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 new method assigns a value to only one symbol, that symbol is passed by value and the assignment is done via return value in the original method.
Symbols local to extracted code become local to the new method. In the following example, strWndText is defined and referenced only within these statements.
If these statements are extracted to a new method, strWndText is moved to the new method and only str appears in the parameter list. Since str is not used in the left side of an expression within the new method, it is passed by value.
Use care when including local variable declarations in the selected text to be extracted. Since the declaration is moved to the extracted method, referencing the variable in the original code outside the selection will result in a compile error.
New Method Location for C/C++
For C/C++, new methods by default are placed in an appropriate header and in a reasonable location within that header -- typically after the declaration of the original method. To create the method implementation in a source file instead (usually the same file from which you extract the method), check the Extract to Source box in the Extract Method dialog.
Extract from Functions
If your selection is part of a global function in C/C++, Visual Assist X will insert a function prototype or the extracted function itself above the call site.
In the following example, Extract Method
is used to replace an expression with another global function.
After entering the name of the new function, it is placed in the source before the original.
You may use Create Declaration
on the new function if you need to declare it in a header.
Format of Extracted Method
You can modify the format of newly created methods by editing the VA Snippet
for Refactor Extract Method if you are programming in C++, C# or VB. There are separate VA Snippets for each language. If you are programming in pure C, Extract Method
creates methods using the VA Snippets C++ entry for Create Implementation
Cancel the dialog for Extract Method
if you do not like the parameter list in the new method. Rearrange or modify your original code and try Extract Method
Use UNDO to revert the changes made by Extract Method
. (One UNDO reverts all changes when using VS2005 or VS.NET. Multiple UNDOs are required when using VC++ 6.0.)
If you extract a frequently used segment to a new method, there is no automated process by which similar segments in your project are replaced with references to the new method.