thetna
thetna

Reputation: 7143

Does passing many parameters from constructor slows down the program?

I have been desigining classes in following way:

public class add{
      private int firstEntry;
      private int secondEntry;

      public add(int a , int b){
          this.firstEntry= a;
          this.secondEntry=b
      }

      public int makeAddition(){
          return firstEntry+secondEntry;
      }

}

Does this pattern of program slows down program in java?

Upvotes: 1

Views: 671

Answers (3)

user1486826
user1486826

Reputation: 119

passing 2 parameters isn't very much, it won't slow down the program very much. as an alternative to that code, you could have:

public class add{
    public int add(int a, int b){
        return a+b;
    }
}

Upvotes: 0

shem
shem

Reputation: 4712

Passing many parameters from constructor doesn't slow down the program, it slows down the programmer that needs to read it.

Upvotes: 3

Michael Berry
Michael Berry

Reputation: 72254

First off - 2 parameters isn't a lot! And secondly, no it won't slow down the program - what "faster" alternative would you use?

Focus on designing good, readable code and then if (and only if) you need to optimise, you can do that later. The ability to design good, readable code is much more important, and at this stage that's what you should be focusing on.

If you have loads of parameters (you say 15, which is a lot) then potentially look into the builder pattern. Nothing to do with performance, but doing things this way is generally better when you have a large amount of parameters in your constructor since it means when someone calls it, they can clearly see what parameter they're referencing each time.

Upvotes: 6

Related Questions