← GCSE Computer Science · OCR · GCSE Computer Science revision
⭐ 0
OCR GCSE Computer Science (J277) · 2.3 Producing robust programs
Mini-Lesson

Producing robust programs

2 missions. Guess, learn, then check. Your place is saved.

  1. Mission 1You typed 41, not 14, for your age. Why did the form accept it?
  2. Mission 2It passed every test. So why did it fail for an 18-year-old?
Mission 1

You typed 41, not 14, for your age. Why did the form accept it?

Make a guess: You typed 41, not 14, for your age. Why did the form accept it?

Programming · validation and authentication

What validation does

ValidationAgeValidation: range 0 to 120,presence41accepted150out of rangeask againnothing enteredask again
  • Validation is an automatic check that input is sensible and follows rules before the program uses it.
  • Invalid data is rejected, and the user is told why and asked again.
  • It stops bad data causing crashes or wrong results later.
  • Validation cannot tell whether data is correct: a mistyped value that still follows the rules gets through.
Programming · validation and authentication

Types of validation check

CheckWhat it testsExample
Range checka number is between limitsage 0 to 120
Length checknumber of characterspassword at least 8
Presence checksomething was entered (not empty)surname not blank
Quick check
?A field for a 4-digit PIN accepted 12. Which check would have rejected it?
Programming · validation and authentication

Writing a validation routine

  • Ask for input, test it, and keep asking until it passes.
  • A loop does this: the user cannot move on with invalid data.
  • This WHILE loop is a range check for 1 to 10.
OCR Exam Reference Language
score = int(input("Score 1 to 10: "))
while score < 1 OR score > 10
  score = int(input("Must be 1 to 10: "))
endwhile
Calculate
?The user types three names in this order: an empty string, Al, then Amir. How many times is "Too short" output?
OCR Exam Reference Language
name = input("Name: ")
while name.length < 3
  print("Too short")
  name = input("Name: ")
endwhile
print("Hello " + name)
Programming · validation and authentication

Authentication

AuthenticationYesNoStartInput usernameInput passwordmatches storeddetails?Output "Accessgranted"Output "Accessdenied"Stop
  • Authentication confirms the identity of a user before they get access, e.g. with a username and password.
  • The program compares what is typed with the stored details.
  • Anticipating misuse: plan for every input a user might give, by mistake or on purpose, such as blank entries or repeated wrong passwords.
OCR Exam Reference Language
username = input("Username: ")
password = input("Password: ")
if username == "sam01" AND password == "Tr33house" then
  print("Access granted")
else
  print("Access denied")
endif
Quick check
?A programmer types OR instead of AND in the authentication condition. Which login is now wrongly granted access?
Mission check

?You typed 41, not 14, for your age. Why did the form accept it?
Mission 2

It passed every test. So why did it fail for an 18-year-old?

Make a guess: It passed every test. So why did it fail for an 18-year-old?

Programming · testing

Why test?

  • Testing checks that a program works as intended and meets its requirements.
  • It finds errors so they can be fixed before users meet them.
  • A program can run without crashing and still give wrong results.
  • Passing a few easy tests proves little: choose test data that could expose errors.
  • Iterative testing tests each module while it is being written; final (terminal) testing tests the whole program at the end.
Programming · testing

Normal, edge and wrong data

Test datavalid: 1 to 10−1234678912015101115normalerroneousboundaryboundarytenerroneous

For a program that accepts whole numbers from 1 to 10:

  • Normal data: sensible values that should be accepted, e.g. 5.
  • Boundary data: the correct type, on the very edge of being valid: 1 and 10.
  • Invalid data: the correct type but should be rejected, e.g. 15. Erroneous data: the wrong data type, e.g. ten.
Calculate
?The rule is "adult ticket for 18 and over". Tests with ages 10, 30 and 50 all gave the right ticket. Which single age would show the error?
OCR Exam Reference Language
age = int(input("Age: "))
if age > 18 then
  print("Adult ticket")
else
  print("Child ticket")
endif
Quick check
?A program accepts whole numbers from 1 to 10. A tester types the word ten. What type of test data is this?
Programming · testing

Types of error

  • A syntax error breaks the rules of the language, e.g. a missing bracket. The program cannot be translated or run.
  • A logic error lets the program run, but it gives the wrong or unexpected result, e.g. > where ≥ was needed.
  • Testing with good test data is how you find logic errors.
Quick check
?A program should output the average of three marks, 60, 70 and 80. It runs, but outputs 156.67 because the code says a + b + c / 3. What kind of error is this?
Programming · testing

Test plans

Test planTest planTestDataTypeExpectedActual15NormalAcceptedAccepted210BoundaryAcceptedRejected311InvalidRejectedRejected4tenErroneousRejectedRejectedTest 2: expected ≠ actual
  • A test plan lists each test before it is run: what it tests, the test data, its type and the expected result.
  • After running, record the actual result. If they differ, fix the code and test again.
TestDataTypeExpectedActual
15NormalAcceptedAccepted
210BoundaryAcceptedRejected
311InvalidRejectedRejected
4tenErroneousRejectedRejected
  • Test 2 failed, so the range check needs fixing.
Programming · testing

Code that is easy to maintain

  • Other people (and you, later) must be able to read and change code. This is maintainability.
  • Naming conventions: totalScore, not t.
  • Comments explain what each part of the code does.
  • Indentation shows which lines belong inside loops, selection and subprograms.
  • Sub programs break code into named, reusable parts.
Mission check

?It passed every test. So why did it fail for an 18-year-old?
Recap

Big ideas

  • Validation automatically checks input is sensible, e.g. range, length and presence checks, and rejects data that breaks the rules; it cannot check data is correct.
  • Authentication confirms who a user is, e.g. by matching a username and password.
  • Choose test data of every kind: normal, at the edges of the range, and data that should be rejected (normal, boundary, invalid, erroneous).
  • Syntax errors stop a program running; logic errors give wrong results.
  • Meaningful names, comments, indentation and subprograms make code easy to maintain.
🏆

Mini-lesson complete!

⭐⭐⭐

You've worked through Producing robust programs for OCR GCSE Computer Science (J277). 🎉

  1. Mission 1You typed 41, not 14, for your age. Why did the form accept it?
  2. Mission 2It passed every test. So why did it fail for an 18-year-old?

Your stars: 0 / 0

Next: test yourself in the Verify stage.

📣 Smashed it? Share your score

Challenge a mate to beat your stars, or show a parent how you got on.

→ Back to GCSE Computer Science · OCR

Specification points and sources for this lesson are listed at the bottom of the page.

Sources (2)

The specification points and sources behind this lesson.

  1. 1 OCR J277 spec v3.1, 2.3.1; 3c Exam Reference Language
  2. 2 OCR J277 spec v3.1, 2.3.1, 2.3.2; 3c Exam Reference Language