1.6 Conditional processing
When you write code, you create instructions for a computer that state what steps the computer must take to accomplish some task. In that sense, you are specifying a recipe. Depending on some conditions, like if a bank balance is positive or a car’s speed exceeds a stated limit, the sequence of steps may vary. We call how your code moves from one step to another its flow.
Let’s make some bread to show where a basic recipe has points where we should make choices.
Ingredients
- 1 ½ tablespoons sugar
- ¾ cup warm water (slightly warmer than body temperature)
- 2 ¼ teaspoons active dry yeast (1 package)
- 1 teaspoon salt
- ½ cup milk
- 1 tablespoon unsalted butter
- 2 cups all-purpose flour
Straight-through recipe
- Combine sugar, water, and yeast in a large bowl. Let sit for 10 minutes.
- Mix in the salt, milk, butter, and flour. Stir with a spatula or wooden spoon for 5 minutes.
- Turn out the dough onto a floured counter or cutting board. Knead for 10 minutes.
- Butter a large bowl, place the dough in it, turn the dough over to cover with butter.
- Cover the bowl and let it sit in a warm place for 1 hour.
- Punch down the dough and knead for 5 minutes. Place in a buttered baking pan for 30 minutes.
- Bake in a pre-heated 375° F (190° C, gas mark 5) oven for 45 minutes.
- Turn out the loaf of bread and let it cool.
Problems
I call that a “straight-through” recipe because you do one step after another from the beginning to the end. The flow proceeds in a straight line with no detours. However, the recipe is too simple because it does not include conditions that must be met before moving to later steps.
- In step 1, your bread will not rise if the yeast is dead. We must ensure that the mixture is active and foamy.
- In step 2, we need to add more flour in little portions until the stirred dough pulls away from the bowl.
- In step 3, if the dough gets too sticky, we need to add even more flour.
- In step 5, an hour is only an estimate for the time it will take for the dough to double in size. It may take more or less time.
- In step 6, we want the dough to double in size in the loaf pan.
- In step 7, 45 minutes is an estimate for the baking time for the top of the bread to turn golden brown.
Conditional recipe
- Combine sugar, water, and yeast in a large bowl. Let sit for 10 minutes. If the mixture is foamy, then continue to the next step. Otherwise, discard the mixture, and combine sugar, water, and fresh yeast in a large bowl. Let sit for 10 minutes.
- Mix in the salt, milk, butter, and flour. Stir with a spatula or wooden spoon for 5 minutes. If the mixture does not pull aside from the sides of the bowl, then add 1 tablespoon of flour and stir for 1 more minute. Otherwise, continue to the next step.
- Turn out the dough onto a floured counter or cutting board. Knead for 10 minutes. If the mixture is sticky, then add 1 tablespoon of flour and stir for 1 more minute. Otherwise, continue to the next step.
- Butter a large bowl, place the dough in it, turn the dough over to cover with butter.
- Cover the bowl and let it sit in a warm place for 1 hour. If the dough has not doubled in size, then let it sit covered in the bowl for 15 more minutes. Otherwise, continue to the next step.
- Punch down the dough and knead for 5 minutes. Place in a buttered baking pan for 30 minutes. If the dough has not doubled in size, then let it sit covered in the baking pan for 15 more minutes. Otherwise, continue to the next step.
- Bake in a pre-heated 375° F (190° C, gas mark 5) oven for 45 minutes. If the top of the bread is not golden brown, then bake for 5 more minutes. Otherwise, continue to the next step.
- Turn out the loaf of bread and let cool.
Figure 1.2 is a flowchart, and it shows what is happening in the first step. The rectangles are things to do, and the diamond is a decision to be made that includes a condition.
You may find it useful to use such a chart to map out the flow of your code.
These instructions are better. We test several conditions to determine the next course of action. We still are not checking whether a condition is met after repeating a step multiple times.