diff --git a/README.md b/README.md index edeb0dc1..ea7ff226 100755 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ documentation](https://docs.rubocop.org/rubocop/configuration.html#inheriting-co * [Regular Expressions](#regular-expressions) * [Percent Literals](#percent-literals) * [Testing](#testing) +* [Memoization](#memoization) ## General @@ -1391,3 +1392,21 @@ documentation](https://docs.rubocop.org/rubocop/configuration.html#inheriting-co do_something end ~~~ + +## Memoization + +* Prefer `return @x if defined?(@x)` over a simple `||=` + +~~~ ruby +# bad - if @merchant is assigned a falsy value (`nil` or `false`) and is called N times, the query will also run N times +def merchant + @merchant ||= Merchant.find_by(id: merchant_id) +end + +# good - A single query, even when @merchant is nil +def merchant + return @merchant if defined?(@merchant) + + @merchant = Merchant.find_by(id: merchant_id) +end +~~~