Skip to main content

Extract Method

  • 5 minutes to read

Moves the selected code to a new method. Inserts the appropriate calling code into the source method or property.

#Purpose

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.

#Availability

Available from the context menu or via shortcuts:

  • when one or more statements are selected.
  • when an expression is selected.

Using the Intelligent Paste feature:

  • When a qualifying code block is cut from a method and pasted inside a struct or a class it will be automatically converted into a method, and a call to that method will be added to the location the code was cut from.

#Notes

  • You can choose the newly declared method’s location using the target picker (see Settings 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. (See additional information below.)
  • 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 Settings 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.
  • Extract Method is also called when you invoke the Replace Temp with Query refactoring when the cursor is on the left-hand side of an assignment that has an expression on its right-hand side. The expression is extracted into a new method and all the local variable occurrences are replaced with the extracted method call.

#Tips

  • 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.
  • The Extract Method refactoring can be automatically called by the Replace Temp with Query refactoring. Refer to the documentation for the Replace Temp with Query refactoring for additional information.
  • 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.
  • Extract Property and Introduce Local refactorings are similar to Extract Method in functionality.

#Options

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

#Example

public int TotalPrice(ProductInfo info)
{
    bool a = info.count > 10;if (a)
        info.unitPrice = 50;
    else 
        info.unitPrice = 70;
    return info.unitPrice * info.count;
}
Public Function TotalPrice(ByVal info As ProductInfo) As Integer
    Dim a = info.count > 10If a Then
        info.unitPrice = 50
    Else
        info.unitPrice = 70
    End If
    Return info.unitPrice * info.count
End Function

Result:

private static int TotalPriceExtracted(ProductInfo info, bool a)
{
    if (a)
        info.unitPrice = 50;
    else
        info.unitPrice = 70;
    return info.unitPrice * info.count;
}
public int TotalPrice(ProductInfo info)
{
    bool a = info.count > 10;
    return TotalPriceExtracted(info, a);
}
Private Shared Function GetPrice(ByVal info As ProductInfo, ByVal a As Boolean) As Integer
    If a Then
        info.unitPrice = 50
    Else
        info.unitPrice = 70
    End If
    Return info.unitPrice * info.count
End Function
Public Function TotalPrice(ByVal info As ProductInfo) As Integer
    Dim a = info.count > 10
    Return GetPrice(info, a)
End Function

#Screenshot

rsExtractMethodCSharp

See Also