← Back to course
Code Crew G4 / Week 11 / Friday
5/6
Week 11 Β· Refactor

Friday

The refactored report
// Same output, cleaner code
⏱ about 15 min

Friday: The refactored report

It is time for the big switch. Rocket and Raven write the new station report together. Raven reads each line out loud while Rocket types.

"Now the supplies live in one dictionary," Rocket says. "And one loop calls report for each one." Raven asks, "What do you notice about adding tape now?"

Rocket grins. "One new pair in the dictionary. That is it!" Nova glows with pride. "Great discovery! Let's check that the output still matches."

Version 2

# Station report, version 2
low_line = 20

def total_of(amounts):
    total = 0
    for amount in amounts:
        total = total + amount
    return total

def report(name, amounts):
    total = total_of(amounts)
    print(name + ":", total)
    if total < low_line:
        print("Low on", name.lower())

supplies = {
    "Water": [12, 9, 15],
    "Oats": [4, 6, 2],
    "Fuel": [40, 35, 50],
}
for name in supplies:
    report(name, supplies[name])
print("Report done")
The screen shows
Water: 36
Oats: 12
Low on oats
Fuel: 125
Report done

The output matches version 1 line for line. The refactor worked.

QuestionVersion 1Version 2
Lines that add up a listthree loopsone loop, in total_of
To add tapecopy a whole chunkadd one pair to supplies
To change the low linechange 20 in three placeschange low_line once
Add "Tape": [3, 1, 2] to supplies, after Fuel. How many lines does the program show now?
WHY THIS EXERCISEWith the refactor, new data needs no new code.
PUT THE STEPS OF A SAFE REFACTOR IN ORDER
  • ?Write down the program's output
  • ?Replace each chunk with a call
  • ?Turn one chunk into a function
  • ?Check that the output still matches
  • ?Find the chunks that repeat
WHY THIS EXERCISEEngineers follow these steps so a cleanup never breaks a working program.
Change to version 2Would the output stay exactly the same?
Rename amount to a on both lines?
Change low_line to 10?
Add a comment above report?
WHY THIS EXERCISEKnowing which changes are safe is what makes refactoring safe.
At the computer
This is never required.
If a grown-up has set up Python from python.org, run both versions in IDLE.
Compare the output line by line.

Practice lab: shift totals

def total_of(amounts):
    total = 0
    for amount in amounts:
        total = total + amount
    return total

shifts = {"Rocket": [2, 3], "Raven": [4, 1, 2], "Kai": [5]}
for name in shifts:
    print(name, total_of(shifts[name]))
What number shows next to Rocket?
What number shows next to Raven?
How many lines show?
VERSION 2 THINKING
  • Read the question.
  • Tap your answer.
To add a new crew member, what changes?
What does total_of(shifts["Kai"]) return?
What does total_of([]) return?
StatementTrue or false?
total_of is written once and called three times.?
The loop visits each key in shifts.?
shifts["Raven"] is a number.?
Adding "Ada": [1, 1] adds one more screen line.?
WHY THIS EXERCISEKnowing what each value holds tells you which function to use.
Add "Ada": [3, 3, 3] to shifts. What number shows next to Ada?
WHY THIS EXERCISENew data, same code, correct answer.

Week eleven is done, Crew Engineer! You traced, cleaned and tested a long program. Next week is launch day!

← Thursday