3.13 Developing Procedures HW
Exercise 1: Rename the Functions to Be More Descriptive
A good procedure name says what it does, so a collaborator never has to open the function to find out. Original names on the left, better names on the right:
| Original | Descriptive |
|---|---|
make_dessert |
prepare_dessert |
measure_flour |
measure_two_cups_of_flour |
mix_ingredients |
mix_flour_sugar_eggs |
bake |
bake_at_350_for_25_minutes |
Code Runner Challenge
Exercise 1 - Rename the functions to be more descriptive
View IPYNB Source
# CODE_RUNNER: Exercise 1 - Rename the functions to be more descriptive
def prepare_dessert():
measure_two_cups_of_flour()
mix_flour_sugar_eggs()
bake_at_350_for_25_minutes()
def measure_two_cups_of_flour():
print("Measuring 2 cups of flour.")
def mix_flour_sugar_eggs():
print("Mixing flour, sugar, eggs.")
def bake_at_350_for_25_minutes():
print("Baking at 350F for 25 minutes.")
# Run the procedure
prepare_dessert()
Actual output: Measuring 2 cups of flour. / Mixing flour, sugar, eggs. / Baking at 350F for 25 minutes.
The output is identical to the original, only the names changed. Now prepare_dessert() reads like a recipe outline.
Bad naming versus good naming
Code Runner Challenge
Exercise 1 - Bad names versus good names
View IPYNB Source
# CODE_RUNNER: Exercise 1 - Bad names versus good names
# Bad: a reader must open the function to learn what it does
def do_step():
print("Choosing ingredient...")
print("Mixing...")
print("Heating...")
print("Finished!")
# Good: the name states the goal
def prepare_and_bake_dessert():
print("Choosing ingredient...")
print("Mixing...")
print("Heating...")
print("Finished!")
print("Calling do_step() (confusing name):")
do_step()
print()
print("Calling prepare_and_bake_dessert() (clear name):")
prepare_and_bake_dessert()
Exercise 2: Convert the Python Functions to Java Methods
Code Runner Challenge
Exercise 3 - Split a long procedure into smaller ones
View IPYNB Source
public class DessertRobot {
// public: other classes can call the main procedure
public static void prepareDessert() {
measureTwoCupsOfFlour();
mixFlourSugarEggs();
bakeAt350For25Minutes();
}
// private: helper steps only this class needs to call
private static void measureTwoCupsOfFlour() {
System.out.println("Measuring 2 cups of flour.");
}
private static void mixFlourSugarEggs() {
System.out.println("Mixing flour, sugar, eggs.");
}
private static void bakeAt350For25Minutes() {
System.out.println("Baking at 350F for 25 minutes.");
}
public static void main(String[] args) {
prepareDessert();
}
}
Access modifiers:
publicmeans any other class can call the method.prepareDessert()is the one entry point a caller needs.privatemeans only code insideDessertRobotcan call the method. The three helpers are implementation details, so hiding them lets us change how the steps work later without breaking anyone who callsprepareDessert().- Java also needs a return type (
voidmeans it returns nothing),staticso it can be called without creating an object, and method names in camelCase by convention (prepareDessert) instead of Python’s snake_case.
Exercise 3: Split a Long Procedure into Smaller Ones
One long procedure that does everything is hard to read and hard to test. Here it is split into four small procedures with clear names, and each one can be tested on its own.
# CODE_RUNNER: Exercise 3 - Split a long procedure into smaller ones
# BEFORE: one long procedure. To test heating, you must run everything.
def make_dessert_all_in_one():
ingredient = "flour"
print("Choosing ingredient: " + ingredient)
print("Mixing " + ingredient + " into the bowl.")
print("Heating the oven to 350F.")
print("Dessert is ready!")
# AFTER: small procedures, each does one job
def choose_ingredient():
return "flour"
def mix(ingredient):
return "Mixing " + ingredient + " into the bowl."
def heat_oven():
return "Heating the oven to 350F."
def finish():
return "Dessert is ready!"
def prepare_dessert():
ingredient = choose_ingredient()
print("Choosing ingredient: " + ingredient)
print(mix(ingredient))
print(heat_oven())
print(finish())
prepare_dessert()
# Testing is easier: each small procedure can be checked by itself
print()
print("Tests:")
print("choose_ingredient() returns flour:", choose_ingredient() == "flour")
print("mix('sugar') is correct:", mix("sugar") == "Mixing sugar into the bowl.")
print("heat_oven() is correct:", heat_oven() == "Heating the oven to 350F.")
print("finish() is correct:", finish() == "Dessert is ready!")
Why splitting helps:
- Testing: Each small procedure returns a value, so one line can check it (
mix("sugar") == "Mixing sugar into the bowl."). With the all-in-one version, you can only watch printed text. - Debugging: If the oven message is wrong, the bug is in
heat_oven(), not somewhere in 20 lines. - Reuse:
mix()works for any ingredient, so a second recipe can call it without copying code. - Readability:
prepare_dessert()now reads like a list of steps.
Note on the mini-game: the lesson’s mini-game cell uses input(), which the code runner cannot answer, so it is not included here. Run it in a local Jupyter notebook if you want to try it.