Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient parameter passing in method in java [closed]

Tags:

java

I have a method signature like this.

public void calculateFinalInvoice(int a, int b, int c, int d, int e, int f, int g, int h, int i, InvoiceDO invoiceDO ) {
     // TO DO

}

I am passing somany parameters. So is it better to pass all these parameters performance wise. Or 1) maintain a class for all this parameters 2) maintain HashMap for all these parameters 3) maintain an ArrayList for this.

Can you please suggest which one is better to achieve performance.

like image 705
John Avatar asked Oct 05 '22 01:10

John


2 Answers

What version of Java are you using? What are you doing with those parameters. You might be better off moving the DAO to the front of the argument list and then using varargs to pass however many ints you want:

calculateFinalInvoice(InvoiceDO invoiceDO, int args...) {
}
like image 180
Robert Avatar answered Oct 13 '22 10:10

Robert


Do not worry about micro managing efficiency on things like this. Code for clarity and after done coding, worry about efficiency if needed. In this case, you might want to rethink your design and create some more structure to your code. For example, you might want to use arrays :

public void calcualteFinalInvoices(int [] invoices, InvoiceDO action){
  //...
}
like image 45
dchhetri Avatar answered Oct 13 '22 10:10

dchhetri