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

后端 未结 6 463
庸人自扰
庸人自扰 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:13

    I would agree with the local variable approach for readability only if the local variable's name is self-documenting. Calling it "description" wouldn't be enough (which description?). Calling it "selectableListTypeDescription" would make it clear. I would throw in that the incremented variable in the for loop should be named "selectableListType" (especially if the "listTypeManager" has accessors for other ListTypes).

    The other reason would be if there's no guarantee this is single-threaded or your list is immutable.

提交回复
热议问题