Welcome to Robust and Secure Programming!

In this chapter, we are going to learn how to write "tough" code. Have you ever used an app that crashed the moment you typed something wrong? Or a website that let you log in without a password? Those programs weren't robust or secure. By the end of these notes, you’ll know how to make sure your programs handle mistakes gracefully and keep intruders out. Don't worry if this seems like a lot to take in at first—we'll break it down step-by-step!

1. Data Validation: The "Gatekeeper"

Data validation is a check carried out by a computer to make sure that the data entered is sensible or reasonable. It doesn’t check if the data is 100% correct, but it does check if it’s "allowed" based on certain rules.

Think of it like a bouncer at a club: they check if you have an ID and are the right age, but they don't necessarily know your whole life story!

Common Validation Checks

According to the AQA syllabus, you need to know these three simple routines:

1. Length Check: This checks if the data has a minimum or maximum number of characters.
Example: Ensuring a password is at least 8 characters long.

2. Presence Check: This checks if the data has actually been entered (it ensures the input isn't empty).
Example: Making sure you don't leave the "Email Address" box blank on a sign-up form.

3. Range Check: This checks if a number or date falls within a specific set of values.
Example: If a program asks for a month, the number must be between 1 and 12.

Key Takeaway:

Validation stops the program from trying to process "garbage" data, which prevents the program from crashing later on.

2. Authentication: Who Are You?

Authentication is the process of confirming the identity of a user. In your GCSE, you focus on simple routines using a username and a password.

A simple authentication routine usually involves:
1. Asking the user for their username.
2. Asking for their password.
3. Checking if the password matches the one stored for that specific username.
4. If they match, let them in. If not, tell them "Access Denied."

Did you know? Even though we see fancy things like FaceID or Fingerprint scanners, they are all just different ways of doing authentication!

3. Testing Your Code

Testing is the process of running a program to find errors (bugs). It is a vital part of development because even the best programmers make mistakes. Once you find an error, you must refine (correct) it.

The Three Types of Test Data

To test a program properly, you can't just type in the same numbers every time. You need to use different types of test data. Imagine a program that accepts numbers from \( 1 \) to \( 10 \):

1. Normal Data: This is "typical" data that the program should be able to handle.
Example: Entering the number 5.

2. Boundary (Extreme) Data: These are values at the very limit of what is allowed.
Example: Entering 1 or 10. (Boundary data also includes values just outside the limit, like 0 or 11, to see if the "fence" works).

3. Erroneous Data: This is data that is clearly the wrong type and should be rejected.
Example: Entering the word "banana" or a symbol like "#" when a number is expected.

Quick Review: Testing Table

If the allowed range is \( 1 \) to \( 10 \):
- 7 is Normal.
- 1 is Boundary.
- 10 is Boundary.
- 15 is Erroneous.
- "Hello" is Erroneous.

4. Identifying and Categorising Errors

When something goes wrong, it usually falls into one of two categories. Understanding which one it is will help you fix it much faster!

Syntax Errors

A syntax error is a mistake in the "grammar" or "spelling" of the programming language. If you have a syntax error, the program will not run at all.

Analogy: It's like trying to read a sentence that is complete gibberish; you can't even start to understand it.

Common causes: Missing a bracket, misspelling a keyword like print, or forgetting a colon.

Logic Errors

A logic error is a mistake in the way the program was designed. The program runs perfectly fine, but it gives the wrong result.

Analogy: It’s like following a recipe for a cake but accidentally using salt instead of sugar. You still get a cake, but it tastes terrible!

Common causes: Using a \( + \) instead of a \( - \), or using "greater than" when you meant "greater than or equal to."

Memory Aid:

Syntax = Spelling (The code won't start).
Logic = Lost (The code runs but gets the wrong answer).

Summary: How to make code Robust and Secure

1. Use Validation to ensure only sensible data gets in.
2. Use Authentication to make sure the right people are using the program.
3. Test your program using Normal, Boundary, and Erroneous data.
4. Find and fix Syntax and Logic errors to refine your code.

You're doing great! Keep practicing writing these routines in your chosen programming language, and soon "robust programming" will feel like second nature!