Learning Outcomes
Students should be able to demonstrate an understanding of and use the following:
• data validation, including presence, length, type and format checks;
• detection and correction techniques for syntax, execution and logic errors; and
• simple error trapping techniques.
There is an old saying in computing: “Garbage in garbage out.” – GIGO. This refers to the fact that a computer system cannot produce correct results from incorrect data. This is – or ought to be – obvious to anyone who understands computers. It is, however, not always obvious to the general public. It is not, in general, possible for a computer system to determine whether or not the data that it has been given is correct. It is, however, possible for it to determine, in some circumstances, whether or not the data is reasonable, given its intended meaning. This process can be automated and is known as data validation. Data validation
“[Data validation] is the automatic checking of data entered into a computer system. Validation involves using the properties of the data to identify any inputs that are obviously wrong. Validation only proves that the data entered is a reasonable value for the computer to accept. It cannot prove that the data entered is the actual value the user intended.”
BCS Glossary of Computing, 13th edition, p 75.
For example, if a user enters the value –5 when asked to enter their age, then this is an obvious error (age can’t be negative), and an automated check should be able to spot it. Similarly, 33 April 2017 is clearly incorrect if a date was expected – again an automated check should be able to spot this. We refer to these as invalid values and computer systems should have validation checks that prevent invalid values being entered. Validation checking cannot of course catch all errors. For example, a user may mistakenly enter the value 21 as their age, when they are actually 22. Errors of this kind cannot generally be caught by validation checks. The following kinds of validation check are commonly used: presence checks, length checks, type checks and format checks.
Presence Checks In entering data, it may sometimes be acceptable to leave out some values. For example, in collecting a user’s contact details it may be acceptable for the user to leave the telephone number field blank. On the other hand, other fields may be compulsory – for example, there is little value in collecting contact details without the user’s name. A presence check is a validation check that ensures that data has been entered into all compulsory fields.
It is often possible to anticipate appropriate lengths for certain text values. For example, it might be reasonable to say that a user’s name must contain more than 1 character and less than (say) 20. A range check is a validation check that ensures that data values are of appropriate length.
Data values are normally required to be of specific types. For example, a user’s age must be numeric, while their name must be text. A type check is a validation check that ensures data values are of the appropriate type.
Sometimes data is required to be entered in a predefined pattern or format. For example, dates are often entered as three pairs of digits such as 27/05/17. A format check is a validation check that ensures data values are in the expected format.
Errors in a computer program are sometimes known as bugs. Anyone who has developed more than the very simplest of computer programs will know hat errors are inevitable and that finding and fixing bugs is an important part of the software development task. “If debugging is the process of removing software bugs, then programming must be the process of putting them in.” ~ Edsger Dijkstra http://www.azquotes. com/quote/561997 Most computer program errors may be classified as either: syntax errors, execution errors or logic errors.
Syntax Errors
“[Syntax errors] occur either when the program statements cannot be understood because they do not follow the rules laid down by the programming language, statement syntax errors, or when program structures are incorrectly formed, program syntax errors or structure errors.” BCS Glossary of Computing, 13th edition, p 301.
Execution Errors
“[Execution errors] or run-time errors are errors that are detected during program execution. These errors, such as overflow and division by zero, can occur if a mistake is made in the processing algorithm or as a result of external effects not catered for by the program, such as lack of memory or unusual data.” BCS Glossary of Computing, 13th edition, p 301.
Logic Errors
“[Logical errors] are mistakes in the design of a program, such as the use of an inappropriate mathematical formula or control structure, recognised by incorrect results or unexpected displays.” BCS Glossary of Computing, 13th edition, p 301.
Debugging is the process of looking through code to identify and fix errors.
A NameError in Python is an execution error – sometimes called an exception. When an error such as this occurs we say that an exception has been raised. An exception handler is a section of code that is designed to trap these errors. The most straightforward way to deal with this exception is to use Python’s try-except construct. This construct tells Python to try to complete a task but also specifies alternative code that is invoked if the event of an error.
Keywords
Possible Exam Questions
1) Each of John’s tests was marked out of 25. Complete the following sentences by inserting the appropriate word from the list below. (Use each word only once) RANGE VALIDATION PRESENCE [3]
__________________is the action of checking data to ensure it is acceptable. A ______________________check will be required to ensure that the value entered for the Score is valid. A ________________________________check will ensure that NameOfTest1 is actually entered.
2)Programs often contain syntax errors and logic errors. Explain what syntax and logic errors are within a computer program.
Syntax errors [2]
Logic errors [2]
3) (b) When Sally enters numberOfFrames for an order, the program outputs the following error message: “The number of frames entered is invalid, please re-enter”.
(i) Write an algorithm or code which will ensure that users enter a valid value for numberOfFrames. [8]
4)
5) (a) Select the statement that is true about data validation: [1]
A Data validation involves entering data twice to check for errors.
B Data validation ensures that data entered is complete and falls within specified boundaries.
C Data validation can eliminate erroneous data completely.
D Data validation aims to increase the amount of erroneous data accepted by the program.
6)