Home > Refactorings > Refactorings – Extract Method

Refactorings – Extract Method

August 13th, 2010

The Extract Method refactoring creates a new method from the selected code block. The selection is replaced with appropriate calling code to invoke the newly-declared method. The Extract Method is great when you need to turn a big, complex method into smaller, simpler ones. Small methods are much easier to maintain, and encourage code reuse, and also have the following advantages:

  • It increases the chance that other methods can use a simple method when the method is finely organized and well-formed.
  • It allows the higher-level methods to read more like a series of comments, which improves the code readability. Simple methods with good names comment themselves, and improve overall code clarity. Overriding also is easier when the methods are finely grained.


The opposite of this refactoring is the Inline Method refactoring.


Refactor! Extract Method preview


Refactor! Extract Method preview

Calling side:

Refactor! Extract Method preview


  • You can choose the newly declared method’s location using the target picker (see Options below).
  • Right after Extract Method has completed, Rename is automatically enabled for all the extracted method’s parameters so you can easily give them appropriate names.
  • Local variables used in the extracted code block are automatically passed to the newly created method as parameters.
  • If a variable hasn’t been assigned before the extracted block and is modified within this block and used after the block, it is passed to the extracted method as an out parameter.
  • If a variable has been assigned before the extracted block, is modified within this block and is used after the block in the source method, it is passed as ref parameter.
  • The extracted method can be organized as a Function instead of Sub in Visual Basic (or as a function rather than a void procedure in C#) if there are out parameters. See Options for more information.
  • If the source method is a function and the extracted code block contains return statements that return this function’s value, the extracted method is also organized as a function having the same return value as the source function.
  • If the extracted code block is an expression, the extracted method will be a function with the appropriate return value type.
  • If the source method is a Sub in Visual Basic (or void function in C#) and the extracted code block contains return statements, then the extracted method includes an additional out parameter that indicates whether the source function should return after the extracted function call. The appropriate code is inserted into the source function automatically.
  • If the extracted method’s return value is assigned to a variable that is referred but not changed later, you can use Inline Temp to replace all variable occurrences with the extracted method call. This will eliminate a redundant local variable.
  • If the extracted method contains an out parameter that you want to make the method’s return value, use the Reorder Parameters refactoring to move the out parameter left until it becomes the return value.
  • You can choose whether the extracted method should be located before or after the source method or property or whether its position should be specified manually by using the target picker.
  • You can choose whether the extracted method should be a Function instead of a Sub in Visual Basic (function instead of a void procedure in C#) to become available only when you have a single out parameter, or available when you have multiple out parameters.
  • If you’ve chosen to generate functions when multiple out parameters are available, you can choose which parameter should be the function’s default return value – the first parameter receiving an assignment, the last parameter receiving an assignment, the first out parameter declared or the last out parameter declared.
  • You can choose whether the Extract Method should be available for code blocks that contain return statements. If so, you can set the name for the variable holding the return flag.
  • You can enable or disable the Intelligent Paste option that allows you to cut code blocks and paste them inside a class or struct automatically enabling Extract Method.
Refactor! Extract Method options page
Products: CodeRush Xpress, Refactor! Pro and free editions of Refactor!
Versions: all
VS IDEs: any
Updated: Mar/14/2011
ID: R010

Similar Posts: