Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method parameters with same type

Tags:

java

methods

I am wondering what is the best practices to write method that takes at least three parameters of the same type. The problem is that you can mix up parameters when using such method. For example

method(int userId, int productId, int weight, int price)

The only solution I see is to use some holder class(maybe with builder pattern used) and passing it as method parameter.

like image 797
Andrii Andriichuk Avatar asked Nov 01 '25 01:11

Andrii Andriichuk


1 Answers

The best practice is to use clear parameter names (and possibly clarifying them in the Javadoc). If there are too many parameters, a separate object is better.

If the caller of the method can't distinguish between different (well named) parameters, then maybe he shouldn't be calling the method in the first place.

like image 71
Kayaman Avatar answered Nov 02 '25 16:11

Kayaman