I have this class:
public class User {
private String name;
private int age;
//getters setters
}
I have a method, which updates a user object:
public void foo(User user) {
boolean needUpdate = false;
if(needUpdateName(user.getName())) {
user.setName("new name");
needUpdate = true;
}
if(needUpdateAge(user.getAge())) {
user.setAge(42);
needUpdate = true;
}
if(needUpdate) {
userRepository.update(user);
}
}
It's a simple example, only as an example. How can I refactor this code and remove needUpdate variable?