Let’s say I’ve got a type called Superstar. Now I want to have a method that does some work and edits some properties of a Superstar object.
Here are two ways of how I could implement this. Way 1 would be the following:
private Superstar editSuperstar(Superstar superstar){
....
superstar.setEdited(true);
return superstar;
}
...
superstar = editSuperstar(superstar);
And way 2 would be this:
private void editSuperstar(Superstar superstar){
....
superstar.setEdited(true);
}
...
editSuperstar(superstar);
Which one of these two possible ways is considered “best practice”? The first one, or the second pseudo “by reference” one?
In your case, the second form is preferrable, as you directly change one of you superstar properties (
edited). However, if you have a method that use a superstar object and returns an updated version of it (without changing the initial one) the first form will have my favor.Finally, since both of this examples only use Superstar object, they should be member methods of the Superstar class.