Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Reviewing code an AI wrote

After this lesson: I can check AI code before I trust it.

45 min lessonNo commands

It ran, so it shipped

An online shop asked an assistant for a discount rule. The code ran with no errors, so it went live.

A week later the finance team noticed something. Customers who spent exactly £100 got no discount. Nobody had read the one line that decided it.

By the end of this lesson

Note

I can check AI code before I trust it.

Key words

WordWhat it meansExample
Code reviewreading code to find faults before it is used
Requirementwhat the code must do, in words
Edge casean unusual input, such as empty or zero
Dependencyoutside code that your code needsimport requests
Packagea library you install by namepip install requests

The cells students fill in are left blank in this preview.

Read the code against the requirement

Before you run anything, read the code beside the request. Go line by line. For each line, ask what it does and why it is there.

Look hard at comparisons such as > and >=. Look at every number. A single symbol can change who gets a discount or who gets charged.

Try it: find the wrong line

Code: python
# Request: "Take 10 per cent off orders of 100 pounds or more."
# The assistant wrote this function.
def discounted(total):
    if total > 100:
        return total * 0.9
    return total

print(discounted(150))   # expect 135.0
print(discounted(100))   # expect 90.0
print(discounted(40))    # expect 40

# One line does not match the request. Find it, fix it,
# and press Run until all three match.

Check every library is real

An assistant may import a package that does not exist, or one with a name close to a real one. Attackers publish fake packages with such names. Installing one can run their code on your laptop.

Before you install, find the package's own page and documentation. Check the exact spelling. Ask your team if it is already approved.

A review checklist

QuestionWhere to look
Does it do what the request said?Each line, beside the requirement
Are the functions and packages real?The official documentation
What happens with empty or zero input?Loops, divisions and indexes
Does it add a dependency?The import lines
Does it delete, send or change data?File, network and database calls

The cells students fill in are left blank in this preview.

Try the edge cases in your head

Most assistant code works for the normal case it was shown. Faults hide in the edge cases.

Ask what happens with an empty list, a zero, a negative number or a very long text. Dividing by the length of an empty list crashes. Your next task has exactly that fault.

A common mistake

Warning

Many people think that if code runs without an error, it is correct. Running only shows that Python understood it. It is correct when it does what the requirement says, for every case you can think of.

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.