Decompose Parameter
- 2 minutes to read
Replaces a single parameter with one or more new parameters, each representing a property accessed in the original parameter. And appropriately changes all method references.
Decompose Parameter is a cascading refactoring. That is, the refactoring affects all method calls and method declarations in interfaces, base and descendant classes. For instance, if you apply the refactoring to the method declaration within an interface, it also changes the appropriate method declarations in all implementors and all calls to these methods.
#Purpose
Sometimes there may be a situation when you already have all parameters ready to be passed to a method, but the method requires an object that has these values assigned to properties. If this happens a lot in your code, you can remove unnecessary object creation statements by using Decompose Parameter. As a result, the method will accept parameter values directly, instead of obtaining them from an object’s properties.
#Availability
From the context menus or via shortcuts:
- when the caret is on a parameter declaration. The parameter should be a complex type. In this case, this type’s properties referenced within the method body are replaced by newly created parameters and the reference to the complex type is removed.
- when the caret is on a reference to a property of a complex-type parameter. In this case, a newly declared parameter replaces the referenced property. If the method uses other properties of the parameter object, the parameter object is not deleted, so that it still supplies the required values.
Note
The refactoring is available for method calls and method declarations in interfaces, base and descendant classes, as well as for the declaration in the current class.
#Notes
- This refactoring is the functional opposite of Introduce Parameter Object.
#Example
public int GetTotalPrice(│ProductInfo info)
{
return info.UnitPrice * info.Count;
}
Public Function GetTotalPrice(ByVal │info As ProductInfo) As Integer
Return info.unitPrice * info.count
End Function
Result:
public int GetTotalPrice(int unitPrice, int count)
{
return unitPrice * count;
}
Public Function GetTotalPrice(ByVal unitPrice As Integer, ByVal count As Integer) As Integer
Return unitPrice * count
End Function