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

后端 未结 8 2693
醉话见心
醉话见心 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: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.

提交回复
热议问题