Sunday 15 April 2012

Add Parameter

Introduction
  • Caution when adding a parameter

Although it’s something that happens quite often during a developer’s day, but some questions need to be answered before adding a new parameter to a method.

1. Will the new parameter dilute the method’s cohesion?

A method should do one thing and do it well. At times when additional functionality is needed it’s easiest added to an existing method. Doing so might be in-line with the method’s original intention, which is fine, however it might also just be convenient to add a parameter and additional functionality to the method. In the latter case, it’s best to review the method and perhaps separate additional functionality to its own class/method.

2. Can the new parameter value be found is existing parameter?

When a method’s parameter starts to grow, it’s worth reviewing the existing parameters to consider using a value that they provide to be used directly or used to resolve a value instead of adding an additional parameter. It might be worth to consider adding a the desired value to an existing object only if it makes sense; a natural fit.

3. Would the method benefit from using a parameter object or an aggregate object?

At times a methods require the same data from many different sources, data that does not make sense to exist in a single class. In such a case, a parameter object (typically a simple bean) is used instead of methods with multiple parameters which can lead to client consumption confusion. Another approach is to create an aggregate class that is constructed using multiple class that contain the data needed. The aggregate class is responsible for not exposing containing objects but rather only exposing their relevant data through its own methods. The aggregate approach should only be adopted if the class and the data it exposes make sense to be contained within an aggregate class.

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