if I want to memoize the result of a method in a module, is that considered bad practice?
imagine I have a rails helper:
module FooHelper
def values_for_thingie
if some_condition && that_other_condition
{ a: "foo", b: "bar" }
else
{ a: "bar", b: "baz" }
end
end
end
Let's assume a rails view is going to call this helper method multiple times .. one to get the value for "a", and another to get the value of "b"... Let's also assume that some_condition is a method that does something super complicated so we don't want that to get called more than once...
This can be solved by doing:
module FooHelper
def values_for_thingie
@values_for_thingie ||= \
if some_condition && that_other_condition
{ a: "foo", b: "bar" }
else
{ a: "bar", b: "baz" }
end
end
end
However, I don't like how this feels because this instance variable has nothing to do with an instance of FooHelper.. It's not really under its control. And so for that reason, it seems like this is not a good solution.