This discussion is archived
4 Replies Latest reply: May 16, 2006 9:03 AM by 843853 RSS

Default method parameter values

843853 Newbie
Currently Being Moderated
I really, really think that they should incorporate default method parameter values in the next major release.

It is time-consuming, obstructing and confusing to have methods with 1...n parameters.

I.e.
public void doStuff(int id)
{
    doStuff(id,"",false,Collections.EMPTY_LIST, new Date());
}
public void doStuff(int id, String name)
{
doStuff(id,name,false,Collections.EMPTY_LIST, new Date());
}
public void doStuff(int id, String name, boolean sort)
{
doStuff(id,name,sort,Collections.EMPTY_LIST, new Date());
}
public void doStuff(int id, String name, boolean sort, List values)
{
doStuff(id,name,sort,values, new Date());
}
public void doStuff(int id, String name, boolean sort, List values, Date date)
{
doStuff(id,name,sort,values, date);
}
Not to mention the amount of JavaDoc-tags it requires.


Wouldn't it just be simpler, more understandable and easier to have it as:
public void doStuff(int id, String name : "", boolean sort : false, List values : Collections.EMPTY_LIST, Date date : new Date())
{
    //Do the stuff here
}
Suggestions?
  • 1. Re: Default method parameter values
    843853 Newbie
    Currently Being Moderated
    The last: "
    public void doStuff(int id, String name, boolean sort, List values, Date date)
    {
        doStuff(id,name,sort,values, date);
    }
    "

    should obviously be:
    public void doStuff(int id, String name, boolean sort, List values, Date date)
    {
        //Do your stuff
    }
  • 2. Re: Default method parameter values
    843853 Newbie
    Currently Being Moderated
    As far as I understand the problem, its talking about having default values to the parameters passed in method declaration. If Iam right whats the value addition we get doing this?
  • 3. Re: Default method parameter values
    843853 Newbie
    Currently Being Moderated
    What we gain?

    +Less code.
    +More readable code
    +Less maintenance


    C++ has already this feature, and is pretty popular.

    We wouldn't have to write 1...N constructors
    We wouldn't have to write 1...N methods for the same thing but with additional parameters.
  • 4. Re: Default method parameter values
    843853 Newbie
    Currently Being Moderated
    I agree that getting default method parameter values implemented will be a good thing.

    If you really have a large number of methods (>5) that usally just add a parameter with a default value, you could introduce a "Parameter Object" that contains the defaults for the parameters. The drawback is of course an extra indirection to get to the parameter.
    class MyParameterClass() {
      String name = "";
      boolean sort = false;
      List values = Collections.EMPTY_LIST;
      Date date = new Date();
    }
    
    myMethod(String arga, String argb) {
       return myMethod(arga, argb, new MyParameterClass());
    }
    
    myMethod(String arga, String argb, MyParameterClass params) {
    ...
    }
    arga and argb are there to illustrate you might have parameters you don't have defaults for. (Yes, you could include them in the constructor of MyParameterClass instead.)