Calling getters on an object vs. storing it as a local variable (memory footprint, performance)

后端 未结 6 461
庸人自扰
庸人自扰 2020-11-28 06:25

In the following piece of code we make a call listType.getDescription() twice:

for (ListType listType: this.listTypeManager.getSelectableListTyp         


        
6条回答
  •  广开言路
    2020-11-28 07:25

    I agree on everything. About the readability I'd like to add something: I see lots of programmers doing things like:

    if (item.getFirst().getSecond().getThird().getForth() == 1 ||

    item.getFirst().getSecond().getThird().getForth() == 2 ||

    item.getFirst().getSecond().getThird().getForth() == 3)

    Or even worse:

    item.getFirst().getSecond().getThird().setForth(item2.getFirst().getSecond().getThird().getForth())

    If you are calling the same chain of 10 getters several times, please, use an intermediate variable. It's just much easier to read and debug

提交回复
热议问题