Midnight Pub

Programming a PB&J


The post that inspired this

The above post reminded me of an analogy I once heard to try to explain to non-programmers what programming is actually like.

Suppose you are teaching someone how to make a peanut butter and jelly sandwich. For most people, the instructions would look something like:

This would not work for a computer, though. If we look closer at just that first step, we would need to tell the computer to get the slices of bread from the pantry.

Error: could not find slices of bread. Found: loaf of bread, jar of peanut butter, [...]

There was a logical skip in our instructions: to get the loaf out and take slices from it. The computer doesn't know how to infer that step (ignoring machine learning/artificial intelligence), so it fails.

If we extrapolate this further and remove any assumptions about how to make a sandwich and what to do if the current state is not as we expect (e.g. a door is closed but instructions assume it is open), we get the following list of computer-friendly instructions:


My English teacher once taught this lesson, although I can't remember why (grammar rules?). I felt pretty clever with my list of ~20 steps until she pointed out, rather dramatically, that I hadn't described _how_ to get bread out of the bag by wrenching it apart and scattering slices across the front row of students.



To illustrate how "difficult" it is to program (finnicky may be a better word, if my understanding of English is correct), I will add some steps:

This is just for fun, not to actually "correct" you :)