Code Rescue
This past week, I created my first command line application that returned data from an API. My partner and I used the Yummly API (tip: you can bypass paying $500 for it if you apply to access it as a student and get approval) to create Méli-Mélo. The app name is derived from the French phrase which roughly translates to “hodgepodge.” Our idea was to create a program that, when given three food ingredients from the user, would return recipes to create from the input. In that vein, a user can create something that benefits him/her out of what seems like a random assortment of unrelated things.
Creating an application is generally quite fun but the real task to overcome is taking into account edge cases that may break the code. By being forced to compensate for any errors from the user end, there is a lot more room for something to go wrong and shut down the application entirely. The usability of the program was important because such a superficial program that does not account for user errors will not function well in the real world.
The biggest issue was when a user put in ingredients that either were not words that existed in the English language or ones that were viable words but had nothing to do with food (i.e., putting “help” in as an ingredient). In the application, when the API call is created with the user input of ingredients, a new object is instantiated from the Recipe class with the name attribute set as the value of the “recipeName” key from the hash returned from the API. If the input is not a viable food ingredient and passed into the search query, no hash is returned from the API and therefore, no object is instantiated.
Let’s look at a detailed example to explain the above. Below, I put in “need,” “more,” and “sleep” as my three recipe ingredients in my application which of course are not ingredients any person could ever cook with.
Since a recipe object was never instantiated due to the lack of return from the API, I cannot call the name method on an object that does not exist. But all is not lost. The error message is key in figuring out how to debug any problem in code.
Get Laura Kim’s stories in your inbox
Join Medium for free to get updates from this writer.
The error output highlights exactly where the code is breaking. As a rule of thumb, whenever you run into an error, try to figure out what it is conveying as opposed to jumping right back into your code and throwing a pry into a random place. In line 8 below, the return_recipe method calls “.name” on the instance variable “recipe_obj.” Just as the error message output, you cannot call on the name method on something that is nil. Knowing exactly where the application was shutting down is absolutely crucial to rescue it.
“Rescue” stops the shutdown from occurring where the error in your code is and will run the code in the block below instead. To rescue Méli-Mélo, I put the “begin” where I know the error occurs, follow up with “rescue” immediately after, and call on the “rescue_from_breaking” method. The “rescue_from_breaking” method outputs a message to prompt the user to enter their ingredients again and loops back to the beginning of the application that asks for the very first ingredient.
In this case, the code inside my rescue block is a call to a different method but that method could just as easily have been in the “return_recipe” method instead. Abstracting the code into a separate method that I can call on as needed helps in simplifying code and increasing readability.
The above shows how “rescue” essentially saved my application. It did not return a recipe object for my ingredients “chug,” “a,” and “redbull” but it also did not throw an error message and break. It output a message notifying the user that those were not valid food ingredients and loops the application to one of the beginning methods to give the opportunity for the user to restart with the proper ingredients. This code does not just account for the edge case of random input, but also if the user may accidentally spell an ingredient wrong. Can you, at any given time, spell “Worcestershire” correctly without looking it up?
Rescue is a valuable tool in helping coders understand what is truly happening in their code. It forces you to analyze what is being output and how to debug the problem. Besides, it is visually much more pleasing to receive a single string, “Error,” as opposed to a huge block just highlighting how wrong your code is.




