Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Python from Zero

Module 7 · Lesson 4 · Core

Debugging as a method

Reproduce a fault, narrow it down, change one thing, retest and keep a test.

  • 50 minutes
  • Serves O5
  • Assumes M7.L3

Phase 1: Prepare

Step 1 Warm-up recall

From M7.L3: what is a boundary test?

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • reproduce a fault (Foundation);
  • narrow it down with print and tracing (Core);
  • fix, retest and keep a test (Core).

Phase 2: Teach

Step 3 A familiar example

A mechanic does not replace parts at random. She hears the fault, narrows it to one system, changes one part and tests the car. Debugging follows the same discipline.

Step 4 Clear explanation

Debugging is finding and fixing a cause. A method beats guessing.

Reproducesame inputNarrowfind the lineFixone changeRetestall testsKeepa test
Debugging is a loop: reproduce, narrow down, change one thing, retest, then keep a test so it cannot return.
  1. Reproduce the fault with the same input every time.
  2. Narrow it down: print values at key points, or trace the code on paper, until you find the first line where the value is wrong.
  3. Change one thing at a time.
  4. Retest with every earlier test, not only the one that failed.
  5. Keep the failing case as a test so the fault cannot quietly return.
def average(values):
    total = 0
    for v in values:
        total = total + v
        return total / len(values)

print(average([2, 4, 6]))
0.6666666666666666

The right answer is 4.0, so the output is wrong, with no error message. Printing total inside the loop shows 2 after the first item only. The return sits inside the loop, so the function ends after one item. Moving it out by one indentation level gives 4.0. Then add assert average([2, 4, 6]) == 4 as a kept test.

If you cannot reproduce a fault, say so and record what you know. Dismissing an odd result as a glitch is how faults ship.

Step 5 Worked example (4 marks)

A loop adds the items of [1, 2, 3] but prints 3 instead of 6. Using the method, what is a sensible first move?

  1. The answer is wrong with no error, so reproduce with the same list. (1 mark)
  2. Print the total after each step to see where it goes wrong. (1 mark)
  3. The total should be 1, 3, 6; if it shows 1, 2, 3 the line uses = instead of adding to total. (1 mark)
  4. Fix that line only, retest, and keep the case as a test. (1 mark)

Go deeper: the professor's notes

Debug by method: reproduce, narrow, change one thing, retest, keep a test.
Reproduce

Same input.

Narrow

Print or trace.

Keep

A test.

Reproducesame inputNarrowfirst wrong valueFixone changeRetestand keep
Method

Order

StepWhy
one change at a timeknow what worked
keep a testfault cannot return

At work: A wrong total

The total is wrong and no error appears.

  1. Print the total each step.
  2. Find the first wrong value.

What a careful practitioner decides. A method beats guessing.

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. The first step in debugging is to:

2. You should change:

3. After a fix, you should rerun:

4. A fault that cannot be reproduced should be:

Phase 4: Close

Step 7 Recap

  • Reproduce, narrow, change one thing, retest, keep a test.
  • Print or trace to find the first wrong value.
  • Never dismiss an odd result as a glitch.

Step 8 Exit ticket (ungraded)

List the five steps of the debugging method in order.

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Take a program that gives a wrong total and trace it on paper to find the first wrong value.

In industry: Mars Pathfinder: debugged from Earth

A few days after landing in July 1997, Pathfinder began total system resets. Engineers recreated the fault on a replica in their laboratory with tracing turned on, found priority inversion, and uploaded a short change that stopped the resets. Resets seen in pre-flight tests had been put down to a hardware glitch.

Link to this lesson. Reproduce the fault with the same conditions, trace to the first wrong step, and never write off an odd result as a glitch.

Source: Mike Jones, What really happened on Mars Rover Pathfinder, RISKS Digest 19.49 (1997); Rapita Systems

Common mistakes

MistakeWhy it fails
Changing several things at onceYou will not know which change helped.
Not keeping a testThe fault may return.
Blaming a glitchKeep notes and try to reproduce.

Limits. Larger programs use a debugger that steps through the code; the thinking is the same as tracing on paper.

Flashcards for this lesson (3)

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 3 · What is an edge case?
Boundary49 and 50Odd0 or emptyWhyfaults hide here
Edge case

A boundary or odd input: 49 and 50 for a pass mark of 50, zero, or an empty list. Faults hide at edges.

Card 2 of 3 · What are the steps of debugging?
Reproducesame inputNarrowprint or traceKeepa test
Debugging

Reproduce the fault, narrow it down, change one thing, retest everything, and keep a test. A method beats guessing.

Card 3 of 3 · Why change only one thing at a time?
Changeone thingRetestall testsKnowwhat worked
One change

If you change several things and the fault goes, you do not know which change worked, and you may have added a new fault. One change, then retest.

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards