You are on page 1of 17

GCSE J277 Unit 2.

3 | Producing robust programs Craig’n’Dave

Name: Zainuddin Subedar

Specification and 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 textbook 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: Checking that data input by user meets specific criteria/rules before processing

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

The includes the


Explanation: The data is the correct The data is in a given All required data has The input is in the correct/minimum/ma
data type range been entered correct format ximum number of
characters
Example: Integer, real, string
Between 1 and 2 Reject blank inputs DD/MM/YYYY password
or 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 may be possible to Whitelists can be used to store all data inputs that a program should accept – e.g., A, B and C.
cast a string to a Blacklists are used to store data inputs that a program should reject – e.g., illegal characters in file names.
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 issues should be identified by the programmer via exception handling.

The disk being out of space


The dara being corrupt

File/folder not being


found

The end of the file


being reached

A user might also misinterpret on-screen prompts or enter data into the wrong input box. The programmer should plan for every eventuality.
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.

Use anti AI-technologies to prevent bots from constantly spam


entering
People who find authentication challenging may use the “ read
about” features

If a user forgets their passwords, they can recover


their account by using a phone number or email
address.
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: #--------------------------------------------------------------------------
-

#Main program starts here


#Input the numbers to find greatest common factor
Comment added to separate indentations used input1 = int(input("Enter a number: "))
Relevant descriptive variables input2 = int(input("Enter a number: "))
Constants used #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 to make them more robust

The following is a very simple program that 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 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:

Separating the sections relevantly, which helps with organisation


Use comment to: explain the purpose of the program
Explain sections of the code
Explain unusual approaches that were necessary
Visually divide sections of a program
Use indentation and white space to make the program easier
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 release:

To check the program meets To make sure unauthorised access


To reduce the chances of glitching To ensure there are no errors
requires is prevented

Iterative testing Final/Terminal testing

Each new module is tested as it is written A beta test may find errors

Program branches are checked for


Testing all modules work together
functionality

Check new modules don’t introduce Checking programs meet


new errors in existing codes requirements
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 using only 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:

Has wrong spelling


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 using only 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:

logic

Reason this is an error:

Wrong logic should be false instead of true as there is already a


true
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 using only 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:

Syntax

Reason this is an error:

Wrong error, missed out on the ‘’ “


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 using only 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:

Logic

Reason this is an error:

Missed out “”and had true instead of false


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 using only 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:

logic

Reason this is an error:

Had a true instead of false and extra =


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

Selecting and using suitable test data

A program is written to accept a score entered by the user in the range of 0 – 100.
The user enters the following inputs, which 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 Data which should be accepted by a program without causing errors 24

0 Boundary Data of the correct type on the edge of the accepted validation boundary 100

105 invalid Data of the correct tyoe but out side accepted validation checks -105

Dave Erronuous Incorrect type of data <>?<@


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

Selecting and using suitable test data

Typical test table for a program that asks the user to make a choice from three items numbered 1 – 3:

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

It is important to test a range of valid, invalid and


erroneous data inputs.
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 and limited to 160 characters. It has since become one of the product’s defining
characteristics. Here is a typical test table that could be used:
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave

Assessment Target: Overall grade:

Minimum expectations by the end of this unit


 Learn terms 200-217 from GCSE Level Key Terminology.
 Complete all 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 and action Student response


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

Reflection and 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 code.
 I can explain how indentation and white space improves the maintainability of code.
 I can explain how the use of sub-programs improves the maintainability of code.
 I can explain how adopting naming conventions improves the maintainability of 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 to make them more robust.
My revision focus:

You might also like