Premature optimization in Java: when to use “x = foo.getX()” vs simply “foo.getX()”

后端 未结 8 2743
醉话见心
醉话见心 2021-02-19 14:40

When I find myself calling the same getter method multiple times, should this be considered a problem? Is it better to [always] assign to a local variable and call only once?

相关标签:
8条回答
  • 2021-02-19 15:15

    That depends on what getX() actually does. Consider this class:

    public class Foo {
        private X x;
    
        public X getX() { return x; }
    }
    

    In this case, when you make a call to foo.getX(), JVM will optimize it all the way down to foo.x (as in direct reference to foo's private field, basically a memory pointer). However, if the class looks like this:

    public class Foo {
        private X x;
    
        public X getX() { return cleanUpValue(x); }
    
        private X cleanUpValue(X x) {
            /* some modifications/sanitization to x such as null safety checks */
        }
    }
    

    the JVM can't actually inline it as efficiently anymore since by Foo's constructional contract, it has to sanitize x before handing it out.

    To summarize, if getX() doesn't really do anything beyond returning a field, then there's no difference after initial optimization runs to the bytecode in whether you call the method just once or multiple times.

    0 讨论(0)
  • 2021-02-19 15:16

    The choice shouldn't really be about performance hit but about code readability.

    When you create a variable you can give it the name it deserves in the current context. When you use a same value more than one time it has surely a real meaning, more than a method name (or worse a chain of methods).
    And it's really better to read:

    String username = user.getName();
    SomeMethod1(a, b, username, c);
    SomeMethod2(b, username, c);
    SomeMethod3(username);
    

    than

    SomeMethod1(a, b, user.getName(), c);
    SomeMethod2(b, user.getName(), c);
    SomeMethod3(user.getName());
    
    0 讨论(0)
  • 2021-02-19 15:18

    I use the second style if it makes my code more readable or if I have to use the assigned value again. I never consider performance (on trivial things) unless I have to.

    0 讨论(0)
  • 2021-02-19 15:21

    Most of the time I would use getX if it was only once, and create a var for it for all other cases. Often just to save typing.

    With regards to performance, the compiler would probably be able to optimize away most of the overhead, but the possibility of side-effects could force the compiler into more work when doing multiple method-calls.

    0 讨论(0)
  • 2021-02-19 15:21

    Two things have to be considered:

    1. Does the call to getX() have any side effects? Following established coding patterns, a getter should not alter the object on which it is called, the in most cases, there is no side effect. Therefore, it is semantically equivalent to call the getter once and store the value locally vs. calling the getter multiple times. (This concept is called idempotency - it does not matter whether you call a method once or multiple times; the effect on the data is exactly the same.)

    2. If the getter has no side effect, the compiler can safely remove subsequent calls to the getter and create the temporary local storage on its own - thus, the code remains ultra-readable and you have all the speed advantage from calling the getter only once. This is all the more important if the getter does not simply return a value but has to fetch/compute the value or runs some validations.

    Assuming your getter does not change the object on which it operates it is probably more readable to have multiple calls to getX() - and thanks to the compiler you do not have to trade performance for readability and maintainability.

    0 讨论(0)
  • 2021-02-19 15:30

    They could be 2 different things.

    If GetX is non-deterministic then the 1st one will give different results than the 2nd

    Personally, I'd use the 2nd one. It's more obvious and less unnecessarily verbose.

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