Home > Mobile >  How to avoid repetitive parameter names (i.e. nearly the same name as an attribute of the object) wh
How to avoid repetitive parameter names (i.e. nearly the same name as an attribute of the object) wh

Time:05-12

I'm VERY new to OOP programming, so I apologize in advance if this is a stupid/irrelevant question

Below is a very simple object constructor in JavaScript

function Class(assign_to_a, assign_to_b){
    this.a=assign_to_a;
    this.b=assign_to_b;
}
object=new Class(2,3);

I do feel like I'm repeating myself a little. I've already named the attribute of the object inside the class, so why do I need to rewrite the same (slightly altered) name on a parameter whose sole purpose is to be assigned to an attribute that already has a descriptive name?

To better understand what I am trying to achieve, JavaScript already offers a neat solution for this:

function Class(){
    this.a=arguments[0];
    this.b=arguments[1];
}
object=new Class(2,3);

This way, I won't have to come up with/rewrite parameter names, when I already know that all the arguments will be assigned to an attribute of the object upon creation.

My problem is, I don't think too many other languages have an arguments object like JavaScript does (though I could be wrong about this), so I will have to find another workaround. Right now, I'm trying to create the same class in Java.

public class Class{
    int a, b;
  
    public Class(int assign_to_a, int assign_to_b){
        a = assign_to_a;
        b = assign_to_a;
    }

    public static void main(String[] args) {
        Class object = new Class(2, 3);
    }
}

Is there a way to work around coming up with repetitive names for the parameters of the object constructor, similar to what I did in JavaScript, or is this something I'll simply have to put up with?

CodePudding user response:

As far as i know you can't avoid specify the arguments in a method signature in Java.

But I think that it's a great feature of Java, because it's check the parameters type at compilation time and avoid all the problems that comes when you pass the wrong arguments to a function.

If you want avoid repetitive and boilerplate code there are lot of libraries to help, like "Project Lombok".

CodePudding user response:

Instead of creating multiple parameters, you could have the constructor take only one argument: an ArrayList. Then you can set the object's private and public properties by pulling them out of the contructor's ArrayList. In Java, this seems to be the closest match to JavaScript's "arguments object" (in terms of type flexibility).

I don't code in Java, but based on your example I assume that it might look something like this:

public class Class{
    int a, b;
  
    public Class(ArrayList ary){
        a = ary[0];
        b = ary[1];
    }

    public static void main(String[] args) {
        ArrayList ary = new ArrayList();
        ary.add(2);
        ary.add(3);
        Class object = new Class(ary);
    }
}
  • Related