Instead of having separate functions to add each ingredient, why not have one function?


#1

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.