开发者

Long parameter list where a group of parameters can be grouped to an existing class, but these parameters are not gleaned from an object [closed]

开发者 https://www.devze.com 2023-02-22 21:09 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 3 years ago.

Improve this question

The below code is the Long Parameter List Smell method.

// Long Parameter List Smell
public void paint (Graphics gr, 
                   double x, double y, double width, double height,
                   Boolean shouldValidate){.....}

The below code is the caller method calling the above smell.

// Caller
public void someMethod(......){
    double x = someMethod();
    double y = 200;
    double width = 100;
    double height = width/2;
    paint(gr, x, y, width, height, true);
}

We can notice that the parameters double x, double y, double width and double height sound like a Rectangle object. These parameter are not gleaned from an object so we cannot use Preserve Whole Object. The next option is Introduce Parameter Object. However,there is already the class Rectangle2D.Double that is the class in Java Library existing.

Should I group these parameters into the object of Rectangle2D.Do开发者_如何转开发uble? The code that is refactored will be like the below. If it is ok to refactor this way. What can I call this type of refactoring?

// Long Parameter List Smell
public void paint (Graphics gr, 
                    Rectangle2D.Double rec
                    Boolean shouldValidate){.....}


// Caller
public void someMethod(......){
    double x = someMethod();
    double y = 200;
    double width = 100;
    double height = width/2;        
    paint(gr, 
          new Rectangle2D.Double(x, y, width, height), 
          true);
}


How about:

public void someMethod(......){
    Rectangle2D.Double rect = new Rectangle2D.Double(someMethod(), 200, 100, 50);
    paint(gr, rect, true);
}

? Only a little different, but maybe a little more readable.

To answer your question on terminology, how about Consolidate Parameter Object?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号