java pattern: when does it make sense to use temporary variables

How should I evaluate the "cost" of the someMethod() to determine when a temporary variable should be used?

Simply look at the implementation of someMethod(). If it just returns a field, like a typically getter method would do, there's no need to declare a local variable (performance wise). If the method creates objects, calls the database or reads file contents, then is usually wise to cache the return value.

Personally, I don't care of performance issues caused be declaring a temporary variable. I just keep the return value instead of calling a method twice. The code is much easier to read and to understand (unless you name all those variables just temp;-) )


Clearly I can't call someMethod() twice if there are side-effects but when does it make sense to only call it once from an optimization standpoint?

When you've proved that it matters using a profiler. But get used to not calling methods twice in a row anyway, since it will make maintenance easier (what if the check changes and you forget to change it in both places?). Use temporary variables liberally, they're incredibly cheap.