Computer Science Essentials, Unit 2, Lesson 5 of 6

Robust programs and testing

GCSE Computer Science, Programming, 20 minutes

Sign in

Validation and authentication, syntax and logic errors, test data and test plans, and code that is easy to maintain.

What you will learn

  • Name and choose validation checks
  • Write a validation loop
  • Choose normal, boundary and erroneous test data
  • Tell a syntax error from a logic error

Defensive design

A robust program copes with whatever users type.

  • Validation checks that input is sensible before it is used: range (from 1 to 12), length (at least 8 characters), presence (not left empty), format (looks like a postcode), type (a whole number).
  • Authentication checks that a user is who they claim to be, for example a username and password.
  • Maintainability: comments, meaningful names, indentation and subroutines make code easier to fix later.

A validation loop: it repeats until the value is in range

n = int(input("Enter a month number"))
while n < 1 OR n > 12
    print("Invalid")
    n = int(input("Enter a month number"))
endwhile
print("Accepted " + str(n))
n ← STRING_TO_INT(USERINPUT)
WHILE n < 1 OR n > 12
    OUTPUT "Invalid"
    n ← STRING_TO_INT(USERINPUT)
ENDWHILE
OUTPUT "Accepted " + INT_TO_STRING(n)
n = int(input("Enter a month number"))
while n < 1 or n > 12:
    print("Invalid")
    n = int(input("Enter a month number"))
print("Accepted " + str(n))

Suppose the user types 15, then 0, then 7. 15 is more than 12, so the loop prints Invalid and asks again. 0 is less than 1, so it prints Invalid again.

7 is in range, so the condition is false, the loop ends, and the program prints Accepted 7. Invalid was output 2 times: once for each value that was out of range.

The rest of this lesson and its 7 practice questions are locked.

In this lesson

  1. Defensive design
  2. Errors and test data
  3. Try it: Test data

7 practice questions, marked as you go; about 20 minutes in all.

Key terms

Validation, Range, Length, Presence, Format, Type, Authentication, Syntax error, Logic error, Invalid

In this unit: Programming

Data types, operators, selection, loops, arrays, strings, subroutines, robust programs, SQL and files, read and traced step by step.

All of Programming