Inline Temp

Replaces all references to a local variable with its initial value.

Purpose

  • Inline Temp is useful when you have a simple expression assigned to a variable used only once. In these cases you can often improve the clarity of your code by replacing the local variable with the expression.
  • Inline Temp is also useful if you want to prepare a code block for extraction (to a method or property). If the selected code block references locals declared above that block, you can reduce the codes dependence on local variables by inlining all those references.

Availability

Available from the context menu or via shortcuts:

  • when the edit cursor, or caret is on the declaration or reference of a local variable. The variable should have been assigned only once.

Tips

  • Inline Temp is the functional opposite of Introduce Local.
  • Replace Temp with Query calls Inline Temp after it has extracted a local variable's initialization routine into a method or property.
  • If a variable has assignments other than at initialization, you can use Split Temporary Variable to make Inline Temp available.
  • If a variable is initialized as a result of multiple statements you can first use Extract Method or Extract Property to extract the complete initialization routine to a method or property so that Inline Temp then becomes available.

Example

public int CalculateTotal(int UnitPrice, int Count) 
{
    int total;
    total = UnitPrice * Count;
    return total;
}
Public Function CalculateTotal(ByVal UnitPrice As Integer, ByVal Count As Integer) As Integer
    Dim total As Integer
    total = UnitPrice * Count
    Return total
End Function

Result:

public int CalculateTotal(int UnitPrice, int Count) 
{
    return UnitPrice * Count;
}
Public Function CalculateTotal(ByVal UnitPrice As Integer, ByVal Count As Integer) As Integer
    Return UnitPrice * Count
End Function

Screenshot

rsInlineTempCSharp

See Also