1

mrahmedcomputing

KS3, GCSE, A-Level Computing Resources

Lesson 3. Robust and Secure Programming


Lesson Objective

  1. Understand the purpose of testing and validation.
  2. Be able to state some features of IDEs.
  3. Identify good coding practice.

Lesson Notes

Testing?

Testing aims to find and fix errors and ensure a program's quality. You should make a test plan with various tests and test data to check the program. Test data should include different inputs to show the program's strengths and weaknesses. Test data has three categories: Normal, Boundary and Erroneous.

Normal Test Data

Sensible, valid data that the program should accept and be able to process. For example, if a program asks for a number from 1 to 10, normal data could be 5.

Boundary Test Data

Valid data that falls at the boundary of any possible ranges, sometimes known as extreme data. For example, if a program asks for a number from 1 to 10, boundary data could be 1 or 10.

Erroneous Test Data

Invalid data that the program cannot process and should not accept. For example, if a program asks for a number from 1 to 10, erroneous data could be -5, 20, or "hello".


Validation

Validation is used to make sure that data is entered in a correct way. This ensures that the data is relevant and useful for its intended purpose.

Different types of validation checks can be used to validate data:

  1. Range check: The input should be within a certain range. For example, you can enter your age from 1 to 120. Or, you can choose a number of items from 0 to the available stock.
  2. Length check: The input should have a specific number of characters. For example, you should enter a valid phone number with 10 digits. Or, you should use a username with at least 6 characters.
  3. Presence check: The input should not be blank, like something has to be entered. For example, you have to type your password to log in. Or, you have to fill in your name on a survey.
  4. Format check: The input should follow a certain format. For example, you might want the email address in this format: name@domain.com. Or, you might want the time in this format: hh:mm:ss.
  5. Type check: The input should be a specific data type, such as a Boolean.

Validation does not guarantee the accuracy of the data. It might be in the right format, but still incorrect. For instance, typing an incorrect birth date.

Below is an example of an algorithm that validates an input.

  1. Password = input
  2. WHILE LENGTH(Password) < 8 OR LENGTH (Password) > 15
  3. OUTPUT "Sorry this is a weak password, try again...
  4. Password = input
  5. ENDWHILE
  6. OUTPUT "Your password is strong"

Authentication

Authentication is the process of confirming your identity. Below are some examples of authentication methods:

  1. Username and Password
  2. Pin
  3. Pattern Lock
  4. Secret answer to a question
  5. Swipe card
  6. Biometrics

IDE (Integrated Development Environment)

An IDE is a software tool that helps you make other software. It gives you a space to type, change, and store code in a convenient way. IDEs have various features that assist you in making and modifying programs. Here are some examples of features:

  1. Auto-Completion - This saves you time by suggesting or completing the function name and its parameters as you type.
  2. Bracket Pairing - This helps you avoid missing brackets by showing coloured sections where you need to close them.
  3. Syntax Highlighting - This shows you where you have made syntax errors by highlighting the code differently.
  4. Breakpoints - These let you pause the program at certain points to check what is going on and find bugs.
  5. Libraries - These give you access to extra functions that are not built into the programming language. You can use these functions by importing them at the beginning of your code. For example, in Python you can use the Turtle Graphics library to draw simple shapes and graphics.
  6. Debugger - This is a feature that helps you find and fix errors in your code. It tells you what kind of error it is and where it is located.

Debugging

Debugging is the process of finding and fixing any problems or bugs in the program, usually by testing it in a methodical way. A debugger program is a common feature in IDEs that helps with debugging.


Good Coding Practice

Meaningful Names

You should use meaningful variable and subroutine names in your program. This helps other programmers to understand what is going on. It can also help you remember how your code is working.

Example: No meaningful names

  1. x = 60
  2. y = input
  3. z = (y/x) * 100
  4. OUTPUT z + "%"

Example: With meaningful names

  1. total = 60
  2. score = input
  3. percentage = (score/total) * 100
  4. OUTPUT percentage + "%"

Comments

Comments are also good practice as they can again explain sections of code to other programmers and also help you remember things.

Example: No comments

  1. x = 60
  2. y = input
  3. z = (y/x) * 100
  4. OUTPUT z + "%"

Example: With comments

  1. x = 60 #x is the total
  2. y = input #y is the score
  3. z = (y/x) * 100 #z is the percentage
  4. OUTPUT z + "%"
3