Sunday 15 April 2012

Parameter Object

Introduction
  • Make methods parameters more cohesive through the introduction of a parameter object

Consider encapsulating multiple parameters into a parameter object to keep the method signature concise.

1. More expressive method signature

A method containing multiple parameters can lead to confusion with understanding how to satisfy the method and with its implementation details having to trace the order of parameters and relying on poorly named parameter names to track values. Try to group some parameters (if not all) into parameter objects that make sense. This will reduce the parameter list and result in a more expressive method signature.

2. Less validation

The basic parameter validation can be moved from the method to the parameter object by introducing a parameter object. This makes the method’s implementation more concise since it can trust the parameter object to be well constructed. Constructor validation enforced when extracting parameter objects introduces a parameter trusting philosophy rather than a paranoid check for null/empty in every method implementation.

3. Aggregate object

It’s worth reflecting on the domain model to ensure whether the parameters can fit in an existing domain object rather than introducing a parameter object. If one does not exist, the parameter object that needs creation could indeed be missing from the domain model. Consider introducing an aggregate object that will fit within the domain model rather than a parameter object. Do this only if it makes sense and gives me insight into the domain model.


Manual refactoring (REPEAT from Update method name lesson)

Now, let’s see how to become a refactorKing using Eclipse
todo....

No comments:

Post a Comment