Robust Programming

GCSE Computer Science ยท Programming

Robust Programming

Robust programming means writing code that handles unexpected inputs and errors gracefully without crashing. This topic covers defensive design, testing, and maintainability.

---

Defensive Design

Defensive design anticipates problems before they happen. The key techniques are:

Input Validation

Validation checks that data meets specific criteria before processing it. Common validation checks include:

CheckPurposeExample
Range checkValue within acceptable rangeAge must be 0โ€“150
Type checkCorrect data typePrice must be a number
Length checkCorrect number of charactersPassword must be 8โ€“20 chars
Presence checkField is not emptyUsername cannot be blank
Format checkMatches expected patternEmail must contain @
Lookup checkValue exists in a listCountry must be in approved list

Example (Python)

while True:
    age = input("Enter your age: ")
    if age.isdigit() and 0 <= int(age) <= 150:
        age = int(age)
        break
    else:
        print("Invalid. Enter a number between 0 and 150.")

Authentication

  • Username and password โ€” most common method
  • Two-factor authentication (2FA) โ€” adds a second verification step (e.g. text code)
  • Access levels restrict what different users can do

Input Sanitisation

Sanitisation cleans input to prevent malicious data (e.g. removing <script> tags to prevent SQL injection and cross-site scripting).

---

Testing

Testing ensures the program works correctly. There are several types:

Types of Testing

TypeDescriptionWhen Used
Iterative testingTesting during development, after each moduleThroughout development
Final/terminal testingTesting the complete programEnd of development
Unit testingTesting individual components in isolationDuring development
Integration testingTesting components working togetherAfter unit testing
Alpha testingIn-house testing by developersBefore release
Beta testingTesting by a limited group of end usersBefore full release

Test Data

You must use three types of test data:

Data TypePurposeExample (for age 0-150)
NormalTypical valid input25, 67, 10
BoundaryEdge of valid range0, 1, 149, 150
ErroneousInvalid input that should be rejected-5, 200, "abc", ""

Trace Tables

A trace table tracks the value of each variable as a program executes, line by line. They help you dry run code to find logic errors.

x = 1
WHILE x <= 4
    x = x * 2
END WHILE
Iterationxx <= 4?
Start1True
12True
24True
38False โ€” exit

---

Types of Errors

ErrorDescriptionExample
Syntax errorBreaks language rules; won't runMissing colon after if
Logic errorRuns but gives wrong resultsUsing + instead of -
Runtime errorCrashes during executionDivision by zero

---

Maintainability

Writing code that others (or your future self) can understand and update:

  • Comments โ€” explain the purpose of code sections using # in Python
  • Meaningful variable names โ€” totalScore not x
  • Indentation โ€” consistent indentation shows code structure
  • Sub-programs โ€” break code into reusable functions and procedures
  • Constants โ€” use named constants like MAX_LIVES = 3 instead of magic numbers
  • Modular design โ€” separate program into logical modules

Example of Good vs Bad Practice

Bad:

x = 10
y = x * 1.2

Good:

VAT_RATE = 1.2  # Current UK VAT rate
price_before_vat = 10
price_after_vat = price_before_vat * VAT_RATE

---

Exam Tips

  • Always give specific examples of validation โ€” don't just say "check the input"
  • When writing test plans, include all three types of test data (normal, boundary, erroneous) with expected outcomes
  • Trace tables are frequently asked โ€” practise following code line by line
  • Remember the difference between validation (is it reasonable?) and verification (is it what the user intended?) โ€” verification uses methods like double entry or screen confirmation
Test yourself on Programming

5 quick GCSE Computer Science questions, marked instantly, so you know what stuck. Free account, no card needed.

Start the free quiz โ†’
Don't understand a part?

Sign in and ask our AI tutor to explain any passage in plain English.

Try AI explanations โ†’

More on Programming

Programming Fundamentals Selection and Iteration Arrays, Lists and Records Subroutines: Functions and Procedures String Manipulation String Manipulation and File Handling

โ† All GCSE Computer Science notes