← VCE Software Development U4O1

Refactoring code to improve it

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.

Understanding Refactoring code to improve it in VCE Software Development U4O1

What it does: A test case checks the output after the change. If it still passes, the behaviour has not changed. Both lines calculate the same value. Only the variable names changed, so this is refactoring and not a new feature. Refactoring changes how the code is written, not what the program does. New features are a different job. Even small changes can introduce mistakes, so you re-test to prove the behaviour is unchanged. Renaming a variable makes the code easier to read but leaves the output the same. Calculating GST changes behaviour. A function removes repetition, so a fix or change only needs to be made in one place.

Common mistakes: 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. The variable was renamed to area, but this line still uses the old name areas, which causes a NameError. It should be print(area).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 82–114) and word search (Modules 3–114).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which best describes refactoring?
Adding new features to a program so it does more thingsRewriting working code to be clearer or simpler without changing what it does
Matches
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.
Applying
Meanings
Term
refactoring
Meaning
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.
Practice
def apply_discount(price):
Testing
Gaps
= price * qty
Check Answer
Spots
1def total_price(price, qty):
2 return price + qty

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Elephant' badge — OOP Heavyweight, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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 ✓
Refactoring changes how the code is written, not what the program does. New features are a different job.
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 ✓
Even small changes can introduce mistakes, so you re-test to prove the behaviour is unchanged.
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
Renaming a variable makes the code easier to read but leaves the output the same. Calculating GST changes behaviour.
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
A function removes repetition, so a fix or change only needs to be made in one place.
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
A variable name does not change the logic. If the output changes, something went wrong during the refactor.

Match the Pairs

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
def show_total(price): print("Total: $", price) → A repeated print line moved into one function that can be called many times
After refactoring, re-run the same test cases to check the program still gives the same results → assert calc_area(3, 4) == 12
Refactoring changes how the code is written but not what it does → x = w * h -> area = width * height
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.
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.

Key Term

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.

Fill in the Blanks

1. totalPrice = price * qty print(totalPrice)
Hint: Type the clear variable name that the print line uses to show the result
2. def showTotal(price, qty): return price * qty
Hint: Type the keyword that starts a function definition, used to move repeated lines into one place
3. def showTotal(price, qty): return price * qty print(showTotal(4, 3))
Hint: Type the function name that is called on the last line
4. def showTotal(price, qty): return price * qty assert showTotal(2, 3) == 6
Hint: Type the result this test must still give after refactoring, because behaviour must not change
5. before = 12 after = showTotal(4, 3) print(before == after)
Hint: Type the variable that holds the new result from the refactored function so it can be compared with the old result

Spot the Error

Question 1
A def total_price(price, qty):
B return price + qty
C print(total_price(4, 3))
D # expected output: 12
Line 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.
Question 2
A width = 5
B height = 3
C area = width * height
D print(areas)
Line 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).
Question 3
A def greet(name):
B msg = 'Hi ' + name
C print(mesg)
D greet('Sam')
Line C — After renaming a variable you must change every use of it. The variable is msg, not mesg. It should be print(msg).
Question 4
A def show_total(cost):
B gst = cost * 0.1
C print(cost + gst
D show_total(20)
E show_total(35)
Line C — The closing bracket is missing, which is a syntax error, so the new function will not run. It should be print(cost + gst).
Question 5
A def average(a, b)
B total = a + b
C return total / 2
D print(average(6, 10))
Line A — A function definition must end with a colon. Without it the refactored code will not run. It should be def average(a, b):

Student with a class code? Join your class →