On many occasions, I have seen the code that invokes the same method on the same object several times within few consecutive lines of code. Let's have a look at the following code snippet:
if(person.getDateOfBirth() != null) {
System.out.println("Date of birth: " + person.getDateOfBirth());
}
When I see this repetition of code I feel like I should refactor it order to remove the duplicity. Refactoring itself would be very easy. The return value would be stored in a local variable and used wherever the original method was invoked.
The example above could be refactored in following way:
final Date dob =person.getDateOfBirth();
if (dob != null) {
System.out.println("Date of birth: " + dob);
}
The reason behind my urge to refactor such code is in performance. Method invocation can repeatedly cause execution of potentially expensive operations such as slow database access, network communication or some lengthly calculations.
If in the example above the date of birth of person object was very unlikely to change between the two getDateOfBirth() method invocations and therefore it was a safe bet to store the result locally in a local variable.
Invoking a method once, storing the result in a local variable could speed the application up. However, it could also break it.
Why would a local store break the application? Well, it may not, it really depends on the invoked method itself and the way our application uses the returned value. If a method performs some side operations it actually may rely on its invocation. Such operations can be for example increasing a counter, retrieving updated stock ticker info, etc. The return value may also depend on the number or time of invocations or other things that may affect it. Another example would be an application that performs business logic based on the most recent data.If the data that method returns varies frequently and business logic depends on it, storing it may not be such a good idea.
I'll give you a very bad example here, but this is the extreme where the result is guaranteed to return a different result almost every time:
if (Math.random() < 0.5) {
// notify developer A
} else if(Math.random() >= 0.5) {
// notify developer B
} else {
// this should never happen,right?
// :-)
// but it does happen with likelihood of 25%
}
So it really brings us down to the point where we have to examine what the method does, what data it returns and what the client does with the returned data.
Conclusion
- Use local store if the invoked method returns consistent results and has no side-effects. Method invocation could be slower or potentially break the client code.
- Use method invocation if the method invoked has side-effects or the data returned changes frequently and the client relies on the latest result.






