Java Method invocation vs using a variable

前端 未结 14 2285
北海茫月
北海茫月 2020-11-27 02:57

Recently I got into a discussion with my Team lead about using temp variables vs calling getter methods. I was of the opinion for a long time that, if I know that I was goin

相关标签:
14条回答
  • 2020-11-27 03:11

    Another reason to not use a temporary variable to contain the result of a method call is that using the method you get the most updated value. This could not be a problem with the actual code, but it could become a problem when the code is changed.

    0 讨论(0)
  • 2020-11-27 03:13

    The virtual machine can handle the first four local variables more efficiently than any local variable declared after that (see lload and lload_<n> instructions). So caching the result of the (inlined) getter may actually hurt your performance.

    Of course on their own either performance influence is almost negligible so if you want to optimize your code make sure that you are really tackling an actual bottleneck!

    0 讨论(0)
  • 2020-11-27 03:19

    It is not worth if it is just getFoo(). By caching it into a temp variable you are not making it much faster and maybe asking for trouble because getFoo() may return different value later. But if it is something like getFoo().getBar().getBaz().getSomething() and you know the value will not be changed within the block of code, then there may be a reason to use temp variable for better readability.

    0 讨论(0)
  • 2020-11-27 03:21

    It depends. If you would like to make it clear that you use the same value again and again, I'd assign it to a temp variable. I'd do so if the call of the getter is somewhat lengthy, like myCustomObject.getASpecificValue().

    You will get much fewer errors in your code if it is readable. So this is the main point.

    The performance differences are very small or not existent.

    0 讨论(0)
  • 2020-11-27 03:21

    I think that recent versions of the JVM are often sufficiently clever to cache the result of a function call automatically, if some conditions are met. I think the function must have no side effects and reliably return the same result every time it is called. Note that this may or may not be the case for simple getters, depending on what other code in your class is doing to the field values.

    If this is not the case and the called function does significant processing then you would indeed be better of caching its result in a temporary variable. While the overhead of a call may be insignificant, a busy method will eat your lunch if you call it more often than necessary.

    I also practice your style; even if not for performance reasons, I find my code more legible when it isn't full of cascades of function calls.

    0 讨论(0)
  • 2020-11-27 03:27

    Don't forget that by assigning the value of getSomething() to a variable rather than calling it twice, you are assuming that getSomething() would have returned the same thing the second time you called it. Perhaps that's a valid assumption in the scenario you are talking about, but there are times when it isn't.

    0 讨论(0)
提交回复
热议问题