The shape of the lesson
By the end of the lesson, learners can
- Three types of error: The type of an error depends on when and how it shows itself (Fig. 1).
- Exposing and avoiding faults: Each program in the lab has one logic error.
- Methods of testing: No single method finds every fault, so a test strategy uses several, at different points in development.
- Syntax: breaks the language rules; found by the translator.
- Logic: runs, but wrong result; found by testing or a dry run.
- Run-time: crashes or hangs while running, e.g. divide by zero.
- Methods: dry run, walkthrough, white-box, black-box, stub, integration, alpha, beta, acceptance.
One picture every idea returns to
A syntax error breaks the grammar rules of the language, so the translator cannot translate the code. A logic error is a mistake in the algorithm: the program runs but gives the wrong result. A run-time error happens while the program is running and makes it crash or hang, for example dividing by zero or using an array index outside its bounds. Faults are avoided by good design and programming practice and exposed by testing: dry runs, walkthroughs, white-box and black-box testing, stub and integration testing, then alpha, beta and acceptance testing.
Forty-five minutes, phase by phase
| Time | Phase | What happens in the room | Grouping |
|---|---|---|---|
| 0 to 5 min | Hook: predict | Open with the question in the title, Find the faults.. Every non-trivial program contains errors when it is first written. Put the figure on the board: it is the one picture every idea in the lesson returns to. | Whole class, then pairs |
| 5 to 13 min | Teach and check: Three types of error | The type of an error depends on when and how it shows itself (Fig. 1). Model the worked example Locate, identify, correct on the board, one line at a time. | Whole class, then pairs |
| 13 to 21 min | Teach and check: Exposing and avoiding faults | Each program in the lab has one logic error. Project tab 4 of the lab (Find the error) and let a student drive it while the class predicts. Your turn: A program to find the average of N numbers crashes when N is 0. Identify the type of error and suggest how to avoid it. | Pairs |
| 21 to 30 min | Teach and check: Methods of testing | No single method finds every fault, so a test strategy uses several, at different points in development. Model the worked example Choose the method on the board, one line at a time. | Cooperative Concept Mapping |
| 30 to 36 min | Check | The four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong. | Individual, then whole class |
| 36 to 43 min | Plenary | The exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording. | Individual, random call |
| 43 to 45 min | Exit and homework | Set the worksheet below (6 questions, 21 marks). Learners self-assess against the objectives before they leave. | Individual |
Protect the parts that carry the learning
The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.
Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.
Cut first if the build overruns: the last check in Methods of testing, never the check or the plenary.
In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.
Cooperative Concept Mapping
Teams receive the key terms on cards and arrange them on a large sheet, drawing and labelling the links. Compare maps between teams before showing the model map.
The full structure, with its variations and the other twelve, is on the cooperative-learning page.
What to head off, and how
| Trap learners fall into | Teaching move that pre-empts it |
|---|---|
| A program that crashes is not a logic error. | If it stops while running (divide by zero, index out of range), it is a run-time error; if it finishes but gives the wrong answer, it is a logic error. |
| Alpha and beta are not about version numbers. | Alpha testing is done in-house by the developer's staff; beta testing is done by real users outside the company. |
Types of error and testing methods: worksheet
6 questions, 21 marks. Written in the style of the papers; no past paper question is reproduced.
Describe a syntax error, a logic error and a run-time error.
The function should return the sum of the integers from 1 to N.
FUNCTION SumTo(N : INTEGER) RETURNS INTEGER
DECLARE Count, Sum : INTEGER
Sum ← 1
Count ← 1
WHILE Count < N
Sum ← Sum + Count
Count ← Count + 1
ENDWHILE
RETURN Sum
ENDFUNCTIONThe function contains two errors. Identify each error, state its type and write the correction.
Give two ways a programmer can avoid making errors when writing code.
Explain the difference between white-box testing and black-box testing.
A team is developing a program top-down. The main module is written but the modules it calls are not. Explain how stub testing lets the main module be tested now.
Describe alpha testing, beta testing and acceptance testing, saying who carries out each.
Answer key and marking notes
- Syntax error: code that breaks the grammar rules of the language, so it cannot be translated.
- Logic error: an error in the algorithm; the program runs but produces an incorrect result.
- Run-time error: an error that occurs while the program is running, causing it to crash or hang (e.g. division by zero).
- Sum ← 1 should be Sum ← 0 (logic error).
- The 1 is counted twice: with N = 3 the code returns 1 + 1 + 2 = 4.
- WHILE Count < N should be WHILE Count <= N (logic error).
- Otherwise N itself is never added.
- Corrected: Sum ← 0 and WHILE Count <= N.
- Check: N = 3 gives 0 + 1 + 2 + 3 = 6.
Any two from: design first with structure charts / pseudocode; use small modules; meaningful identifiers, comments, indentation; use tested library routines; use constants; validate input; use IDE features such as syntax highlighting and auto-complete.
- White-box: the tester can see / uses knowledge of the code.
- Test data is chosen to test every path / branch through the code.
- Black-box: the code is not looked at; testing is based on the specification.
- The actual outputs for given inputs are compared with the expected outputs.
- Each missing module is replaced by a stub: a dummy module with the same name and parameters.
- The stub outputs a message to show it was called and / or returns a fixed value.
- So the main module's logic and calls (and parameter passing) can be tested before the real modules exist.
- Alpha: testing of a nearly complete program in-house by the developer's own staff / testers.
- Beta: a nearly finished version is given to a limited number of real users outside the company, who report faults.
- Acceptance: the client / end user tests the finished program against the requirements before accepting it.
6 multiple-choice questions on this lesson
From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.
Scores is declared as ARRAY[1:50] OF INTEGER. While a program is running it tries to read Scores[51] and stops with an error message. What type of error is this?
- ARun-time error
- BLogic error
- CSyntax error
- DTranslation error
Which numbered line contains a syntax error?
01 DECLARE Total, Index : INTEGER 02 Total ← 0 03 FOR Index ← 1 TO 10 04 IF Values[Index] > 0 05 Total ← Total + Values[Index] 06 ENDIF 07 NEXT Index
- ALine 04
- BLine 03
- CLine 06
- DLine 07
A programmer talks a group of colleagues through her code line by line, so that they can spot any errors. Which testing method is this?
- AWalkthrough
- BDry run
- CWhite-box testing
- DAlpha testing
Module CalcTax has not been written yet. To test the main module, a programmer writes:
FUNCTION CalcTax(Pay : REAL) RETURNS REAL
OUTPUT "CalcTax called"
RETURN 100.0
ENDFUNCTIONWhich testing method is being used?- AStub testing
- BIntegration testing
- CBeta testing
- DBlack-box testing
CountEven should return how many even numbers there are from 1 to N.
01 FUNCTION CountEven(N : INTEGER) RETURNS INTEGER 02 DECLARE Count, Index : INTEGER 03 Count ← 0 04 FOR Index ← 1 TO N 05 IF Index MOD 2 = 1 THEN 06 Count ← Count + 1 07 ENDIF 08 NEXT Index 09 RETURN Count 10 ENDFUNCTIONCountEven(7) returns 4 instead of 3. Which is the error, and what type is it?
- ALine 05 should test
Index MOD 2 = 0: a logic error - BLine 04 should be
FOR Index ← 0 TO N: a logic error - CLine 05 should test
Index MOD 2 = 0: a run-time error - DLine 03 should be
Count ← 1: a syntax error
What happens when this code is run?
Count ← 1
WHILE Count <> 10
OUTPUT Count
Count ← Count + 2
ENDWHILE- ACount goes 1, 3, 5, 7, 9, 11 and never equals 10, so the loop never ends: a run-time error
- BWHILE must be followed by DO in 9618 pseudocode, so it is a syntax error and does not run
- CIt outputs 1, 3, 5, 7 and then stops one value too early because of the condition: a logic error
- DIt outputs the odd numbers 1, 3, 5, 7 and 9 and then stops, so there is no error in the code
Quiz key
An index outside the array bounds is found only while the program runs, and it makes the program crash: a run-time error. A logic error lets the program finish with a wrong result. A syntax error, which the translator reports, stops the code being translated at all, and Scores[51] is valid syntax.
In 9618 pseudocode an IF condition must be followed by THEN, so line 04 should read IF Values[Index] > 0 THEN. Line 03 is a correct FOR header, line 06 correctly closes the IF with ENDIF, and line 07 correctly ends the FOR loop with NEXT Index.
A walkthrough is the programmer explaining the design or code, step by step, to peers who look for errors. A dry run is working through the code by hand with a trace table. White-box testing runs the program with data chosen to cover every path. Alpha testing is in-house testing of a nearly complete program.
A stub is a simple stand-in for an unwritten module: it shows it was called and returns a fixed value, so the calling module can be tested. Integration testing combines real modules that have already been tested. Beta testing gives a nearly finished program to outside users. Black-box testing compares outputs with the specification for chosen inputs.
Index MOD 2 = 1 is TRUE for odd numbers, so it counts 1, 3, 5, 7 and returns 4. Testing MOD 2 = 0 counts 2, 4, 6, giving 3. The program runs but gives the wrong result, so it is a logic error, not a run-time error. Starting the loop at 0 changes nothing, because 0 is not odd, so the function would still return 4. Count ← 1 would make the result 5, and the original line 03 is valid syntax.
Count only takes odd values, so Count <> 10 is always TRUE and the loop runs for ever: an infinite loop makes the program hang, which is a run-time error. 9618 pseudocode writes WHILE without DO. The loop does not stop at 7, or at 9: after 9, Count becomes 11, which is still not 10.
What to project
The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.
Slides for this lesson
A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.