A proposal (pre-proposal?) to add parameter groups to Java

Behrang Saeedzadeh behrangsa at gmail.com
Wed Jul 29 07:37:49 UTC 2020


When we apply the Introduce Parameter Object [1
] refactoring on methods that take many arguments we usually have to
define a wrapper class for one or more of the arguments.

For example, we can refactor:

    void line(int x1, int y1, int x2, int y2) {

    }

    line(0, 0, 10, 10);


into:

    class Point {
        private final int x;
        private final int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        // getters
    }

    void line(Point p1, Point p2) {

    }

    line(new Point(0, 0), new Point(10, 10));

Wouldn't it be nicer if we could eliminate the need to define Point
and creating p1 and p2 to wrap x1, y1, x2, and y2
and instead grouped parameters in the method declaration in a way similar
to this:

    void line((int x, int y) p1, (int x, int y) p2) {
        System.out.println(p1.x + " " + p1.y);
        System.out.println(p2.x + " " + p2.y);
    }

    line((0, 0), (10, 10));

What do you think?

P.S: Are there any existing languages with a similar feature?

[1] https://refactoring.guru/introduce-parameter-object

-- 
Best regards,
Behrang Saeedzadeh


More information about the discuss mailing list