Question
In the context of this exercise, instead of having separate functions to add each ingredient, why not have one function to add all the ingredients?
Answer
You absolutely can!
However, there are some reasons why you might keep the functions separate.
Usually, we want code to be efficient and keep functionality separate. By keeping the functions separate for adding each ingredient, this can be accomplished.
For instance, what if we didn’t want to add mushrooms or cheese to our set of orders? If we had one function that added all the ingredients, we would have to change the function itself, or create a new function to add just what is in the orders. Ultimately, this would end up separating the functionality anyway, so this might not be a good idea.
Furthermore, by keeping the functions separate, if we need a new function for a new ingredient, it is simple because we just need to create a function with the same functionality, but for a different ingredient.
We could even apply these functions to foods other than burgers, like if we wanted to add the ingredients to salads or even soups.
8 Likes
Would it be more efficient to create a list of available ingredients in one “add” function and specify ingredients when running the function? Or would that make it more complicated rather than less?
4 Likes
you could even argue:
on_the_griddle = [["eggs"], ["eggs"], ["eggs"], ["eggs"]]
def _add_ingredient(being_cooked, ingredient):
for item in being_cooked:
item.append(ingredient)
print(being_cooked)
def add_spinach(being_cooked):
_add_ingredient(being_cooked, "spinach")
def add_mushrooms(being_cooked):
_add_ingredient(being_cooked, "mushroom")
def add_cheese(being_cooked):
_add_ingredient(being_cooked, "cheese")
# Paste your code on the lines below:
add_spinach(on_the_griddle)
add_mushrooms(on_the_griddle)
add_cheese(on_the_griddle)
which means you have separate functions for adding the ingredient, taking away the need to worry about the value of the argument for ingredient
, which might be nice if you need to call the functions a lot.
Now you have the ingredients at one place, you won’t create inconsistency (within a team of developers) that someone writes mushroom while someone else writes mushrooms.
on the other hand, worrying too much about very small details might cost more time then advantages gained. Always a tricky balancing act
32 Likes
Very smart solution. One function is programmed as a fully loaded omelette. Thanks for the post.
1 Like
from the idea to the real coding to make that idea is hard 
Did you came up with that just from the fundamentals course?
1 Like
I was wondering the same thing.