Skip to main content

Decompose Parameter

  • 2 minutes to read

Purpose

This Refactoring is used to improve encapsulation. It changes the method signature, allowing you to pass only the part or parts of the object instead of the object itself. This Refactoring often helps in promoting reuse by reducing the complexity of the objects required to call a method.

Note

  • Decompose Parameter is a cascading Refactoring. That means that the Refactoring affects all method calls and method declarations in interfaces, base and descendant classes.
  • Decompose Parameter is a cross-language Refactoring. This means that this Refactoring can make required changes in projects in any supported language (see the code snippet below).

Availability

Available when the caret is on a method parameter or argument, assuming it is a class, interface or structure.

Usage

  1. Place the caret on an appropriate item in a method parameters or arguments list.

    Note

    The blinking cursor shows the caret’s position at which the Refactoring is available.

    static bool IsAdult(Person person) => person.age >= 18;
    
    static void Main(string[] args) {
        var andrew = new Person("Andrew Fuller", 34);
        if (!IsAdult(andrew)) return;
        Console.WriteLine($"{andrew.FullName} is adult");
        Console.ReadKey();
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select Decompose Parameter from the menu.

After execution, the Refactoring detects which fields and properties were used by the method and changes the method signature so that it accepts only the required fields. Then it changes the method calls according to the new signature.

static bool IsAdult(int age) => age >= 18;

static void Main(string[] args) {
    var andrew = new Person("Andrew Fuller", 34);
    if (!IsAdult(andrew.Age)) return;
    Console.WriteLine($"{andrew.FullName} is adult");
    Console.ReadKey();
}
See Also