From 7538b393f0de72427dffc272ee375dd0aba1eeeb Mon Sep 17 00:00:00 2001 From: Jamison Ordway Date: Fri, 27 Oct 2023 10:39:19 -0600 Subject: [PATCH] Error handling review: remove memoization and class vars --- module3/lessons/error_handling_review.md | 25 ++++-------------------- module3/lessons/index.md | 1 + 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/module3/lessons/error_handling_review.md b/module3/lessons/error_handling_review.md index e90e4209..96b473a7 100644 --- a/module3/lessons/error_handling_review.md +++ b/module3/lessons/error_handling_review.md @@ -56,23 +56,6 @@ class SearchFacade end ``` -First, lets isolate our call to our service. - -```ruby -def senate_service - @@service_result ||= CongressService.senate_members -end - -def all_senate_members - senate_service[:results][0][:members] -end -``` -Isolation helps on a few fronts. It adheres more closely to SRP, and we can properly test this method. We can also take advantage of memoization to ensure our we only ever make one external API call per request. - -Because we are memomizing within a class method, we need a class variable to hold the result of our service call. An instance variable or local variable will not memoize properly. - -Now, we can build in some sad path and edge case handling within our facade. - We need to build in a condition that checks if the method `#members_by_last_name` returns an empty array. We can do that by changing our code: ```ruby @@ -85,9 +68,8 @@ def senate_member_by_last_name end def members_by_last_name(last_name) - # By utilizing memoization, the iteration over our all_senate_members array only happens once per request. - @@matches ||= all_senate_members.find_all do |member| - member.last_name == last_name + all_senate_members.find_all do |member| + member[:last_name] == last_name end end ``` @@ -314,9 +296,10 @@ Take a few minutes on your own to answer the following questions: * What do you not like about this approach? * Can you think of a better approach? +The completed code for this lesson is available [here](https://github.com/turingschool-examples/house-salad-7) on the `error_handling_complete` branch. ### Resources * [Rebased on API error handling](https://blog.rebased.pl/2016/11/07/api-error-handling.html) * [Stackify on rescuing exceptions in ruby](https://stackify.com/rescue-exceptions-ruby/) -* [Geeks for Geeks on hanling exceptions in ruby](https://www.geeksforgeeks.org/ruby-exception-handling/) +* [Geeks for Geeks on handling exceptions in ruby](https://www.geeksforgeeks.org/ruby-exception-handling/) diff --git a/module3/lessons/index.md b/module3/lessons/index.md index 980dd438..60811492 100644 --- a/module3/lessons/index.md +++ b/module3/lessons/index.md @@ -47,6 +47,7 @@ layout: page ## Additional Exploration Topics & Resources * [Review of Abstraction and Encapsulation](../archive/lessons/abstraction_and_encapsulation) +* [Review of Error Handling](../lessons/error_handling_review) * [Application Coordination with Message Queues](../archive/lessons/application_coordination_with_message_queues) * [Hash Functions - MD5 and SHA-256](../archive/lessons/hash_functions) * [Understanding Namespacing](../archive/lessons/namespacing)