Refactoring code to improve it

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Refactoring code to improve it

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which best describes refactoring?
A Adding new features to a program so it does more things
B Rewriting working code to be clearer or simpler without changing what it does
2. After refactoring a program, what should you do next?
A Assume it works because you only changed the layout
B Re-run your test cases to check it still gives the same results
3. Which change is an example of refactoring?
A Renaming x to totalPrice so the variable's purpose is clear
B Changing the program so it now also calculates GST
4. A program has the same three lines copied in five places. What is the best refactoring?
A Move the repeated lines into one function and call it where needed
B Copy and paste the lines again so each place is self-contained
5. You rename a variable from n to numStudents. What should the program's output be afterwards?
A Exactly the same as before
B Slightly different because the name is longer

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1def calc(a, b): -> def calc_area(width, height): AThe tests were not re-run, so there is no proof the refactored code still works. Always re-test after refactoring.
2def show_total(price): print("Total: $", price) Bx = w * h -> area = width * height
3After refactoring, re-run the same test cases to check the program still gives the same results Cassert calc_area(3, 4) == 12
4Refactoring changes how the code is written but not what it does DRenaming a function and its variables so the names show their purpose, without changing the result
5area = width * height -> area = width + height EThe calculation was changed, so the program now behaves differently. That is not refactoring because refactoring must keep the same behaviour.
6Refactor the code, then hand it in without running the tests FA repeated print line moved into one function that can be called many times

Definitions

refactoring

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
__________ = price * qty print(totalPrice)
2.
___ showTotal(price, qty): return price * qty
3.
def _________(price, qty): return price * qty print(showTotal(4, 3))
4.
def showTotal(price, qty): return price * qty assert showTotal(2, 3) == _
5.
before = 12 after = showTotal(4, 3) print(before == _____)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def total_price(price, qty):
B return price + qty
C print(total_price(4, 3))
D # expected output: 12
Question 2
A width = 5
B height = 3
C area = width * height
D print(areas)
Question 3
A def greet(name):
B msg = 'Hi ' + name
C print(mesg)
D greet('Sam')
Question 4
A def show_total(cost):
B gst = cost * 0.1
C print(cost + gst
D show_total(20)
E show_total(35)
Question 5
A def average(a, b)
B total = a + b
C return total / 2
D print(average(6, 10))

Answer Key

Refactoring code to improve it
Section A: Quick Questions
Q1
B — Rewriting working code to be clearer or simpler without changing what it does
Q2
B — Re-run your test cases to check it still gives the same results
Q3
A — Renaming x to totalPrice so the variable's purpose is clear
Q4
A — Move the repeated lines into one function and call it where needed
Q5
A — Exactly the same as before
Section B: Match the Pairs
Q1
def calc(a, b): -> def calc_area(width, height): → Renaming a function and its variables so the names show their purpose, without changing the result
Q2
def show_total(price): print("Total: $", price) → A repeated print line moved into one function that can be called many times
Q3
After refactoring, re-run the same test cases to check the program still gives the same results → assert calc_area(3, 4) == 12
Q4
Refactoring changes how the code is written but not what it does → x = w * h -> area = width * height
Q5
area = width * height -> area = width + height → The calculation was changed, so the program now behaves differently. That is not refactoring because refactoring must keep the same behaviour.
Q6
Refactor the code, then hand it in without running the tests → The tests were not re-run, so there is no proof the refactored code still works. Always re-test after refactoring.
Section C: Definitions
Q1
refactoring — Rewriting working code to make it clearer or simpler (e.g. renaming variables, moving repeated lines into a function) without changing what it does, then re-running tests.
Section D: Fill in the Blanks
Q1
totalPrice = price * qty print(totalPrice)
Q2
def showTotal(price, qty): return price * qty
Q3
def showTotal(price, qty): return price * qty print(showTotal(4, 3))
Q4
def showTotal(price, qty): return price * qty assert showTotal(2, 3) == 6
Q5
before = 12 after = showTotal(4, 3) print(before == after)
Section E: Spot the Error
Q1
B — Refactoring must not change what the code does. Changing * to + gives 7 instead of 12, so the tests would fail. It should be price * qty.
Q2
D — The variable was renamed to area, but this line still uses the old name areas, which causes a NameError. It should be print(area).
Q3
C — After renaming a variable you must change every use of it. The variable is msg, not mesg. It should be print(msg).
Q4
C — The closing bracket is missing, which is a syntax error, so the new function will not run. It should be print(cost + gst).
Q5
A — A function definition must end with a colon. Without it the refactored code will not run. It should be def average(a, b):