Java Method invocation vs using a variable

前端 未结 14 2419
北海茫月
北海茫月 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.

提交回复
热议问题