Home > Refactorings > Refactorings – Decompose Parameter

Refactorings – Decompose Parameter

September 9th, 2011

The Decompose Parameter refactoring splits a single parameter into one or more parameters, depending on the function of the original parameter. The refactoring analyzes how the parameter is used and which of its properties are accessed, after which it can replace a single parameter into several others of the appropriate type for each property being accessed through the original parameter.

If the parameter serves as a container for other objects, it will be split into its internal objects that are used in the code. For example, this allows you to create different overloads of a method and may create the method with the widely used parameters. Consider the following class:

class Square
  public System.Drawing.Size Size { get; set; }
  public System.Drawing.Point Location { get; set; }

And a helper method that returns the area of the squire:

public int GetArea(Square square)
  return square.Size.Height * square.Size.Width;

You can split the squire parameter into two parameters – height and width:

Refactor! Decompose Parameter preview

and get the following method:

public int GetArea(int height, int width)
  return height * width;

The new method allows you to pass the height and width without creating the Square object.

The opposite of the Decompose Parameter refactoring is the Introduce Parameter Object refactoring.

Products: Refactor! Pro
Versions: 11.1 and up
VS IDEs: any
Updated: Sep/09/2011
ID: R038

Similar Posts: