Often, I come across code where the Getter method is repeatedly used/abused to get some value or pass it as a method parameter, for ex:
public c
I would agree in principle that pre-optimizing is evil, but I still prefer using the 2nd form. The main reason is that it is consistent with other cases other than getters where it would be foolish to repeatedly call it, such as an expensive operation:
if(person.expensiveOp() != null && person.expensiveOp().equalsIgnoreCase("Einstein")) {
method1(person.expensiveOp());
}
method2(person.expensiveOp());
method3(person.expensiveOp());
method4(person.expensiveOp());
Also, with syntax highlighting IDEs, I can isolate all usages of a value. Someone will reply that you could also highlight all usages of the method. But there may be multiple calls to the same method on different instances of the object (i.e. toString()
)