Having lots of parameters in a constructor

23,825

Solution 1

  1. In general, if you find yourself have too many parameters, that means you don't have enough low-level classes. In your case, you could have a class Name { /* fname, lname, initial, */ } and perhaps a class Contact { /* email, phone */ }

  2. When you extend your class, have the constructor take the base as one parameter, and then add the new extra parameters. (You probably mean Employee will extend Person, rather than vice versa, so public Employee (Person person, Company company, String employeeId) { super(person); this.company = company; this.employeeId = employeeId; }

Good question!

Solution 2

Instead of using telescoping constructor pattern, use builder pattern

public class Person {
    private final String fName;
    private final String lName;
    private final String mInitial;
    private final int age;
    private final String contactNumber;
    private final String emailAddress;

    public Person(PersonBuilder builder) {
       //insert rest of code here 
       fName = builder.fName;
       ...
    }

    public static class PersonBuilder {
        private String fName;
        private String lName;
        private String mInitial;
        private int age;
        private String contactNumber;
        private String emailAddress;
        // setter methods
        public PersonBuilder setFirstName(String name) {
             fName = name;
             return this;
        }
        ...
        // build method
        public Person build() {
            return new Person(this);
        }

    }
}

...

Person p = new PersonBuilder()
              .setFirstName("")
              // set all the setter methods
              .build();

Solution 3

You could decompose Person into Name and Contact.

public class ComposedPerson {
    private Name name;
    private int age;
    private Contact contact;

    public ComposedPerson(Name name, int age, Contact contact) {
        this.name = name;
        this.age = age;
        this.contact = contact;
    }

    public static void main(String... args) {
        Name name = new Name("John");
        Contact contact = new Contact("12345", "[email protected]");
        ComposedPerson person = new ComposedPerson(name, 45, contact);
   }

Sample Name. See how I use a telescoping constructor to allow for optional arguments.

public class Name {
    private String fName;
    private String lName;
    private String mInitial;

    public Name(String fName) {
        this(fName, null, null);
    }

    public Name(String fName, String lName) {
        this(fName, lName, null);
    }

    public Name(String fName, String lName, String mInitial) {
        this.fName = fName;
        this.lName = lName;
        this.mInitial = mInitial;
    }
} 

Solution 4

Yes it is not good to have many parameters in any kind of functions. Maximum parameters should be around 7, according to a book named Code Complete 2.

It is because it will decrease the code readability and maintainability and usability. Imagine other developers working on the same project, how to follow your code?

There are lots of different ways to handle this, factory pattern, for example. It depends on how you design your application.

But in your code, I think it is ok that the no. of parameters is still acceptable (6 parameters)

If your object require so many parameters to instantiate, then you need to rethink how you design your code. For example, can some of the attributes wrap into a separate class? can some of the attribute not necessary pass as a parameter? i.e., get the value from the other class. etc...

Share:
23,825
KyelJmD
Author by

KyelJmD

Software Engineer

Updated on February 22, 2020

Comments

  • KyelJmD
    KyelJmD about 4 years

    Is it wrong to have a lot of parameters inside a constructor? Like 10 to 15 parameters? Because I was designing a class where a constructor will have lots of parameters, for example, a Person class.

    My example person class has 6 parameters in its constructor:

    public class Person {
        private String fName;
        private String lName;
        private String mInitial;
        private int age;
        private String contactNumber;
        private String emailAddress;
    
        public Person(String fName, String lName, String mInitial, int age, String contactNumber, String emailAddress) {
           //insert rest of code here 
        }
    }
    

    Is that wrong? Creating lots of parameters for a constructor?

    Then I am planning to create a class named Employee, then extending it to person class, then it will have a long constructor as well.

    The thing that worries me about is the practice, is this good or what? Any other suggestions?