J277 2.3.1 · Year 11 Computer Science

Defensive Design

Section 1 · Do Now

Break my program

Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine());
Console.WriteLine("In 10 years you'll be " + (age + 10));

List inputs that would break this program, then sort them into two groups.

Section 2 · Guided notes

Defensive design notes

Make notes in your own words as we go through the slides. Use examples.

Anticipating misuse
Input validation
Dealing with invalid data
Authentication
Section 3 · Research

Validation checks and research questions

Sources: BBC Bitesize (OCR J277), Isaac Computer Science, Craig 'n' Dave. Write in your own words; don't copy and paste.

Part A: The six validation checks (everyone)

CheckWhat it checksExample of data it rejectsReal form where it's used

Part B: Research questions (on your own)

Research all four questions. For each one, write 2-3 sentences in your own words and include a real example.

Section 4 · Practical

TryParse, validation and login

TryParse check

Hinge question

while (!int.TryParse(Console.ReadLine(), out age) || age < 0 || age > 120)
{
    Console.Write("Invalid. Enter a whole number from 0 to 120: ");
}
Console.WriteLine("In 10 years you'll be " + (age + 10));

The user types twelve, then -5, then 30.

Plan before you code

Your code

Section 5 · Maintainability

Messy Code Makeover

Do the Messy Code Makeover task on CodeBash: Year 11 C# Programming → Defensive Design. Switch the editor to C#. Then make notes below.

For each technique, write one sentence: what it is → why it makes code easier to maintain.

Sub programs
Naming conventions
Indentation
Commenting
Section 6 · Exit ticket

Exit ticket

When you've finished, download your notes and upload the file to Teams.