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
| Word | What it means |
|---|---|
| Code review | Another person reads your code to find mistakes. |
| Paired programming | Two people write code together at one computer. |
| Driver | The person who types in paired programming. |
| Navigator | The person who watches and spots mistakes in paired programming. |
| Walkthrough | An informal talk where the author explains the code. |
| Inspection | A formal, planned review with set roles and a written record. |
| Defect | A 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 questionsCompare the three
| Paired programming | Informal walkthrough | Formal inspection | |
|---|---|---|---|
| People | Two | The author and a few colleagues | A team with set roles |
| When | While the code is written | After a part is written | After a part is written, on a planned day |
| Paperwork | None | None or a few notes | A checklist and a written record |
| Cost in time | Two people at once | Low | High |
| Best for | Hard problems and teaching | Quick feedback | Code 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
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
| Question | Why 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.
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