Answers for "calling getter vs local variable"

1

calling getter vs local variable

For most programming languages and envs, consider:

- Memory footprint: Negligible (4-8 bytes?), the local variable is a reference.
- Performance: Depends on the computation of the getter. If a simple getter,
it wont matter because of compiling optimizations.
- DRY: local wins.
- Correctness: matters in multi threaded programs, value of getter may change.

In general try to use local variables if possible.
If your getter is a simple computaiton, can also use it. Avoid long object 
chaning though!
Posted by: Guest on September-24-2021

Browse Popular Code Answers by Language