Name: Specification & Learning Objectives: by The End of This Topic You Will Have Studied
Name: Specification & Learning Objectives: by The End of This Topic You Will Have Studied
Name: Specification & Learning Objectives: by The End of This Topic You Will Have Studied
Name:
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
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
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
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
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
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.
#--------------------------------------------------------------------------
-
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
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")
• 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
Four main reasons why a program should be thoroughly tested before being given to a user:
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
print("Option",choice,"chosen.") print("Option",choice,"chosen.")
Syntax
print("Option",choice,"chosen.") print("Option",choice,"chosen.")
print("Option",choice,"chosen.") print("Option",choice,"chosen.")
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.")
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.")
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:
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
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
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.
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
Feedback
Breadth Depth Understanding
Some aspects complete Basic level of depth shown Some work is accurate
Little work complete Little depth and detail provided Little work is accurate
I can explain the following defensive design consideration: anticipating misuse.
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: