Extract Method

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 PI and m_radius are accessible within the Cylinder class, and which returns type double.



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, follow Extract Method with another refactoring: Move Implementation to Source File. You see that the method extracted returns the result in place of the selection.

Parameters

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 withn the new method, it is passed by value.


New Method Remains in Header for C/C++

For C/C++, new methods are placed in an appropriate header and in a reasonable location within that header -- typically after the declaration of the original method. The new method can remain in the header for inline execution or moved to a source file using Move Implementation to Source File.

This two-step process minimizes the number of files modified during a single refactoring, keeps UNDO reliable, and simplifies editing of a parameter list. (See Change Signature to refactoring a parameter list.)

Extract from Functions

If your selection is part of a global function in C/C++, the extracted function is placed before the current function. No declaration is created.

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 C++, C# and VB.

If you are programming in pure C, Extract Method creates methods using the VA Snippets entries for Create Implementation.

Navigate after Extract

For C/C++, use the Navigate commands of Visual Assist X to jump between the header and source after Extract Method. Immediately after the extract, Alt+Left returns you to the original method. Alt+Right takes you back to the header.

Miscellaneous

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 again.

Use UNDO to revert the changes made by Extract Method. (One UNDO reverts all changes when using VS2005 or VS.NET. Multiple UNDOs 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.