Doing it this way is something you would only ever do if you knew beforehand the user would be asking for the same value over and over, but it’s clean and invisible to the calling code and doesn’t break your assumptions about what the function is going to return.
The memoization pattern is valid too of course, I just find it interesting I’ve always done the same thing when advantageous but in a different way.
I’ve always done it this way:
double calculation(double arg) {
}Doing it this way is something you would only ever do if you knew beforehand the user would be asking for the same value over and over, but it’s clean and invisible to the calling code and doesn’t break your assumptions about what the function is going to return.
The memoization pattern is valid too of course, I just find it interesting I’ve always done the same thing when advantageous but in a different way.