Let's begin with an example (just for the sake of explanation)
public void mySecretOperation() {
User user = new User();
if (user.getAge() > 2 && user.getAge() < 5) {
//TODO : do something...
}
if (user.getAge() > 12 && user.getAge() < 15) {
//TODO : do something...
}
if (user.getAge() > 31 && user.getAge() < 55) {
//TODO : do something...
}
if (user.getAge() > 78 && user.getAge() < 89) {
//TODO : do something...
}
}
Another alternative is int age = user.getAge();
and then use age
everywhere instead of user.getAge()
.
So, performance wise (or let's consider space complexity) will there be any difference?
Can we say that one approach is better than other?
I know its a noob question, still curious to know.