Welcome to Robust and Secure Programming!

Ever used an app that crashed the moment you typed something wrong? Or a website that let you log in without a password? Those are examples of fragile programming. In this chapter, we are going to learn how to do the opposite: robust and secure programming.

Robust means your program is "tough" and won't crash if it gets unexpected input. Secure means your program protects its data from people who shouldn't see it. Don't worry if some of these terms sound technical; we'll break them down step-by-step!

1. Data Validation: The Digital Bouncer

Data validation is a check carried out by a computer to make sure that the data being entered is sensible or reasonable. Think of it like a bouncer at a club: they check if you're on the list and if you're the right age before letting you in.

It doesn't prove the data is correct (you could lie about your age!), but it checks if the data follows the rules. Here are the three simple checks you need to know for your AQA exam:

A. Length Check

This checks if the data has a minimum number of characters.
Example: A password must be at least 8 characters long. If you type "123", the program rejects it.

B. Presence Check

This simply checks if a string is empty. It ensures the user hasn't just hit "Enter" without typing anything.
Example: You can't leave the "Surname" box empty when signing up for a new account.

C. Range Check

This checks if a number or value lies within a given range.
Example: If a program asks for a rating between 1 and 10, a range check will stop you from entering 11 or -1.

Quick Review: Validation makes sure data is sensible before the program tries to process it. This prevents the program from crashing later on!

Key Takeaway: Validation = Sensible data. (Length, Presence, Range).

2. Authentication: Who are you?

Authentication is the process of confirming the identity of a user. In simple terms, it's making sure the person logging in is actually who they say they are.

For your exam, you just need to know how to write a simple routine that uses a username and a password.

Did you know? At this level, you only need to think about plain text usernames and passwords. This means we don't need to worry about fancy encryption (scrambling the password) yet!

Simple Authentication Steps: 1. Ask the user for their username.
2. Ask the user for their password.
3. Check if the username and password match the ones stored in the system.
4. If they match, let them in! If not, show an error message.

Key Takeaway: Authentication proves identity using a username and password.

3. Testing: Finding the "Bugs"

No programmer writes perfect code the first time. Testing is the process of running a program to find errors (bugs) and make sure it meets the requirements.

When we test, we use different types of test data to see how the program reacts. Imagine we are testing a program that only accepts numbers between 1 and 10. Here is the data we would use:

Normal (Typical) Data

This is data that is expected and should be accepted.
Example: 5 or 7.

Boundary (Extreme) Data

This is data at the limit of what is allowed. It includes the very edge of the valid range and the first invalid number outside it.
Example: If the range is 1 to 10, the boundary data would be 0, 1, 10, and 11.

Erroneous Data

This is data that is the wrong type or shouldn't be accepted at all.
Example: Typing "banana" instead of a number, or entering a symbol like @.

Mnemonic Aid: Just remember N.B.E.
Normal - Works fine.
Boundary - On the edge.
Erroneous - Errors expected!

Key Takeaway: Testing uses Normal, Boundary, and Erroneous data to find and correct errors.

4. Types of Errors: Why isn't it working?

When a program doesn't work, it's usually down to one of two types of errors. Understanding the difference is a huge help in your exams!

Syntax Errors

A syntax error is a "grammar" mistake in the code. Programming languages have very strict rules. If you forget a bracket, spell a command wrong, or miss a colon, the computer won't understand what to do.

Important: If there is a syntax error, the program will not run at all.
Example: Writing prnt("Hello") instead of print("Hello").

Logic Errors

A logic error is a mistake in the way the program was designed. The program will run without crashing, but it will give the wrong result.

Example: You want to calculate 10 + 10, but you accidentally type 10 * 10. The computer gives you 100 instead of 20. It did exactly what you told it to do, but your "logic" was wrong!

Analogy Time!
A Syntax Error is like writing a sentence in a language that doesn't exist. No one can understand you.
A Logic Error is like giving someone the wrong directions to the cinema. They can follow your instructions perfectly, but they’ll end up at the park instead!

Key Takeaway: Syntax errors stop the program from running. Logic errors let it run but produce the wrong output.

Quick Review Box

Validation: Checks if data is sensible (Length, Presence, Range).
Authentication: Checks who the user is (Username/Password).
Test Data: Normal (typical), Boundary (edges), Erroneous (wrong).
Syntax Error: "Grammar" mistake; program won't start.
Logic Error: Mistake in calculation/steps; program gives wrong answer.

Don't worry if this seems tricky at first! The more you practice writing small programs and "breaking" them with weird data, the easier it will be to spot these errors!