Back to Devexpress

Decompose Parameter

coderushforroslyn-117331-refactoring-assistance-decompose-parameter.md

latest2.2 KB
Original Source

Decompose Parameter

  • Aug 03, 2020
  • 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.

  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.

csharp
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();
}
vb
Function IsAdult(age As Integer) As Boolean
    Return age >= 18
End Function

Sub Main()
    Dim andrew As New Person("Andrew Fuller", 34)
    If Not IsAdult(andrew.Age) Then
        Return
    End If
    Console.WriteLine($"{andrew.FullName} is adult")
    Console.ReadKey()
End Sub

See Also

Make Read-Only

Inline Method and Delete

Widen Scope

Decompose Parameter