Local variable assignment to avoid multiple throws

Recently, the question arose of whether it was a good idea in Java to assign the results of a getter call to a local variable in order to avoid multiple calls of the same access. I can’t find the original post, but it seems that consensus is usually not necessary, since Hotspot optimizes the delay in calling the method anyway.

However, what is the point of view of using this technique to avoid multiple throws? At the moment, I am faced with a choice between:

if (a instanceof Foo) { // Cast once and assign to local variable. Foo foo = (Foo)a; if (foo.getB() == 1 && foo.getC() == 2) { ... } } 

OR

 if (a instanceof Foo) { // Cast twice making code compact but possibly less readable. // Also, is there an overhead in multiple casts? if (((Foo)a).getB() == 1 && ((Foo)a).getC() == 2) { ... } } 
+7
java casting
source share
8 answers

Definitely go first. The performance difference is likely to be irrelevant, but readability will definitely improve.

In addition to removing broadcasts, this also means that you can use a different name. In the end, now you know more about this variable, so it may well make sense to give it a more specific name. This is not always the case, but it may be. The reorganization technique of “introducing a local variable for the sake of naming a value” is underestimated, even without reduction ...

+12
source share

I prefer to create a local variable rather than always casting due to readability issues. Reading the code, for me (or other developers working with the same code), is an important issue.

Concerns about performance at this point amaze me as an example of a "premature optimization" pattern.

+19
source share

I prefer the first one, as it looks cleaner. The second starts to look like Lisp . But this is just a personal opinion.

+3
source share

I would say that it is important not to prematurely optimize. If there is overhead for the casting, it will probably be so small that in practice it will be virtually invisible. If this piece of code did not form most of your application CPU time, I don’t think you will see a measurable performance difference between them.

Therefore, I would also go to the first option, since it looks cleaner and easier to understand and change - it is much more important than performing several measures in 99.99% of situations.

+3
source share

Absolutely a good idea, because it improves clarity. I would say that this also applies to avoiding multiple access calls. This is a good idea for clarity, not for performance.

+1
source share

I prefer the first option for two reasons

  • The necessary brackets make the code clumsy and hard to read.
  • There maybe (small) casting overhead
+1
source share

I prefer the former, not just for reading code, but for the debugging runtime (also for the original example - if you put the getter result in a local one, you see this value, not the trace in getter for the first time).

0
source share

I agree with people who say that the first version is preferable, but I would like to add that if this is possible at all - and this is almost always possible - you should avoid casting in the first place. Not for any performance reasons, but simply for additional code validation.

0
source share

All Articles