2

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
6
  • 1
    Why wouldn't you just use two explicit overloads? Commented Apr 23, 2012 at 17:55
  • What exactly do you mean by 'varargs', could you post an example? Commented 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 two Commented Apr 23, 2012 at 17:58
  • 1
    If 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. Commented 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. Commented Apr 23, 2012 at 18:00

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.