Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So I find myself doing something like the following pattern often. Instead of:

if (map.containsKey(someKey)) {
    Value someValue = map.get(someKey);
    ...
}

To not traverse the map twice (and since I know my map doesn't store nulls), I'll do:

Value someValue = map.get(someKey);
if (someValue != null) {
    ...
}

This seems to me to be a worthwhile pattern given that the Map operations perform a decent number of operations and the optimizer, I assume, is not smart enough to optimize it away.

But then I find myself doing similar patterns in other situations. For example, should I store the someMethod() result in a temporary variable instead of making the call twice? 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?

if (someObject.someMethod() != null) {
    processSomehow(someObject.someMethod());
}

I know this borders on a "not constructive" question so I'm looking for answers that present some facts and references instead of just conjecture.

  • When does it make sense to do this and when does it not?
  • How should I evaluate the "cost" of the someMethod() to determine when a temporary variable should be used?
  • For simple methods such as get methods, might this get in the way of the hotspot compiler or the optimizer and actually produce less efficient code?

For posterity, I am not asking "how can I improve the speed of my existing program". I am trying to figure out "what pattern should I use when I write future code".

Thanks for any information.

like image 329
Gray Avatar asked Jan 22 '26 22:01

Gray


2 Answers

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;-) )

like image 122
Andreas Dolk Avatar answered Jan 25 '26 10:01

Andreas Dolk


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.

like image 42
Fred Foo Avatar answered Jan 25 '26 10:01

Fred Foo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!