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

Free lesson

OS 5.1 (2 of 2): Code reviews

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Spot the mistake

Read this line fast: "The the bus is late."

Did you see the double "the"? Many people miss it. The writer misses it most, because their eyes read what they meant to write.

Code is the same. The person who wrote it often cannot see the mistake. A second person finds it in seconds.

Key words

WordWhat it means
Code reviewAnother person reads your code to find mistakes.
Paired programmingTwo people write code together at one computer.
DriverThe person who types in paired programming.
NavigatorThe person who watches and spots mistakes in paired programming.
WalkthroughAn informal talk where the author explains the code.
InspectionA formal, planned review with set roles and a written record.
DefectA mistake in the code.

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

Three ways to review

There are three common kinds of review.

Paired programming: two people, one computer, all the time.

Informal walkthrough: the author shows the code to colleagues (people they work with) and talks through it. No papers, no rules.

Formal inspection: a planned meeting with roles, a checklist and a written list of defects.

Check: which kind of review?

Have a go

Check: which kind of review?

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Compare the three

Paired programmingInformal walkthroughFormal inspection
PeopleTwoThe author and a few colleaguesA team with set roles
WhenWhile the code is writtenAfter a part is writtenAfter a part is written, on a planned day
PaperworkNoneNone or a few notesA checklist and a written record
Cost in timeTwo people at onceLowHigh
Best forHard problems and teachingQuick feedbackCode that must be very safe, such as payments

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

Driver and navigator

In paired programming, the driver types. The navigator watches and thinks about the next step.

The navigator asks: what if the list is empty? Is the name clear?

Swap roles often, for example every 10 minutes. Both people stay busy and both learn.

Review this loop and fix it

Code: python
def total_cost(prices):
    total = 0
    for price in prices:
        total = price      # reviewer: is this right?
    return total


print(total_cost([2, 3, 5]))

# The answer should be 10. Run it. Find the mistake. Fix it.

Make a free account to save your work and track your progress.

A code review checklist

QuestionWhy it matters
Does it do what was asked?Working code that does the wrong thing is still wrong
Are names clear?Other people must read it later
What if the input is empty, zero or very large?These odd inputs are called edge cases. They cause many defects
Is any code repeated?Repeated code means repeated mistakes
Are there tests?Tests prove it keeps working
Are the comments kind?Review the code, never the person

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

Say it kindly

Tip

Write "What happens if the list is empty?" and not "You forgot the empty list." Ask questions. Say what you like too. A review is for the code, not against the person.

Fix the four defects a reviewer found

A reviewer read this code and left a comment on each function. Fix the four defects. The hidden tests check each one, including the edge cases the reviewer asked about. Press Run Tests.

Code: python
def average(marks):
    # Found in review: the average is wrong, and an empty list must give 0.
    return sum(marks) / (len(marks) + 1)


def count_passes(marks, pass_mark):
    # Found in review: a mark equal to the pass mark is a pass.
    count = 0
    for mark in marks:
        if mark > pass_mark:
            count = count + 1
    return count


def best_student(names, marks):
    # Found in review: this gives the WORST student.
    return names[marks.index(min(marks))]


def is_valid_mark(mark):
    # Found in review: 0 and 100 are both valid marks.
    return 0 < mark < 100

Make a free account to save your work, or press Start lab to work through the whole lesson.

Role play a code walkthrough

Work in threes. One person is the author, one is the reviewer and one is the note taker. The author shows the code from the "Review this loop" block and explains it in plain words. The reviewer uses the checklist. The note taker writes a list of defects and one good thing. Hand in the list. Then write three lines: which kind of review did you just do, and would a formal inspection be better for a banking program? Why?

A teacher marks this in a college. Nothing you write here is kept: it goes when you leave the page.

Make a free account to save your work, or press Start lab to work through the whole lesson.

Match the word

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.