Default method parameter values

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.

publicvoid doStuff(int id)

{

doStuff(id,"",false,Collections.EMPTY_LIST,new Date());

}

publicvoid doStuff(int id, String name)

{

doStuff(id,name,false,Collections.EMPTY_LIST,new Date());

}

publicvoid doStuff(int id, String name,boolean sort)

{

doStuff(id,name,sort,Collections.EMPTY_LIST,new Date());

}

publicvoid doStuff(int id, String name,boolean sort, List values)

{

doStuff(id,name,sort,values,new Date());

}

publicvoid 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:

publicvoid doStuff(int id, String name :"",boolean sort : false, List values : Collections.EMPTY_LIST, Date date :new Date())

{

//Do the stuff here

}

Suggestions?

[2508 byte] By [subSequence] at [2007-9-30 20:10:47]
# 1

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

}

subSequence at 2007-7-7 0:56:23 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 2
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?
kalyan_bh at 2007-7-7 0:56:23 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 3
What we gain?+Less code.+More readable code+Less maintenanceC++ has already this feature, and is pretty popular.We wouldn't have to write 1...N constructorsWe wouldn't have to write 1...N methods for the same thing but with additional
subSequence at 2007-7-7 0:56:23 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 4

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.)

dennisverbeek at 2007-7-7 0:56:23 > top of Java-index,Other Topics,Java Community Process (JCP) Program...