You are on page 1of 17

GCSE J277 Unit 2.

3 | Producing robust programs Craig’n’Dave

Name:

Specification & learning objectives


By the end of this topic you will have studied:
• Defensive design considerations: Anticipating misuse, Authentication
• Input validation
• Maintainability: Use of sub programs, Naming conventions, Indentation, Commenting
• The purpose of testing
• Types of testing: Iterative, Final/terminal
• Identify syntax and logic errors
• Selecting and using suitable test data: Normal, Boundary, Invalid, Erroneous
• Refining algorithms

Resources
We recommend the OCR endorsed text book from PG Online for use during your GCSE studies.
Craig'n'Dave videos for SLR 2.3
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

Defensive design: Input validation

An algorithm is checking data inputted by a user to ensure that the data entered meet specific requirements or rules before
Input validation means:
processing

Type of validation: Type check Range check Presence check Format check Length check

The data has the


Explanation: The data is the correct The data is within the Some data has been The data is in the
correct number of
data type. correct range entered correct format
characters

Example: Integer, Real, String or


Between 1 and 2 Reject blank inputs dd/mm/yyyy password
Boolean

divider1 = date[2:3]
Code example: If not if choice < "1" or divider2 = date[5:6]
If len(choice)!=13:
If choice == "":
year.isnumeric(): choice > "3": if divider1 != "/"
valid = False valid = False
valid = False valid = False or divider2 != "/":
valid = False

It might be possible to Whitelists can be used to store all the valid data inputs a program should accept. E.g. A, B, C in a menu.
cast an input string Blacklists are invalid data inputs a program should reject. E.g. /?* in filenames.
into a number after
input.
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

Defensive design: Anticipating misuse

Even with valid inputs there are a number of reasons why a program could crash. These should be trapped by the programmer with exception handling code.

Disk error
Communication error

Printer and other


peripheral error

Division by zero

A user might also misinterpret the on-screen prompts, or enter data into the wrong input box. A programmer should plan for all possible eventualities.
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

Defensive design: Authentication

Many computer systems contain secure data or need to be protected against internet bots. A programmer can use authentication techniques to minimise
potential computer misuse.

Another method of authentication is using software such as


reCAPTCHA which verifies a user is human..
This prevents online systems and software from being susceptible
to bots which can submit data automatically to online forms.
Additional facilities can be used for authentication such as two
words on display which are distorted and only humans will be able
to read them.

One method of authenticating is with a username


and password to access systems.
If a user has forgotten their password they can
recover this by clicking on a link within an email sent
to the registered address.
Programmers should also be aware of SQL injection
hacks and other methods used by hackers.
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

#--------------------------------------------------------------------------
-
Maintainability def gcf_of(factors1,factors2):
#Finds the greatest common factor (gcf) in two input lists
index = 0
The problem with the program below is that it is #Check all the numbers in the factors1 list until the same number is
very difficult to understand what is happening. found in the factors2 list
#Needs the lists to be in numerical order
def gcf(f1,f2): while factors1[index] not in factors2:
x = 0 index = index + 1
while f1[x] not in f2: #Return the highest number found in both lists
x = x + 1 return factors1[index]
return f1[x]
def fcts(x): #--------------------------------------------------------------------------
f = [] -
for c in range(x,0,-1):
if x % c == 0: def factors_of(number):
f.append(c) #Returns a list of all the factors for a number
return f factors = []
x = int(input("Enter a number: ")) #Check all numbers from the number input down to 0
y = int(input("Enter a number: ")) for countdown in range(number,0,-1):
f1 = fcts(x) #If the number divided by the count down has no remainder...
f2 = fcts(y) if number % countdown == 0:
print(gcf(f1,f2)) #...it is a factor and is added to the list
factors.append(countdown)
return factors
Ways in which the second program has been
made more readable: #--------------------------------------------------------------------------
-
• Use comments to: explain the purpose of the
program, explain sections of code, explain unusual
#Main program starts here
approach’s that were necessary and visually divide #Input the numbers to find greatest common factor
sections of program input1 = int(input("Enter a number: "))
• Use white space to make sections of the program input2 = int(input("Enter a number: "))
easier to see #Find the factors of the two numbers input
• Use indentation for every selection and iteration factors1 = factors_of(input1)
branch factors2 = factors_of(input2)
• Use descriptive variable names and explain their #Output the greatest common factor of the two numbers
purpose print("The GFC of",input1,"and",input2,"is",gcf_of(factors1,factors2))
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

Refining algorithms in order to make them more robust

The following is a very simple program which asks a student for their first initial, the first three letters of their surname, the year of their birth and their age. It
then concatenates these three variables together and outputs the result as a username:
firstInitial = input("Enter your forename initial: ")
surname = input("Enter the first 3 letters of your surname: ")
year = input("Enter the year you where born: ")
age = input("Enter your age in the range 11-19")

username = firstInitial + surname + str(year) + str(age)

print("Your username is:" + username)

This algorithm could be made more robust by:

• Writing code which anticipates a range of possible inputs, those inputs could be invalid data or erroneous data
• Making sure “bad” data doesn’t crash the program
• Making sure prompts to the user are descriptive and helpful
• Making sure only data of the correct “data type” are entered
• Checking and handling missing or blank data
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

The purpose and types of testing

Four main reasons why a program should be thoroughly tested before being given to a user:

To check that the program has an


To ensure there are no errors in To ensure that unauthorised To check the program meets the
acceptable performance and
the code access is prevented requirements
usability

Iterative testing Final/Terminal testing

Performed whilst the software is being developed Performed when the program is finished

Program branches are checked for functionality Testing that all the modules work together

Checking new modules do not introduce new Testing the program produces the required results
errors with normal boundary, invalid and erroneous data

Tests to ensure the program handles erroneous Checking the program meetings the requirements
data and exceptional situations with real data
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

How to identify syntax and logic errors

Program to accept an input of 1, 2 or 3 only using a whitelist: Correct program:

valid = False valid = False


while not valid: while not valid:
valid = True valid = True
print("1. Play game") print("1. Play game")
prnt ("2. Save game") print ("2. Save game")
print("3. Quit") print("3. Quit")
choice = input("Enter choice:") choice = input("Enter choice:")
if not choice in ["1","2","3"]: if not choice in ["1","2","3"]:
valid = False valid = False

print("Option",choice,"chosen.") print("Option",choice,"chosen.")

Type of error in the program:

Syntax

Reason this is an error:

The command print is spelled wrong therefore it was not


recognised
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

How to identify syntax and logic errors

Program to accept an input of 1, 2 or 3 only using a whitelist: Correct program:

valid = True valid = False


while not valid: while not valid:
valid = True valid = True
print("1. Play game") print("1. Play game")
print("2. Save game") print ("2. Save game")
print("3. Quit") print("3. Quit")
choice = input("Enter choice:") choice = input("Enter choice:")
if not choice in ["1","2","3"]: if not choice in ["1","2","3"]:
valid = False valid = False

print("Option",choice,"chosen.") print("Option",choice,"chosen.")

Type of error in the program:

Reason this is an error:


GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

How to identify syntax and logic errors

Program to accept an input of 1, 2 or 3 only using a whitelist: Correct program:

valid = True valid = False


while not valid: while not valid:
valid = True valid = True
print("1. Play game") print("1. Play game")
print("2. Save game") print("2. Save game")
print("3. Quit") print("3. Quit")
choice = input("Enter choice:") choice = input("Enter choice:")
if not choice in [1,2,3]: if not choice in ["1","2","3"]:
valid = False valid = False

print("Option",choice,"chosen.") print("Option",choice,"chosen.")

Type of error in the program:

Reason this is an error:


GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

How to identify syntax and logic errors

Program to accept an input of 1, 2 or 3 only using a whitelist: Correct program:

valid = True
while not valid:
valid = True
print("1. Play game")
print("2. Save game")
print("3. Quit")
choice = input(Enter choice:)
if not choice in [1,2,3]:
valid = False

print("Option",choice,"chosen.")

Type of error in the program:

Reason this is an error:


GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

How to identify syntax and logic errors

Program to accept an input of 1, 2 or 3 only using a whitelist: Correct program:

valid = True
while not valid:
valid == True
print("1. Play game")
print("2. Save game")
print("3. Quit")
choice = input(Enter choice:)
if not choice in [1,2,3]:
valid = False

print("Option",choice,"chosen.")

Type of error in the program:

Reason this is an error:


GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

Selecting and using suitable test data

A program is to be written which will accept a score entered by the user in the range 0-110.
The user enters the following inputs, these are examples of the following categories of data:

Data entered: Type of data: Explanation of this type of data: Additional example
of this type of data:

57 Normal This data is accepted by a program without causing any errors 88

0 Boundary This data is correct but it is on the edge of accepted validation boundaries 110

105 Normal This data is accepted by a program without causing any errors 3

“Dave” Erroneous This data is incorrect which is then rejected by the computer system “Password”
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

Selecting and using suitable test data

A typical test table for the program that asks the user to make a choice from 3 items numbered 1-3:

1. Play game
2. Save game
3. Quit
Enter choice:

Test No. Data input Type of test Expected output It is important to test a range of valid, invalid and
erroneous data inputs.
1 No data Reject input

2 2 Normal data Accept input

3 1 Boundary data Accept input

4 j Erroneous data Reject input

5 2.5 Invalid data Reject input

6 8 Invalid data Reject input

7 # Erroneous data Reject input

9 3 Boundary data Accept input


GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

Selecting and using suitable test data

In September 2017, Twitter announced it was testing doubling the number of characters in a tweet from 140 to 280 characters. Twitter’s character limit is a
holdover from the app’s early days when tweets were sent as texts, which were limited to 160 characters. It has since become one of the product’s defining
characteristics. A typical test table that could be used:

Test No. No. characters Type of test Reason for the test
input

1 No data To see how a program will respond when a user goes to tweet a message with no data.

2 280 Boundary data To see if the data inputted which is on the edge of accepted validation limits will process
properly.

3 -8 Invalid data To see how the program will respond when the correct type of data is inputted but it is out of
accepted validation checks.

4 115 Normal To make sure normal tweets will run properly

5 216 Normal Repeat to make sure any random number within the bound of validation are working.
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

Assessment Target: Overall grade:

Minimum expectations by the end of this unit


 You should have learnt terms 200-217 from your GCSE Level Key Terminology during this unit.
 You have completed all the pages of the workbook
 Score 80% in the end of unit test.

Feedback
Breadth Depth Understanding

 All aspects complete  Excellent level of depth  All work is accurate

 Most aspects complete  Good level of depth  Most work is accurate

 Some aspects complete  Basic level of depth shown  Some work is accurate

 Little work complete  Little depth and detail provided  Little work is accurate

Comment & action Student response


GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

Reflection & Revision checklist


Confidence Clarification
 I can explain the following defensive design consideration: input validation.

 I can explain the following defensive design consideration: anticipating misuse.

 I can explain the following defensive design consideration: authentication.

 I can explain how adding comments improves the maintainability of my code.
 I can explain how indentation and white space improves the maintainability of my code.
 I can explain how use of sub programs improves the maintainability of my code.
 I can explain how adopting naming conventions improved the maintainability of my code.
 I can explain the purpose of testing.
 I can explain what is meant by iterative testing.
 I can explain what is meant by final / terminal testing.
 I can identify both syntax and logic errors in code.
 I can select and use suitable test data for a program.
 I can explain what is meant by “normal” test data.
 I can explain what is meant by “boundary” test data.
 I can explain what is meant by “Invalid” test data.
 I can explain what is meant by “Erroneous” test data.
 I can refine algorithms in order to make them more robust.
My revision focus will need to be:

You might also like