2 ms·
This is likely the typical case of related code calling the same function or getter method twice in a context where it is imperative for both calls to return th
by layer8 2y ago
This is likely the typical case of related code calling the same function or getter method twice in a context where it is imperative for both calls to return the same result.
It is reminds me of code like
if someCondition(getFoo())
then doSomethingWith(getFoo())
or even just
doSomethingWith(getFoo())
doAnotherThingWith(getFoo())
which is always a code smell, as opposed to
foo := getFoo()
if someCondition(foo)
then doSomethingWith(foo)
and
foo := getFoo()
doSomethingWith(foo)
doAnotherThingWith(foo)
- pas 2y agoUsually it's called a TOCTOU bug/vuln https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use
- layer8 2y agoI often see it in multiple-time-of-use scenarios as well that need to be consistent, i.e. no check vs. use involved.
- efreak 2y agoShouldn't the compiler optimize this into a single call if they're called so close together?