Home > Refactorings > Refactorings – Introduce Parameter Object

Refactorings – Introduce Parameter Object

August 13th, 2010

The Introduce Parameter Object refactoring consolidates selected parameters into single object. If you frequently need to pass similar sets of values to methods that tend to be passed together, it might be useful to encapsulate these values into an object that carries all of this data. It is worthwhile to turn these parameters into objects just to group the data together. As the result, calling statements will become more compact and you will be able to add data processing logic to the newly declared object. This refactoring is also useful because it reduces the size of the parameter lists, and long parameter lists are hard to read and understand.

The most useful benefit of this refactoring is that once you have clumped together the parameters, you’ll soon see behavior that you can also move into the new object. Often the method bodies have common manipulations of the parameter values. By moving this behavior into the new object, you can remove a lot of duplicated code.

Opposite:

The opposite of this refactoring is the Decompose Parameters refactoring.

Sample:

Refactor! Introduce Parameter Object preview

Result object:

Refactor! Introduce Parameter Object preview

Calling side:

Refactor! Introduce Parameter Object preview

Notes:

  • This refactoring automatically declares a new class or structure. The created object has a constructor that initializes all properties to values passed as parameters. All initialized values can be accessed via public read-only properties, which replace simple parameters in the original code. Note that Introduce Parameter Object uses public read-only fields instead of properties. See Options below.
  • All method calls are automatically updated to pass the newly declared object instead of multiple parameters.

Options:

The Editor | Refactoring | Introduce Parameter Object page of the Options Dialog allows you to specify whether to declare a class or a struct, where to declare it, and whether it should have properties or fields in it.

Refactor! Introduce Parameter Object options page

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

Similar Posts: