← VCE Software Development U3O1

Optimising code with AI tools

Re-running the same test cases on AI-optimised code to prove it still gives identical results as before. If any result changes, reject the change.

Understanding Optimising code with AI tools in VCE Software Development U3O1

What it does: Comparing the output of the original and optimised functions on the same data shows whether behaviour was preserved. True means it matches. Looping over the existing test cases and asserting the new function gives each expected result is a regression test of the optimised code. A good prompt names the goal (efficiency), sets the rule (same output) and asks for an explanation so you can review the changes. To compare fairly, both versions must use the same test cases. Otherwise you cannot show the behaviour is unchanged. Breaking behaviour is the key reason to reject a change. Different names alone do not change what the code does.

Common mistakes: A single = assigns a value and is invalid in an assert. The regression test must compare the old and new results with ==: assert old == new. A for loop header must end with a colon. It should be: for c in cases:. This loop re-runs every test case on both versions.

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 178–193) and word search (Modules 168–193).

Take this worksheet further — free with Cloodit

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

Learning
Duo
You ask an AI tool to make your function faster. What should you do before accepting its new version?
Re-run the same test cases and compare the results with the originalReplace your code straight away because the AI says it is faster
Matches
The AI version looks shorter, so I will keep it without testing.
↓
Shorter or cleaner code is not proof it works. The optimised code must be re-tested with the same test cases before it is accepted.
Applying
Meanings
Term
regression test
Meaning
Re-running the same test cases on AI-optimised code to prove it still gives identical results as before. If any result changes, reject the change.
Practice
def total_slow(nums):
Testing
Gaps
assert newAvg(data) oldAvg(data)
Check Answer
Spots
3print(old, new)
4assert old = new

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

The 'Firefly' badge — Night Coder, 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. You ask an AI tool to make your function faster. What should you do before accepting its new version?
A. Replace your code straight away because the AI says it is faster
B. Re-run the same test cases and compare the results with the original ✓
Optimised code must be proven to behave the same. Re-running the original test cases is how you check this.
2. Which prompt is better for asking an AI tool to optimise a module?
A. Make this function more efficient without changing its output. Explain each change. ✓
B. Fix my code.
A good prompt names the goal (efficiency), sets the rule (same output) and asks for an explanation so you can review the changes.
3. After optimisation, test case 4 gives a different result from before. What should you do?
A. Accept it because the new code looks shorter and cleaner
B. Reject the change, or ask the AI to fix it, then test again ✓
If any result changes, the optimised code no longer behaves the same. Reject it or refine your prompt and re-test.
4. Which statement about comparing before and after is correct?
A. Run the same test data on both versions and check that the outputs are identical ✓
B. Run the old version on old test data and the new version on brand new test data
To compare fairly, both versions must use the same test cases. Otherwise you cannot show the behaviour is unchanged.
5. What does a regression test show after AI optimisation?
A. That the optimised code still gives the same results as before ✓
B. That the AI tool is always reliable and does not need checking
A regression test checks that existing behaviour has not broken. It does not prove the AI is reliable, so you must still check its output.

Match the Pairs

Make this function faster and keep the same output → A prompt asking an AI tool for a more efficient version while requiring that behaviour stays unchanged
assert total_cost(3, 2.5) == 7.5 → A test case run on both the original and optimised code to check the result is identical
Compare before and after: both versions must give the same result for the same input → print(old_total(data) == new_total(data))
Re-run the same test cases on the optimised code to prove it still works → for t in tests: assert new_total(t[0]) == t[1]
The AI version looks shorter, so I will keep it without testing. → Shorter or cleaner code is not proof it works. The optimised code must be re-tested with the same test cases before it is accepted.
Test 4 now fails on the AI version, but it runs faster, so I will keep it. → If any result changes, the optimised code has broken behaviour and the change must be rejected or fixed, even if it is faster.

Key Term

regression test
Re-running the same test cases on AI-optimised code to prove it still gives identical results as before. If any result changes, reject the change.

Fill in the Blanks

1. assert newAvg(data) == oldAvg(data)
Hint: Type the operator that checks the optimised result is equal to the original result.
2. assert sumNew([1, 2, 3]) == 6
Hint: Type the keyword that stops the program if a test condition is false.
3. if newResult != oldResult: print("Reject the change")
Hint: Type the comparison operator that detects the results are different.
4. for x in tests: assert fast(x) == slow(x)
Hint: Type the keyword that loops through every test case in the list.
5. def totalFast(numbers): return sum(numbers)
Hint: Type the built-in function that adds up all the items in a list.

Spot the Error

Question 1
A old = total_old([1, 2, 3])
B new = total_new([1, 2, 3])
C print(old, new)
D assert old = new
Line D — A single = assigns a value and is invalid in an assert. The regression test must compare the old and new results with ==: assert old == new.
Question 2
A cases = [[1, 2], [5], []]
B for c in cases
C old = slow_sum(c)
D new = fast_sum(c)
E assert old == new
Line B — A for loop header must end with a colon. It should be: for c in cases:. This loop re-runs every test case on both versions.
Question 3
A old = slow_max([4, 9, 2])
B new = fast_max([4, 9, 2])
C if old == new:
D print('Reject change')
E else:
F print('Reject change')
Line D — The logic is backwards. When old and new results match, the AI change passes the regression test and should be kept: print('Keep change'). Rejecting belongs in the else branch.
Question 4
A tests = [(2, 4), (3, 9)]
B for x, want of tests:
C got = square_new(x)
D print(got == want)
Line B — Python loops use the keyword in, not of. It should be: for x, want in tests:. The loop runs the same test cases on the optimised function.
Question 5
A data = [3, 1, 2]
B old = sort_slow(data
C new = sorted(data)
D print(old == new)
Line B — The closing bracket is missing from the function call. It should be: old = sort_slow(data). This stores the original version's result for comparing with the AI's version.

Student with a class code? Join your class →