From 75442f276165288caf1f4e3e8f67dd72e738795c Mon Sep 17 00:00:00 2001 From: Jonas Calvi Meinerz Date: Wed, 10 May 2023 10:09:17 +0100 Subject: [PATCH 1/4] Memoization strategy Memoization is useful if you want to run a command once but be able to access that value many times. However, the popular `||=` operator is not great for when your command might return a falsey value. By guarding memoization instance methods with `if defined?`, we make sure it only runs once. --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index edeb0dc1..ac5a2f91 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 the method 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 +~~~ From 7d55778abbd4d691aea505b8f35d5d549749a646 Mon Sep 17 00:00:00 2001 From: Jonas Calvi Meinerz Date: Wed, 10 May 2023 10:22:18 +0100 Subject: [PATCH 2/4] Update README.md Co-authored-by: Nuno Silva --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ac5a2f91..919b0ddd 100755 --- a/README.md +++ b/README.md @@ -1407,6 +1407,6 @@ end def merchant return @merchant if defined?(@merchant) - @merchant ||= Merchant.find_by(id: merchant_id) + @merchant = Merchant.find_by(id: merchant_id) end ~~~ From f670ae2229b6280ed618850976b9d588ac3f4f34 Mon Sep 17 00:00:00 2001 From: Jonas Calvi Meinerz Date: Wed, 10 May 2023 10:22:49 +0100 Subject: [PATCH 3/4] Update README.md Co-authored-by: Nuno Silva --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 919b0ddd..0e4de77d 100755 --- a/README.md +++ b/README.md @@ -1398,7 +1398,7 @@ documentation](https://docs.rubocop.org/rubocop/configuration.html#inheriting-co * Prefer `return @x if defined?(@x)` over a simple `||=` ~~~ ruby -# bad - if the method is called N times, the query will also run N times +# bad - if @merchant is assigned a falsy value (`nil`, `false`, `0`, etc) and is called N times, the query will also run N times def merchant @merchant ||= Merchant.find_by(id: merchant_id) end From 69807fe20a19710e45891055077e5365c0c29447 Mon Sep 17 00:00:00 2001 From: Jonas Calvi Meinerz Date: Wed, 10 May 2023 11:51:05 +0100 Subject: [PATCH 4/4] Update README.md Co-authored-by: Tim Perkins --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0e4de77d..ea7ff226 100755 --- a/README.md +++ b/README.md @@ -1398,7 +1398,7 @@ documentation](https://docs.rubocop.org/rubocop/configuration.html#inheriting-co * Prefer `return @x if defined?(@x)` over a simple `||=` ~~~ ruby -# bad - if @merchant is assigned a falsy value (`nil`, `false`, `0`, etc) and is called N times, the query will also run N times +# 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