次のコードでは、呼び出しをlistType.getDescription()
2 回行います。
for (ListType listType: this.listTypeManager.getSelectableListTypes())
{
if (listType.getDescription() != null)
{
children.add(new SelectItem( listType.getId() , listType.getDescription()));
}
}
単一の変数を使用するようにコードをリファクタリングする傾向があります。
for (ListType listType: this.listTypeManager.getSelectableListTypes())
{
String description = listType.getDescription();
if (description != null)
{
children.add(new SelectItem(listType.getId() ,description));
}
}
私の理解では、JVM は元のコード、特にchildren.add(new SelectItem(listType.getId(), listType.getDescription()));
.
2 つのオプションを比較すると、どちらが好ましい方法で、その理由は? それは、メモリ フットプリント、パフォーマンス、読みやすさ/使いやすさ、および今は思い浮かばないその他の点です。
後者のコード スニペットが前者よりも有利になるのはいつですか。つまり、オブジェクトを格納するために常にいくつかのスタック操作が必要listType.getDescription()
になるため、temp ローカル変数の使用がより望ましい場合の呼び出しの (おおよその) 数はありますか?listType.getDescription()
this