3.13 Developing Procedures HW
Exercise 1: Rename the Functions to Be More Descriptive
The lesson’s dessert robot uses names like make_dessert() and bake(), and the poor-naming example uses do_step() for everything. I renamed each function so its name says exactly what it does:
| Old name | New name | Why |
|---|---|---|
make_dessert() |
make_vanilla_cake() |
Says which dessert |
measure_flour() |
measure_flour_cups() |
Says what is measured and in what unit |
mix_ingredients() |
mix_cake_batter() |
Says what the mixing produces |
bake() |
bake_cake_at_350() |
Says the temperature right in the name |
do_step() |
prepare_and_finish_dessert() |
A reader knows what it does without opening it |
Code Runner Challenge
Exercise 1 - Descriptive procedure names
View IPYNB Source
# CODE_RUNNER: Exercise 1 - Descriptive procedure names
def make_vanilla_cake():
measure_flour_cups()
mix_cake_batter()
bake_cake_at_350()
def measure_flour_cups():
print("Measuring 2 cups of flour.")
def mix_cake_batter():
print("Mixing flour, sugar, eggs.")
def bake_cake_at_350():
print("Baking at 350°F for 25 minutes.")
def prepare_and_finish_dessert():
print("Choosing ingredient...")
print("Mixing...")
print("Heating...")
print("Finished!")
make_vanilla_cake()
print()
prepare_and_finish_dessert()
Exercise 2: Convert the Python Functions to Java Methods
In Java, every method lives inside a class. main starts the program and calls makeVanillaCake(), which calls the three step methods.
Code Runner Challenge
Exercise 3 - Split a long procedure and test each piece
View IPYNB Source
public class DessertRobot {
public static void main(String[] args) {
makeVanillaCake();
}
// public: other classes can ask the robot to make a cake
public static void makeVanillaCake() {
measureFlourCups();
mixCakeBatter();
bakeCakeAt350();
}
// private: only DessertRobot can call the individual steps
private static void measureFlourCups() {
System.out.println("Measuring 2 cups of flour.");
}
private static void mixCakeBatter() {
System.out.println("Mixing flour, sugar, eggs.");
}
private static void bakeCakeAt350() {
System.out.println("Baking at 350°F for 25 minutes.");
}
}
Access (public / private):
publicmeans any other class can call the method.makeVanillaCake()is public because it is the one thing other code should ask the robot to do.privatemeans only code insideDessertRobotcan call it. The step methods are private so no one can callbakeCakeAt350()before the batter is mixed. That protects the order of the steps.- Python has no
publicorprivatekeywords. By convention, a name that starts with_, like_bake_cake_at_350(), signals that it is meant for inside use only.
Exercise 3: Split a Long Procedure into Smaller Ones
bake_cake_all_at_once() does everything in one block: it scales the recipe, picks the pan, and works out the bake time. The only way to test it is to read everything it prints.
I split it into three small procedures that each return one value. Now each one can be tested by itself with a known input and expected output, so if a test fails, I know exactly which step is wrong.
# CODE_RUNNER: Exercise 3 - Split a long procedure and test each piece
# Before: one long procedure that does everything and only prints
def bake_cake_all_at_once(servings):
flour_cups = 2 * servings / 8
if servings <= 8:
pan = "8-inch round pan"
else:
pan = "9x13 pan"
minutes = 25 + (servings // 8) * 5
print("Flour:", flour_cups, "cups | Pan:", pan, "| Bake:", minutes, "minutes")
# After: small procedures, each with one job and one return value
def flour_needed(servings):
return 2 * servings / 8
def choose_pan(servings):
if servings <= 8:
return "8-inch round pan"
return "9x13 pan"
def bake_minutes(servings):
return 25 + (servings // 8) * 5
def bake_cake(servings):
print("Flour:", flour_needed(servings), "cups | Pan:", choose_pan(servings),
"| Bake:", bake_minutes(servings), "minutes")
# Each small procedure can be tested on its own
def check(label, actual, expected):
result = "PASS" if actual == expected else "FAIL"
print(result + ":", label, "->", actual)
check("flour for 8 servings", flour_needed(8), 2.0)
check("flour for 16 servings", flour_needed(16), 4.0)
check("pan for 8 servings", choose_pan(8), "8-inch round pan")
check("pan for 12 servings", choose_pan(12), "9x13 pan")
check("bake time for 16 servings", bake_minutes(16), 35)
print()
print("Old version:")
bake_cake_all_at_once(16)
print("New version:")
bake_cake(16)
Both versions print the same result for 16 servings. The difference is testing: the old version gives one printed line to read through, and the split version gives five separate PASS checks, one for each job.