Java Method invocation vs using a variable

前端 未结 14 2259
北海茫月
北海茫月 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:06

    Never code for performance, always code for readability. Let the compiler do the work.

    They can improve the compiler/runtime to run good code faster and suddenly your "Fast" code is actually slowing the system down.

    Java compiler & runtime optimizations seem to address more common/readable code first, so your "Optimized" code is more likely to be de-optimized at a later time than code that was just written cleanly.

    Note:

    This answer is referring to Java code "Tricks" like the question referenced, not bad programming that might raise the level of loops from an O(N) to an O(N^2). Generally write clean, DRY code and wait for an operation to take noticeably too long before fixing it. You will almost never reach this point unless you are a game designer.

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

    I will not sacrifice "Code readability" to some microseconds.
    Perhaps it is true that getter performs better and can save you several microseconds in runtime. But i believe, variables can save you several hours or perhaps days when bug fixing time comes.

    Sorry for the non-technical answer.

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

    A general comment: In any modern system, except for I/O, do not worry about performance issues. Blazing fast CPUs and heaps of memory mean, all other issues are most of the time completely immaterial to actual performance of your system. [Of course, there are exceptions like caching solutions but they are far and rare.]

    Now coming to this specific problem, yes, compiler will inline all the gets. Yet, even that is not the actual consideration, what should really matter is over all readability and flow of your code. Replacing indirections by a local variable is better, if the call used multiple times, like customer.gerOrder().getAddress() is better captured in local variable.

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

    I am in favour of using temp variable if you are sure about getter will return same value throughout the scope. Because if you have a variable having name of length 10 or more getter looks bad in readability aspect.

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

    I'm +1 for saving the variable. 1) Readability over performance - your code is not just for you. 2) Performance might be negligible but not all the time. I think it is important to be consistent and set a precedent. So, while it might not matter for one local variable - it could matter in a larger class using the same value multiples times or in the case of looping. 3) Ease of changing implementation/ avoiding DRY code. For now you get the value from this one place with a getter and theoretically you use the getter 100 times in one class. But in the future - if you want to change where/how you get the value - now you have to change it 100 times instead of just once when you save it as an instance variable.

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

    I've tested it in a very simple code :

    • created a class with a simple getter of an int (I tried both with final and non-final value for Num, didn't see any difference, mind that it's in the case num never change also...!):

      Num num = new Num(100_000_000);
      
    • compared 2 differents for loops:

      1: for(int i = 0; i < num.getNumber(); ++i){(...)}
      
      2: number = num.getNumber();
      for(int i = 0; i < number; ++i){(...)}
      

    The result were around 3 millis int the first one and around 2 millis in the second one. So there's a tiny difference, nothing to worry about for small loops, may be more problematic on big iterations or if you always call getter and need them a lot. For instance, in image processing if you want to be quick, don't use repetively getters I would advise...

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