Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method parameters confusion

Tags:

java

Often time methods take more than 3 parameters which are all of the same type, eg.

void mymethod (String param1, String param2, String param3)

then it's very easy for the client to mix up the parameters orders, for instance inverting param1 and param2:

mymethod (param2, param1, param3);

...which can be the cause of much time spent debugging what should be a trivial matter. Any tips on how to avoid this sort of mistake (apart from unit tests) ?

like image 798
Eleco Avatar asked Sep 15 '26 10:09

Eleco


2 Answers

You are searching for: Named Parameter idiom in java

like image 86
Luca Molteni Avatar answered Sep 17 '26 00:09

Luca Molteni


Use javadoc, it is your friend.

With any good IDE, doing mouse-over the name of the method should give you a tool tip window with useful information.
I know that with Eclipse, if your javadoc makes use of the @param tag, your chances for confusing which parameter is which will go down greatly.

like image 24
JustJeff Avatar answered Sep 16 '26 22:09

JustJeff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!