Refactorings – Introduce Parameter Object
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.
The opposite of this refactoring is the Decompose Parameters refactoring.
- 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.
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.