Is it considered a good programing idiom to use Java varargs as an optional parameter?
Even more: if I have an interface, and some implementations need the additional parameter, and some don't, is it okay to use varargs in the method signature for the optional parameter?
In Java it is possible to use the following idiom:
public static void x(String ... strings)
which gets an array of strings, possibly empty. You could call it with
x() (empty array), x("1","2","3") etc
-
1Why wouldn't you just use two explicit overloads?Oliver Charlesworth– Oliver Charlesworth2012年04月23日 17:55:40 +00:00Commented Apr 23, 2012 at 17:55
-
What exactly do you mean by 'varargs', could you post an example?home– home2012年04月23日 17:57:25 +00:00Commented Apr 23, 2012 at 17:57
-
The second argument is necessary for some implementations of the interface, and not for others. The method is always called with two parameters, but it is not known in advance if the specific instance needs one parameter or twoThlipp– Thlipp2012年04月23日 17:58:50 +00:00Commented Apr 23, 2012 at 17:58
-
1If it works, it's ok. Optional params are one of the use cases for varargs. There is a slight performance penalty to creating an array for the argument, but in 99% cases it's nothing to worry about.Marko Topolnik– Marko Topolnik2012年04月23日 17:59:36 +00:00Commented Apr 23, 2012 at 17:59
-
But wait, if it's always called with two arguments, then why would the second argument be optional? Now you don't make sense.Marko Topolnik– Marko Topolnik2012年04月23日 18:00:59 +00:00Commented Apr 23, 2012 at 18:00
3 Answers 3
Varargs is usually used when you don't know the number of arguments of a "particular type" that the users of the api will like to pass. I don't think there is any problem with that since the user can decide to pass any number of parameter or not to pass any at all. For eg
public class NewClass {
public void print(String... a) {
System.out.println(a);
}
public static void main(String[] args) {
new NewClass().print();
}
}
Doen't hurt. Since you know the type of in the varargs.
Comments
I would say no. Using a varargs will allow any number of arguments to be provided for your optional parameter. How will you communicate to people implementing your interface or calling your method that only one value is expected? What should the behavior be when multiple values are provided? These are unnecessary complications.
If your method requires exactly 0 or 1 value for the optional argument, then you should use a language construct that only allows 0 or 1 value to be provided. It would be more appropriate to overload the method signature or allow the optional parameter to be null.
Comments
I don't think it is a good idea to use varargs to implement optional parameters.
Animal a = new Dog();
a.speak("bow");
You have no idea looking at the reference in above example what arguments should be applied as you may not know that it is a dog if the animal was extracted from say a List of animals.
As said by @Oli explicit overload is a good approach instead.