2. What we’ll learn
• Garbage in, garbage out
• The input validation loop
• Defensive programming
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
2
3. Bad Data – Garbage In, Garbage Out
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
3
4. Characteristics of a Program
• If a user provides bad data as input to a program, the program will
process that bad data and, as a result, will produce bad data as output.
• As computers cannot tell the difference between good data and bad
data.
• Programs should be designed to reject bad data that is given as input.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
4
5. Example
// Variables to hold the hours worked, the
// hourly pay rate, and the gross pay.
Declare Real hours, payRate, grossPay
// Get the number of hours worked.
Display "Enter the number of hours worked."
Input hours
// Get the hourly pay rate.
Display "Enter the hourly pay rate."
Input payRate
// Calculate the gross pay.
Set grossPay = hours * payRate
// Display the gross pay.
Display "The gross pay is ", currencyFormat(grossPay)
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
5
6. The Input Validation Loops
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
6
7. Concept
• Input validation is commonly done with a loop that iterates as
long as an input variable contains bad data.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
7
8. Logic Containing An Input Validation Loop
Get input
If input
invalid?
Display an
error message
Get the input
again
True
False
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
8
9. Example Use Case Designing an Input
Validation Loop
• Previous example of Samantha: calculate the retail price of an item in her import
business.
• Samantha has encountered a problem when using the program, however. Some of
the items that she sells have a wholesale cost of 50 cents, which she enters into the
program as 0.50.
• Because the 0 key is next to the key for the negative sign, she sometimes
accidentally enters a negative number. She has asked you to revise the program so
it will not allow a negative number to be entered for the wholesale cost.
• You decide to add an input validation loop to the showRetail module that rejects
any negative numbers that are entered into the wholesale variable.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
9
10. Pseudocode
Module main()
// Local variable
Declare String doAnother
Do
// Calculate and display a retail price.
Call showRetail()
// Do this again?
Display "Do you have another item?
(Enter y for yes)"
Input doAnother
While doAnother == "y" OR doAnother == "Y"
End Module
// The showRetail module gets an item's wholesale cost
// from the user and displays its retail price.
Module showRetail()
// Local variables
Declare Real wholesale, retail
// Constant for the markup percentage
Constant Real MARKUP = 2.50
// Get the wholesale cost.
Display "Enter an item's wholesale cost."
Input wholesale
// Validate the wholesale cost.
While wholesale < 0
Display "The cost cannot be negative.
Please enter the correct wholesale cost."
Input wholesale
End While
// Calculate the retail price.
Set retail = wholesale * MARKUP
// Display the retail price.
Display "The retail price is $", retail
End Module
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
10
11. Flowchart
showRetail()
Declare
Real wholesale, retail
Constant MARKUP = 2.50
Display “Enter an
item’s wholesale cost”
Input wholesale
wholesale
< 0
Set retail = wholesale * MARKUP
Display “The cost
cannot be negative.
Please enter the correct
wholesale cost”
Input wholesale
Display “The retail price
is $”, retail Return
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
11
13. • Input validation can sometimes be more complex
• Validation - put inside a function
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
13
14. Example
// Get the model number.
Display "Enter the model number."
Input model
While isInvalid(model)
Display "The valid model numbers are 100, 200, and 300."
Display "Enter a valid model number."
Input model
End While
Function Boolean isInvalid(Integer model)
// A local variable to hold True or False.
Declare Boolean status
// If the model number is invalid, set status to True.
// Otherwise, set status to False.
If model != 100 AND model != 200 AND model != 300 Then
Set status = True
Else
Set status = False
End If
// Return the test status.
Return status
End Function
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
14
16. Concept
• Defensive programming is the practice of anticipating errors that can
happen while a program is running, and designing the program to
avoid those errors.
• Input validation is part of the practice of defensive programming.
Thorough input validation anticipates both obvious and unobvious
errors.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
16
17. Common Application Usage
• Country address, number
• Hourly wages, salary
• Dates
• Time, temperature
• Etc….
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
17
18. Summary
• Programs should be designed to reject bad data that is given as input.
• Input validation is commonly done with a loop that iterates as long as
an input variable contains bad data.
• Input validation can sometimes be more complex. Validation - put
inside a function
• Defensive programming is the practice of anticipating errors that can
happen while a program is running, and designing the program to
avoid those errors.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
18
Editor's Notes
The input is read, and then a pretest loop is executed.
If the input data is invalid the body of the loop executes.
The loop displays an error message so the user will know that the input was invalid, and then the loop reads the new input.
The loop repeats as long as the input is invalid.