I think that the exercise High Scores gives us a good opportunity to explain different trade-offs for different approaches. I can think of three different approaches here in terms of the burden of calculating the top 3 values:
- Calculate them on the new method and store them as attributes.
- Lazily calculate them on the first call for either
personal_best
orpersonal_top_three
methods, caching them after that. Here, we have a good opportunity to explain the interior mutability pattern, given that those methods immutably borrow theself
and we cannot simply turn them into mutable references. - Calculate them on each call for either
personal_best
orpersonal_top_three
methods, which does not give us the best performance.
I you want, I can create the 3 approaches and add them in such a way that they will appear here: