What to assign the result of a method call to a variable?

Welcome! Recently in the tor stumbled on the code requirements. In short, if within the sight are caused by the same static methods of a class, it is recommended to assign a method's output to a variable and use the variable. What does it do can it be? It can be solved easier inside the class assignment of a single calculation of the internal variable and return this value.
April 3rd 20 at 17:44
1 answer
April 3rd 20 at 17:46
In fact, here talking about the fact that it is possible to cache the result of the calculation method, either inside or outside.
Outside is better because:
1) every method call is in any case the overhead if the method is called frequently (e.g. in a loop), this may become noticeable
2) the result of the calculation may depend on external data, then you need to invalidinput cache, outside of this make it easier
Then, in General, and nothing to argue, because if you take the creation of a new variable against multiple invocation... Readability, optimization and so on depends on the specific code, instead of the mentioned principle in the tor.
Would have added caching, and to the method and the code required, but would set the number of retries in the tor, which begins with the use of an additional variable. - bryon42 commented on April 3rd 20 at 17:49
@dora
then you need to invalidinput cache, outside of this make it easier

This logic is not quite correct. In this case, you give the responsibility of the invalidation means to the outside on the outside you need to completely understand the logic of this method, what data it handles and what is the logic of constructing this cache. I.e. a complete disregard for encapsulation.
The second disadvantage of this approach: external code can be a lot. And potentially get an infinite number of sites that deal with invalidation. And then violate the single responsibility principle. Right to have in exactly one place (method) which is responsible for logic and cache when you change this logic in your code will need refactor exactly one place. - carleton commented on April 3rd 20 at 17:52
@pearline.Strosin, on the other hand, still let the caching deals with another object that has no direct relation either to the called method, or to the caller. In C# I would used to do Lazy, for example, if we knew in advance that the result is calculated 1 time and never outdated. But often simpler and clearer to put the output in a variable (because the invalidation is still going on at the time of exit from a block) - Ivory.Donnel commented on April 3rd 20 at 17:55

Find more questions by tags PHPOOP