Algorithms vs. Cake Mixes

Algorithms vs. Cake Mixes
Photo by Annie Spratt / Unsplash

Cooking is a great source of example algorithms and serves as a great stand-in for learning how to code.

One of my first programming instructors began the first day of class by asking for a volunteer. He then got the student to tell him how to make a peanut butter and jelly sandwich for lunch. He followed the instructions literally. As humans we tend to assume people will know to do certain things without being told. The issue arises when we try to program computers with the same assumptions.

Looking at a cake box, you'll probably find a few steps like this one. The first involves preparing the oven and pan, the second describes the process of mixing the ingredients, and the last instructs readers on how to confirm that the cake is made. The instructions are very linear, and keywords are highlighted in bold to help make it clear what needs to be done.

Betty Crocker Cake Box

One interesting thought, though, is that recipes rarely include decision structures and are largely written as linear, step-by-step processes. Why do we assume that someone baking a cake will open the oven's door to insert the cake? The instructions don't say to. Most would say that's just common sense, but what is common sense? Knowing to infer conditions or to read between the lines is what defines intelligence or common sense. Your computer wouldn't know to open the oven's door if this recipe was literally translated to code controlling a robot, but (most) humans would.

As humans, we know to include decision structures within our processes (and recipes), but computers don't. As programmers, we are responsible for translating human processes into code without leaving out any functionality. Decision structures are something most people don't have to consider directly but are key to writing even simple programs and algorithms.