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
Input validation means:
Type of validation:
Explanation: The data is the correct
data type.
Example: Integer, Real, String or
Boolean
divider1 =
date[2:3]
divider2 =
Code example: If not if choice < "1" or If len(choice)!
If choice == "": date[5:6]
[Link](): choice > "3": =13:
valid = False if divider1 != "/"
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.
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.
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
#Check all the numbers in the factors1 list until the same number
The problem with the program below is that it is is found in the factors2 list
very difficult to understand what is happening. #Needs the lists to be in numerical order
while factors1[index] not in factors2:
def gcf(f1,f2): index = index + 1
x = 0 #Return the highest number found in both lists
while f1[x] not in f2: return factors1[index]
x = x + 1
return f1[x] #--------------------------------------------------------------------
def fcts(x): -------
f = []
for c in range(x,0,-1): def factors_of(number):
if x % c == 0: #Returns a list of all the factors for a number
[Link](c) factors = []
return f #Check all numbers from the number input down to 0
x = int(input("Enter a number: ")) for countdown in range(number,0,-1):
y = int(input("Enter a number: ")) #If the number divided by the count down has no remainder...
f1 = fcts(x) if number % countdown == 0:
f2 = fcts(y) #...it is a factor and is added to the list
print(gcf(f1,f2)) [Link](countdown)
return factors
Ways in which the second program has been
#--------------------------------------------------------------------
made more readable: -------
#Main program starts here
#Input the numbers to find greatest common factor
input1 = int(input("Enter a number: "))
input2 = int(input("Enter a number: "))
#Find the factors of the two numbers input
factors1 = factors_of(input1)
factors2 = factors_of(input2)
#Output the greatest common factor of the two numbers
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:
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:
Iterative testing Final/Terminal testing
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
while not valid:
valid = True
print("1. Play game")
prnt ("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
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
105
“Dave”
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
9
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
5
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: