lundi 29 décembre 2014

Is it better to pass objects as parameters or as returns in Java? [on hold]

I'm surprised I haven't found more discussion on the subject, but is there a generally-accepted-practice, guideline, or even rule about which way of passing objects is superior in Java?


Example:



public ArrayList<Thing> getThings() {
ArrayList<Thing> temp = new ArrayList<Thing>();
temp.add(new Thing());
temp.add(new Thing());
return temp;
}


Versus



public void getThings(ArrayList<Thing> things) {
things.add(new Thing());
things.add(new Thing());
}


The first way, to me, is clearer but the second way takes advantage of language features. My boss and I have butted heads over this before, and I'd like to know what the programming community's opinion is. Preferably a Java-specific, sourced answer, but I'll take any advice.


Aucun commentaire:

Enregistrer un commentaire