If a method populates/modifies an object, would it be preferable to return the object or to keep the return type as void and the method would modify the Object through its reference?
public Obj populate(Obj o)
{
....
return o;
}
public void populate(Obj o)
{
....
}
I know this is a trivial question, but which one is the most preferred?
I depends on your style, but one advantage of returning: you could call
populate(o).doSomethingElse();, i.e. you can chain method calls.Have a look at how
StringBuilderdoes that for example, which allows things like thisnew StringBuilder().append("a").append("b")....