Coder’s Cookbook: The complexity of making a bowl of cereal

Renato Francia
4 min readMar 1, 2018
Photo by Jennifer Pallian on Unsplash

Chances are that if you ever went to a kitchen in the morning then you will be either making breakfast or just grabbing a simple bowl of cereal with milk.

The keyword here is “Simple”.

If you are a developer you’ve probably have heard of the PBJ sandwich problem(1) and how simple task can increase in complexity very quickly. Even double if you are not careful with the variables you are handling. For this, I’m going to use the example of a programmer making a simple bowl of cereal to show how complexity can appear and how can it be handled. Furthermore, I’m going to assume that this is going to be a very lazy programmer who is making the cereal and needs a clear set of instructions. After all, it’s the morning.

TLDR;

One of the best skills you can get as developer is to know what will be the output of your code . So make it a habit of always telling what you expect your code will do compared to what is happening. It will be a lot easier to spot the complexity of the problem and guide your towards your intended solution.

So the first thing we want this programmer to do is to go to the kitchen.

  1. Go to the kitchen

The next thing we see is the programmer going to the neighbors kitchen. Try again?

Remember when I mentioned that the instructions have to be a clear set of instructions. I actually meant that the instructions have to be so clear that no other option could be possible. After, this programmer is sleep-walking and can only follow precise instructions, there is no time to think. For the sake of simplicity, let’s assume that this sleep walking programmer has a very good balance and it’s able to grab things without problems. So, let’s try again. This time let’s be more precise

  1. Go to this house’s kitchen.
  2. Find a bowl
  3. Find cereal
  4. Find milk
  5. Put cereal and milk in the bowl
  6. Eat

Let’s run this program. The programmer goes to the kitchen, this time inside the house, then looks for a bowl. He finds the first bowl he finds. The one in the sink with leftover foods. Then, he finds cereal on the floor and milk. Puts everything on the bowl. Bon Appetit! By now you have realized that this is no a good solution to the bowl a cereal. We want something edible and a clean container. Let’s try it again

  1. Go to this house’s kitchen
  2. Find a clean bowl on the counter
  3. Find a box of cereal. Check expiration date.
  4. Find milk in the fridge. Check expiration date.
  5. Put cereal and milk in the bowl.
  6. Eat Cereal

Let’s run these set of instructions. Since we already tested the kitchen, let’s focus on step 2–5. The programmer will find a clear bowl on the counter. Then, find a box of cereal. Checks expiration date. It passes! (Yay). Now checks the milk in the fridge. Expiration day.. Checking… Passes! Now he proceeds to put the cereal box and milk box in the bowl.

While it’s technically correct that cereal and milk is in the bowl, it’s not the intended output we wanted. So let’s add a couple steps that declared that we want the cereal and the milk be literally poured into the bowl without overflowing it. Also, since the box of cereal and the box of milk fitted the bowl, we will need to be more precise on the bowl size.

  1. Go to this house’s kitchen
  2. Find a clean medium-sized bowl on the counter
  3. Find a box of cereal. Check expiration date.
  4. If expiration date is later than current date proceed to step 5. Else Stop everything.
  5. Pour cereal on the bowl at 50% capacity. Then return cereal box to initial position
  6. Find milk in the fridge. Check expiration date.
  7. If expiration date is later than current date proceed to step 8. Else Stop everything!
  8. Pour Milk and fill the bowl 30% capacity.
  9. Eat Cereal

Output: The programmer is happy that he gets to have a nice bowl of cereal. Or does he? The answer is that there is so much complexity and variables we are assuming that when making even a simple task can be challenging. What happens if there would be more than one kind of cereal and milk. How many combinations do we have to check? Or what happens if the programmer lives outside the US were dates are formatted differently.

Complexity can be explicit like a dirty bowl or implicit like a bowl filled with boxes of cereal. As a programmer is your job to think about the variables that are present and test the outcomes. One of the best skills you can get as developer is to know what will be the output of your code and the changes that you make. So make it a habit of always telling what you expect your code will do compared to what is happening. It will be a lot easier to spot the complexity of the problem and guide your towards your intended solution. Good luck!

(1) A problem that consist on telling a machine how to make a PBJ sandwitch by telling instructions step-by-step. The purpose of this exercise is to show new programmers that even a simple task can include great complexity

--

--

Renato Francia

Software Developer, Digital Nomad, Blogger and Tech Enthusiast.