What is wrong with changing an object, which was passed to another method?, eg:
public void doSomething(User user){
edit(user);
//...
EntityManager.persist(user);
}
public void edit(User user){
//...
user.setName("new name");
}
In general everyone says that the form below would be the "most correct"
public void doSomething(User user){
User updatedUser = edit(user);
//...
EntityManager.persist(updatedUser);
}
public User edit(User user){
//...
user.setName("new name");
return user;
}
But what I do not understand is why return the same instance of the changed object? Some say that this is less prone to errors, because in the first way you end up losing the "trace" of where the object is being altered. Honestly this argument did not convince me.
Is there any "good practice" for such cases, or is it really an ideological developer issue?