J277 20: Robust Programs

OCR GCSE COMPUTER SCIENCE · J277 · PAPER 2

Producing robust programs

Defend it, keep it readable, test it. Programs that survive real users.

Paper 2 · Lesson 20

1 / 22
volgende
Slide 1: Slide
newEditorComputer ScienceUpper Secondary (Key Stage 4)GCSE

In deze les zitten 22 slides, met interactieve quizzen en tekstslides.

time-iconLesduur is: 60 min
Dit is niet oké

Introductie

OCR GCSE Computer Science (J277), Paper 2, lesson 20 of 26 (Block C). Students learn how defensive design protects a program through anticipating misuse, authentication and input validation (range, type, presence, format and length checks), how sub programs, naming conventions, indentation and comments make code easy to maintain, and how to test a program: iterative and final testing, syntax and logic errors, normal, boundary, invalid and erroneous test data and simple test plans. Activities include a do now on lesson 19, picture questions, a drag-and-drop sorting test data, a spot-the-mistake task, a sorting question on the testing process, fill in the blanks and an exam-style question with a model answer. This lesson was created with AI assistance.

Onderdelen in deze les

OCR GCSE COMPUTER SCIENCE · J277 · PAPER 2

Producing robust programs

Defend it, keep it readable, test it. Programs that survive real users.

Paper 2 · Lesson 20

Lesson 20 of the J277 series (Block C, Paper 2, section 2.3 Producing robust programs: 2.3.1 Defensive design and 2.3.2 Testing).

Cover hologram from a photo by Sasun Bughdaryan / Unsplash.

Device-free: show the slide; pupils write the title and date. Image credit: Sasun Bughdaryan / Unsplash (cover hologram)

00 · MISSION · BY THE END OF THIS LESSON

By the end of this lesson you can…

01  Explain how defensive design and validation protect a program.

02  Describe four ways to make code easy to maintain.

03  Choose test data and write a simple test plan.

Read the three outcomes. J277 section 2.3 expects pupils to know defensive design (anticipating misuse, authentication, input validation), maintainability (sub programs, naming conventions, indentation, commenting), the purpose of testing, iterative and final (terminal) testing, syntax and logic errors, the four kinds of test data, refining algorithms and simple test plans.

Device-free: pupils copy the outcomes and tick each one at the end.

02 · CHECK · DO NOW (LESSON 19)

Warm up: Boolean logic

1  What does AND output when both inputs are 1?

2  What does OR output for inputs 0 and 1?

3  What does a NOT gate do?

4  How many rows does a truth table for 3 inputs need?

▮ TERMINAL // do_now_answers

1  1 (both inputs are 1)

2  1 (at least one input is 1)

3  reverses the input:

   1 becomes 0, 0 becomes 1

4  8 rows (2 x 2 x 2)

Retrieval from lesson 19. Give 4 minutes, then click to reveal the answers line by line.

1 AND outputs 1 only when both inputs are 1. 2 OR outputs 1 when at least one input is 1, so 0 OR 1 = 1. 3 NOT reverses (inverts) its single input: 1 becomes 0 and 0 becomes 1. 4 Each input can be 0 or 1, so 3 inputs give 2 x 2 x 2 = 8 rows (2 to the power 3).

Device-free: questions on the board; mini whiteboards.

Switch A is on (1) and switch B is off (0). Which expression outputs 1?

A

A AND B

B

NOT (A OR B)

C

B OR NOT A

D

A AND NOT B

Answer: D, A AND NOT B. With A = 1 and B = 0: NOT B = 1, so A AND NOT B = 1 AND 1 = 1.

The others: A AND B = 1 AND 0 = 0. NOT (A OR B) = NOT 1 = 0. B OR NOT A = 0 OR 0 = 0. Pupils who chose C often forgot that NOT A is 0 when A is on.

Photo: Jaye Haych / Unsplash. Device-free: students hold up A, B, C or D cards. Image credit: Jaye Haych / Unsplash

01 · LEARN · MISSION BRIEFING

Built to survive users

A robust program keeps working, whatever its users type or do.

DEFEND

Stop misuse and bad input before it does harm.

MAINTAIN

Write code that other people can read and fix.

TEST

Find the errors with the right test data.

SYSTEM VOICE

System online. Today's mission: build programs that survive real users. Learn how defensive design stops misuse, how to keep code easy to maintain, and how to test it with the right test data.

Click play for the system voice briefing. The words are also on the slide.

Quick hook: ask what a user might type into an "age" box on a school sign-up form. Collect answers: a word, a negative number, nothing at all, 999. A robust program copes with every one of them without crashing or storing nonsense.

Device-free: pupils write the three mission words (defend, maintain, test) with one line on each.

01 · LEARN · DEFENSIVE DESIGN

Expect the unexpected

ANTICIPATING MISUSE

Think ahead about what users could do wrong, by accident or on purpose: wrong data, blank boxes, endless password guesses.

AUTHENTICATION

Check that users are who they say they are, for example with a username and password.

Limit the tries so nobody can guess forever.

▮ TERMINAL // login.erl

attempts = 1

pass = input("Password: ")

while pass != storedPass AND attempts < 3

    pass = input("Wrong. Try again: ")

    attempts = attempts + 1

endwhile

if pass == storedPass then

    print("Access granted")

else

    print("Locked out")

endif

Click to reveal the two ideas. Defensive design means designing a program so that it copes with misuse and keeps working. It starts with anticipating misuse: listing what users might do wrong, by accident (typos, blank inputs) or on purpose (guessing passwords, typing nonsense).

Authentication confirms the identity of a user, usually with a username and password. The terminal shows authentication in OCR Exam Reference Language: the user gets 3 tries in total. Trace it: one try before the loop, and the loop allows two more, because it stops when attempts reaches 3.

Other ways to authenticate: two-factor codes and biometrics. Device-free: pupils list three ways a user could misuse a login screen.

01 · LEARN · INPUT VALIDATION

Checked at the barrier

Validation checks that input follows the rules before the program accepts it.

RANGE  within limits: age 11 to 18

TYPE  the right data type: a whole number

PRESENCE  something was entered, not blank

FORMAT  the right pattern: DD/MM/YYYY

LENGTH  the right number of characters

Remember: valid data is sensible, not always correct.

Click to reveal the five checks. The picture is the idea to remember: like the barrier, validation stops data at the entrance, and only data that passes the checks gets in. Bad data is rejected and the user is asked again.

Examples: range check, an age from 11 to 18; type check, the age is a whole number (integer); presence check, a username was entered; format check, a date like 05/09/2026 or a postcode pattern; length check, a password of at least 8 characters.

Misconception: validation cannot prove data is correct. Someone aged 15 who types 14 passes every check. Validation only proves the data is reasonable.

Photo: dari gan / Unsplash. Device-free: pupils write each check with their own example. Image credit: dari gan / Unsplash

01 · LEARN · VALIDATION IN CODE

Ask again until it is valid

▮ TERMINAL // validate.erl

// range check: age 11 to 18

age = int(input("Enter age: "))

while age < 11 OR age > 18

    print("Age must be 11 to 18")

    age = int(input("Enter age: "))

endwhile

// presence and length check

user = input("Username: ")

while user == "" OR user.length > 12

    print("Use 1 to 12 characters")

    user = input("Username: ")

endwhile

HOW IT WORKS

The while loop keeps asking until the input passes the check.

age < 11 OR age > 18 is true when the age is out of range.

user == "" means nothing was typed.

user.length > 12 means it is too long.

Click to reveal the code line by line. Both loops follow the same pattern: input, then while the input is NOT valid, show a message and ask again. The loop condition describes bad data, so OR is used: an age is out of range if it is below 11 OR above 18.

Ask: what happens if the user types 18? (18 > 18 is false and 18 < 11 is false, so the loop is skipped and 18 is accepted.) What if they type 19? (19 > 18 is true, so they are asked again.)

Note: int() would crash if the user typed "ten". A type check (for example try/except in Python) is needed to catch that; this is exactly what erroneous test data later in the lesson will find.

Device-free: pupils write a while loop that only accepts a mark from 0 to 100.

01 · LEARN · MAINTAINABILITY

Code other people can read

SUB PROGRAMS  split code into functions and procedures you can reuse and test on their own.

NAMING CONVENTIONS  clear names like pupilAge, not x.

INDENTATION  indent the code inside loops and ifs, so the structure shows.

COMMENTING  explain what the code does, for whoever reads it next.

▮ TERMINAL // age_check.erl

// true if age is 11 to 18

function validAge(age)

    if age >= 11 AND age <= 18 then

        return true

    else

        return false

    endif

endfunction

 

// main program

pupilAge = int(input("Age: "))

print(validAge(pupilAge))

Click to reveal the four ways to make a program maintainable, meaning easy for another programmer (or you, months later) to understand, fix and improve.

Point to each one in the terminal: validAge is a sub program (a function) that can be reused and tested on its own; pupilAge and validAge follow a naming convention (camelCase, meaningful names); the code inside the function and the if is indented; the comments starting with // explain what each part does.

Misconception: comments and indentation do not change what the program does. They help humans, not the computer (except in Python, where indentation is part of the syntax).

Device-free: pupils label the four features on a copy of the code.

01 · LEARN · TESTING

Why test, and when?

ITERATIVE TESTING

During development: test each part as you write it, fix it, test again.

FINAL (TERMINAL) TESTING

At the end: test the whole program against its requirements.

Purpose of testing: find errors, and check the program works and meets its requirements.

SYSTEM VOICE · KEY DEFINITION

Iterative testing happens during development: each part is tested as it is written, fixed, and tested again. Final, or terminal, testing happens at the end: the whole program is tested to check it works and meets its requirements.

The purpose of testing is to find errors and to make sure the program works as intended and meets the requirements it was written for.

Iterative testing is done while the program is being developed: each module is tested as it is written, errors are fixed and it is tested again, in a cycle. Final (terminal) testing is done once the whole program is finished, to check all the parts work together and the requirements are met. Programmers need both: iterative testing catches errors early, when they are easy to fix; final testing catches problems that only appear when the parts are joined up.

Play the key definition. Device-free: pupils draw a cycle (code, test, fix, test again) for iterative testing and a single end point for final testing.

01 · LEARN · SYNTAX AND LOGIC ERRORS

Two kinds of error

▮ TERMINAL // syntax_error

age = 15

if age >= 11 AND age <= 18 then

    print("Welcome"

endif

> SyntaxError: missing )

▮ TERMINAL // logic_error

age = 11

if age > 11 AND age < 18 then

    print("Welcome")

else

    print("Rejected")

endif

> Rejected   (11 is valid!)

SYNTAX ERROR

Breaks the rules of the language. The program will not run.

LOGIC ERROR

The program runs, but gives the wrong result. Refine: use >= and <=.

Left: a syntax error. The closing bracket after "Welcome" is missing, so the code breaks the rules (grammar) of the language and the translator cannot run it. Syntax errors are usually reported with a line number, so they are easier to find.

Right: a logic error. The code runs without complaint, but 11 is rejected even though the valid range is 11 to 18 inclusive. The comparison should be age >= 11 AND age <= 18. Logic errors give no error message, which is why they are found by testing, especially with boundary data like 11 and 18.

Refining an algorithm means improving it after testing: here, changing > to >= and < to <=, then testing again.

Device-free: pupils write one more example of each kind of error.

01 · LEARN · TEST DATA AND TEST PLANS

A test plan for age 11 to 18

NORMAL  sensible data that should be accepted.

BOUNDARY  at the edge of the valid range, and just outside it.

INVALID  the right data type, but outside the range.

ERRONEOUS  the wrong data type.

▮ TERMINAL // test_plan_age

+---+-------+-----------+----------+

| # | Data  | Type      | Expected |

+---+-------+-----------+----------+

| 1 | 14    | normal    | accepted |

| 2 | 11    | boundary  | accepted |

| 3 | 18    | boundary  | accepted |

| 4 | 10    | boundary  | rejected |

| 5 | 19    | boundary  | rejected |

| 6 | 25    | invalid   | rejected |

| 7 | "ten" | erroneous | rejected |

+---+-------+-----------+----------+

Test fails? Refine the algorithm, then run the same test again.

Click to reveal the test plan row by row. A test plan lists each test, the test data, the type of test data and the expected result. When the program is run, an actual result column is filled in and compared with the expected result.

The four types (OCR J277): normal data is sensible and should be accepted (14). Boundary data sits at the edge of the valid range, so 11 and 18 should be accepted, and the values just outside, 10 and 19, should be rejected. Invalid data is the right type (a whole number) but outside the range, so it is rejected (25). Erroneous data is the wrong type, so it is rejected ("ten").

Misconception: invalid and erroneous are not the same. A number that is too big is invalid; a word where a number should be is erroneous.

If a test fails (actual is not the expected result), the algorithm is refined and the test is run again. Device-free: pupils copy the plan and add one more row of each type.

Age must be 11 to 18. Sort the test data.

NORMAL

BOUNDARY

INVALID

ERRONEOUS





18

"twelve"

13

25

-4

16

"abc"

11

The program accepts whole-number ages from 11 to 18. Answers. Normal: 13, 16. Boundary: 11, 18 (the edges of the valid range). Invalid: 25, -4 (whole numbers, the right type, but outside the range). Erroneous: "twelve", "abc" (the wrong data type, text instead of a number).

Ask: where would 10 and 19 go? (Boundary: just outside the range, they should be rejected. They were left out of this task so every card has one clear answer.)

Device-free: pupils draw four columns and write each value in the right one.

Ages must be 11 to 18. Which check rejects an age of 10?

A

Presence check

B

Range check

C

Format check

D

Type check

Answer: B, range check. 10 is a whole number (so it passes a type check), something was entered (presence check) and it has no pattern to break (format check). Only a range check, age >= 11 AND age <= 18, rejects it.

Link back: 10 is boundary test data, just outside the valid range, so it is a good value for testing that the range check works.

Photo: Anuja Tilj / Unsplash. Device-free: students hold up A, B, C or D cards. Image credit: Anuja Tilj / Unsplash

A program runs, but its total is always wrong. What is this?

A

A logic error

B

A syntax error

C

A type check

D

A range check

Answer: A, a logic error. The program runs, so it cannot be a syntax error (a syntax error stops the program from running). It gives the wrong result, which is the sign of a logic error, for example adding the wrong variable or using > instead of >=.

Type check and range check are validation checks, not errors.

Photo: Chris Ried / Unsplash. Device-free: students hold up A, B, C or D cards. Image credit: Chris Ried / Unsplash

03 · DEBUG · SPOT THE MISTAKE

One row in Mateusz's test plan is wrong

▮ TERMINAL // mateusz_test_plan.txt

age 11 to 18

1  15      normal     accepted

2  11      boundary   accepted

3  19      boundary   rejected

4  "ten"   invalid    rejected

5  30      invalid    rejected

Check the type given for each row. Then tap the marker.

Tip: wrong type means erroneous; right type but out of range means invalid.

Give a minute to find the mistake before tapping the marker.

Answer: row 4. "ten" is text, the wrong data type for an age, so it is erroneous test data, not invalid. Rows 1, 2, 3 and 5 are correct: 15 is normal; 11 is at the edge of the range (boundary, accepted); 19 is just outside the range (boundary, rejected); 30 is a whole number outside the range (invalid, rejected).

Device-free: copy the five rows onto the board; pupils write the wrong row number and the correction.

1

Record the actual result

2

Write a test plan with test data

3

Refine the code if a test fails

4

Compare it with the expected result

5

Run the same test again

6

Run the program with each test

Put the steps of testing a program in order.

Correct order: 1 write a test plan with test data (and expected results); 2 run the program with each test; 3 record the actual result; 4 compare it with the expected result; 5 refine the code if a test fails; 6 run the same test again.

Ask: why run the same test again after a fix? (To prove the fix worked, and that it did not break something else.)

Device-free: pupils write the six steps on sticky notes and order them on the desk.

Complete the summary.

A range check makes sure a number is between limits. A presence check stops an input being left blank. Clear names, comments and indentation make code easier to maintain. A syntax error stops a program running, but a logic error gives the wrong result. Test data of the wrong type is erroneous data.

range
presence
comments
syntax
logic
erroneous
boundary
format
normal

Answers in order: range, presence, comments, syntax, logic, erroneous. Distractors: boundary, format, normal.

Device-free: paragraph on the board with gaps and the word bank; pupils write the six missing words.

EXAM · A program accepts ages from 11 to 18. Give one example each of normal, boundary and erroneous test data, and name one validation check it should use. [4 marks]

Exam-style question, 4 marks. Give 6 minutes. The model answer is on the next slide.

Mark scheme: 1 mark normal data, any whole number from 12 to 17 (accept 11 to 18), e.g. 14. 1 mark boundary data, 11 or 18 (accept 10 or 19 as just outside the boundary). 1 mark erroneous data, any data of the wrong type, e.g. "ten", "abc" or blank text. 1 mark a suitable check: range check (age >= 11 AND age <= 18), type check (must be a whole number) or presence check.

Do not award erroneous for a number outside the range, such as 25: that is invalid data.

Device-free: pupils answer on paper, then peer-mark with the next slide.

04 · EXAM · MODEL ANSWER

One of each, plus a check

▮ TERMINAL // model_answer

normal      14     accepted

boundary    18     accepted

erroneous   "ten"  rejected

 

check       range check

            age >= 11 AND age <= 18

MARK SCHEME

1 mark: normal, e.g. 14

1 mark: boundary, 11 or 18

1 mark: erroneous, wrong type, e.g. "ten"

1 mark: range, type or presence check

25 is invalid, not erroneous.

Click to reveal the model answer line by line.

Accept for normal: any whole number inside the range (12 to 17 is safest, 11 to 18 is accepted). Boundary: 11 or 18, or the values just outside, 10 or 19. Erroneous: any data of the wrong type, such as "ten", "abc" or 14.5 if only whole numbers are allowed. Check: range check, type check or presence check, with a short reason.

Common mistakes: giving 25 as erroneous (it is invalid: right type, outside the range) and naming "verification" instead of a validation check.

Device-free: pupils check their own answer against the four marks and correct it in a different colour.

How sure are you about testing?

1

Very: I can write a test plan

2

Fairly: test data types are tricky

3

Not yet: I need another go

Anonymous exit poll. If many pupils find the test data types tricky, start lesson 21 with a quick sort: call out values for "age 11 to 18" and pupils shout normal, boundary, invalid or erroneous.

Device-free: thumbs up, sideways or down.

05 · NEXT · KEEP GOING

Keep going

Isaac Computer Science

Free online lessons and questions. Look for defensive design and testing.

BBC Bitesize

GCSE Computer Science, OCR: revise producing robust programs.

Break it on purpose

Take any program that asks for a number. Write a test plan and try all four kinds of test data.

Next lesson: programming languages and IDEs.

Point pupils to the places to practise. Isaac Computer Science and BBC Bitesize are free.

Break it on purpose: pupils take a program they wrote earlier (or the age check from this lesson), write a test plan with normal, boundary, invalid and erroneous data, run every test and record the actual results. Any failed test is a chance to refine the code.

Device-free: pupils write a test plan for "a mark from 0 to 100" with one row of each type of test data, to bring to lesson 21.