For example if I have method A, which receives 4 parameters
methodA(String,String,Object,Object)
I think it will look cleaner if I use an object to wrap them.
methodA(ParamObject)
But I don't know if it have any disadvantage to the performance?
Performance is the last thing you should worry about when choosing the signature of your API methods. A much more important concern is readability and maintainability of your API, which could be severely influenced by your decision to group parameters together.
First thing to consider is whether grouping some or all of the parameters makes logical sense. For example, if you have an API that looks like this
public void doSomething(String first, String last, Date dob) {
...
}
where the three parameters represent a person's first name, last name, and the day of birth, then changing the API to
public void doSomething(Person p) {
...
}
makes perfect sense.
If, on the other hand, the individual parameters are unrelated to each other, except by virtue of being parameters to the same method, then making a class to enclose them makes sense only if you plan to do something else with the invocation parameters, such as storing them for later use, forwarding them to some other method as a single unit, et cetera.
Otherwise, you should keep individual parameters instead of making a parameter-specific class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With