Long Parameter List


Symptoms

  • A method has more than one or two parameters.

Causes

You might be trying to minimize coupling between objects. Instead of the called object being aware of relationships between classes, you let the caller locate everything; then the method concentrates on what it is being asked to do with the pieces.

Or a programmer generalizes the routine to deal with multiple variations by creating a general algorithm and a lot of control parameters.

What to Do

  • If the parameter value can be obtained from another object this one already knows , Replace Parameter with Method.

  • If the parameters come from a single object, try Preserve Whole Object .

  • If the data is not from one logical object, you still might group them via Introduce Parameter Object .

Payoff

Improves communication. May expose duplication. Often reduces size.

Contraindications

  • Sometimes, you want to avoid a dependency between two classes. For example, the caller may have the dependency, but you don't want to propagate it. Ensure that your changes don't upset this balance.

  • Sometimes the parameters have no meaningful grouping ”they don't go together.

Notes

This is one of those places where a smell doesn't guarantee a problem. You might smell a Long Parameter List but decide it's right for the situation at hand.

EXERCISE 6 Long Parameter List.

Consider these methods declared in the Java libraries:

From java.swing.CellRendererPane:

 public void paintComponent(Graphics gr, Component renderer,    Container parent, int x, int y, int width, int height,    Boolean shouldValidate) 

From java.awt.Graphics:

 public Boolean drawImage(Image image,     int x1Dest, int y1Dest, int x2Dest, int y2Dest,     int x1Source, int y1Source, int x2Source, int y2Source,     Color color, ImageObserver obs) 

From java.swing.DefaultBoundedRangeModel:

 public void setRangeProperties(     int newValue, int newExtent,     int newMin, int newMax,     boolean isAdjusting) 

From java.swing.JOptionPane:

[View full width]
 
[View full width]
public static int showConfirmDialog(Component parent, Object message, String title, int graphics/ccc.gif optionType, int messageType, Icon icon)
  1. For each declaration above, is there any cluster of parameters you might reasonably group into a new object?

  2. Why might those signatures have so many parameters?

  3. Look back at the JTable declaration (EXERCISE-5, earlier in the chapter). Do you see any clusters of parameters there?

See Appendix A for solutions.




Refactoring Workbook
Refactoring Workbook
ISBN: 0321109295
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net