Robust and secure programming

<< Previous: Structured programming Programming home Next: Programming test >>

Data validation

One of the first steps to ensuring a program runs smoothly is checking that data that is received is what is expected before carrying on. Below are the types of validation you need to be familiar with.

Type of validation Description Example use
Presence check Checks whether there has been some data entered For fields that are required in a user input form
Type check Checks whether the data entered has the correct data type Prevent a program from crashing by trying to convert a non inteer value to integer
Length check Checks whether the data matches any length requirements A username might have a minimum and maximum length
Range check Checks whether data entered is within a specified range To ensure a number entered is sensible
Format check Checks whether the data entered fits a certain format Making sure a postcode could be valid

Along with validation we also might see verification. This is the process of checking something is exactly right where validation just checks it could be valid. This can be seen when a form requires you to enter important information twice, such as your email. It is used because if this information was wrong, then you would have no way to recover your account.

Writing authentication routines

You need to ensure you can write a subrotuine for checking a users username and password. It is typical to allow 3 attempts before being locked out.

Testing

When it come to testing you need to know the difference between the three types of testing: normal, erroneous and boundary.

Type of testing What it's for
Normal testing To check that the program behaves as expected when it receives the sort of data we would expect it to receive e.g. if a valid numerical menu choice is received then that choice is selected.
Erroneous testing To check that the program returns an error message/crashes when it receives the wrong kind of data e.g. If the menu has choices 1 to 4 and the user enters 7 they are told they need to enter again.
Boundary testing Sometimes called extreme testing checks that the program behaves correctly either side of the boundaries of a specified range e.g. If our menu expects numbers between 1 and 4 then checking the boundaries would involve checking that 0 and 1 behave as expected and that 4 and 5 behave as expected.

You also need to be able to classify errors in code as either syntax errors or logic errors.

Type of error Description
Syntax error A syntax error is an error in following the rules of a programming language e.g. using a capital P for Print in a python program or calling a subroutine without passing the needed parameters. Syntax errors cause the program to crash.
Logic error Logic errors are harder to spot because they don't cause the program to crash. Instead they cause the program to give an unexpected result. Some common causes of logic errors include incorrect formulae or incorrect conditions in selection statements or loop conditions.
Runtime errors Any error that would stop the program from running is known as a runtime error. This would include things like trying to divide by 0 or convert a string of letters to an integer. It is good practise to trap any of these errors and prevent them from happening.

Knowledge check


Questions:
Correct:

Question text


<< Previous: Structured programming Programming home Next: Programming test >>

© All materials created by and copyright S.Goff