40
11 -1 Chapter 11

Data Validation

  • Upload
    maida

  • View
    39

  • Download
    2

Embed Size (px)

DESCRIPTION

Chapter 11. Data Validation. Chapter Contents. Avoiding Logic Errors by Validating Input What to Do If Input Errors Occur Global Considerations in COBOL When Data Should Be Validated Understanding Program Interrupts Other Methods for Improving Program Performance. Debugging Tips. - PowerPoint PPT Presentation

Citation preview

Page 1: Data Validation

11-1

Chapter 11

Page 2: Data Validation

Avoiding Logic Errors by Validating Input What to Do If Input Errors Occur Global Considerations in COBOL When Data Should Be Validated Understanding Program Interrupts Other Methods for Improving Program

Performance

11-2

Page 3: Data Validation

Use DISPLAY statements during test runs to isolate logic errors

Include test data that produces size errors if ON SIZE ERROR routines are used

For every IF statement, use test data that satisfies and does not satisfy condition

Compile often

11-3

Page 4: Data Validation

Risk of data entry errors is high◦ Large volume of data entered◦ Human error keying in data

Invalid input leads to inaccurate output◦ For example, salary reported incorrectly if entered

as 23000 instead of 32000 Input error can cause program interrupt

◦ For example, spaces entered for numeric field used in arithmetic operation

11-4

Page 5: Data Validation

Use NUMERIC class test to ensure field used in arithmetic operation has numeric value

If Amt-In Is Not NumericPerform 500-Err-Rtn

ElseAdd Amt-In To WS-Total

End-If

11-5

Example

Page 6: Data Validation

Use ALPHABETIC class test if field must be alphabetic

COBOL has built in functions for the following: ◦ Values greater than zero (POSITIVE)◦ Values less than zero (NEGATIVE)◦ Value equal to zero (ZERO)

S must be included in PIC to store a negative number NOT POSITIVE is not same as NEGATIVE

◦ IF X IS POSITIVE THEN …

11-6

Page 7: Data Validation

Check key fields if they must contain data

If Soc-Sec-No = SpacesPerform 900-Err-Rtn

End-If

11-7

Example

Page 8: Data Validation

Useful for validity checking as well as other purposes

Two main functions◦ To count number of occurrences of given

character in field◦ To replace specific occurrences of given character

with another character

11-8

Page 9: Data Validation

To count number of times a given character occurs

INSPECT identifier-1 TALLYINGALL identifier-3

identifier-2 FOR LEADING literal-1 CHARACTERS

11-9

Format

Page 10: Data Validation

identifier-1◦ Field to be "inspected"

identifier-2◦ Field where count stored◦ Not automatically set to zero by INSPECT

identifier-3 or literal-1◦ Character to be counted◦ ZERO, SPACE, 8, 'S' are valid entries for literal-1

11-10

Page 11: Data Validation

ALL - every occurrence of specified character in field counted

LEADING - all occurrences of specified character preceding any other character tallied

CHARACTERS - all characters within field tallied◦ Used to determine size of field

Example next slide

11-11

Page 12: Data Validation

Move Zeros To Ct1, Ct2, Ct3Inspect X1 Tallying Ct1 For All SpacesInspect X2 Tallying Ct2 For CharactersInspect X3 Tallying Ct3 For Leading Zeros

Fields Results X1 = bb82b Ct1 = 3X2 = AB32C Ct2 = 5X3 = 00060 Ct3 = 3

11-12

Examples

Page 13: Data Validation

Optional clause after FOR options to count only characters before or after some initial value

BEFORE INITIAL identifier-4AFTER literal-2

11-13

Format

Page 14: Data Validation

Move Zeros To Ct4, Ct5Inspect X4 Tallying Ct4 For All Zeros Before Initial 9Inspect X5 Tallying Ct5 For Characters After Initial 6

Items Results X4 = 05090 Ct4 = 2X5 = 06762 Ct5 = 3

11-14

Examples

Page 15: Data Validation

To replace specified occurrences of a given character with another

INSPECT identifier-1 REPLACINGCHARACTERS ALL identifier-2 BY identifier-3 LEADING literal-1 literal-2 ... FIRST

11-15

Format

Page 16: Data Validation

Literals must be single characters or figurative constants consistent with type of field being inspected

ALL, LEADING, CHARACTERS have same meaning as previously noted

FIRST means first occurrence of literal-1 will be replaced by literal-2

BEFORE/AFTER clause can be used

11-16

Page 17: Data Validation

Inspect Date-In Replacing All '-' By '/'Inspect SSNo Replacing All Spaces By '-'

Field Before AfterDate-In 10-17-02 10/17/02SSNo 123 45 6789 123-45-6789

11-17

Examples

Page 18: Data Validation

Inspect X1 Replacing Leading 'A' By 'Z'Inspect X2 Replacing First 'R' By 'Q'

Field Before AfterX1 AAABBA ZZZBBAX2 RRSST QRSST

11-18

Examples

Page 19: Data Validation

Code often stored in field to shorten record and minimize typing by using the 88 numbered field

For example, 'H' or 'S' may represent pay type of 'Hourly' or 'Salaried'

Use condition names to check validity of coded fields

(covered this a few chapters ago)

11-19

Page 20: Data Validation

05 Pay-Code Pic X.88 Hourly Value 'H'.88 Salaried Value 'S'.

If Hourly Or Salaried ThenPerform Pay-Calc-Rtn

ElsePerform Pay-Code-Err-Rtn

End-If

11-20

Example

Data Division entries

Procedure Division statements

Page 21: Data Validation

Class test - determine if field contains appropriate type of data (NUMERIC, ALPHABETIC)

Determine if data is missing by comparing field to SPACES

Replace spaces in numeric fields with ZEROS using INSPECT statement

11-21

Page 22: Data Validation

Range test - determine if field in established range

Limit test - determine if field exceeds established limit

Determine if field contains valid codes or values using condition-names to document and test fields

Can we use the inspect to solve our data entry problem of the lack of leading zeros and decimal spaces?

11-22

Page 23: Data Validation

Perform class tests first to ensure field is proper type

Use EVALUATE for further validation tests Three common forms

11-23

Page 24: Data Validation

EVALUATE identifierWHEN value(s) PERFORM …

Evaluate Pay-CodeWhen 'H' Perform 300-Hourly-RtnWhen 'S' Perform 400-Salaried-Rtn

End-Evaluate

11-24

Example

Page 25: Data Validation

May also use THRU clause to check range of values

Evaluate AgeWhen 0 Thru 19

Perform 400-Minor-RtnWhen 20 Thru 99

Perform 500-Adult-RtnEnd-Evaluate

11-25

Example

Page 26: Data Validation

EVALUATE TRUEWHEN condition PERFORM …

Evaluate TrueWhen Age >= 0 And <= 19

Perform 400-Minor-RtnWhen Age >= 20 And <= 99

Perform 500-Adult-RtnEnd-Evaluate

11-26

Example

Page 27: Data Validation

Can also use with condition-names

Assume these condition names defined for Age field

05 Age Pic 99.88 Minor Values 0 Thru 19.88 Adult Values 20 Thru

99.

11-27

Example

Page 28: Data Validation

Evaluate TrueWhen Minor

Perform 400-Minor-RtnWhen Adult

Perform 500-Adult-RtnEnd-Evaluate

11-28

Page 29: Data Validation

EVALUATE conditionWHEN TRUE PERFORM …WHEN FALSE PERFORM …

Evaluate Age <= 19When True Perform 400-Minor-RtnWhen False Perform 500-Adult-Rtn

End-Evaluate

11-29

Example

Page 30: Data Validation

1. Print error record displaying key field, field in error and error message

2. Stop the run to preserve data integrity3. Partially process or bypass erroneous

records4. Stop the run if number of errors exceeds

predetermined limit

11-30

Page 31: Data Validation

5. Use switch/If or 88 field to indicate when record contains error

◦ Initialize field to 'N' for no errors◦ Set field to 'Y' anytime an error occurs◦ Process record as valid only if switch field still

'N' after all validation checks

11-31

Page 32: Data Validation

6. Print count totals and compare to manual counts

◦ Print count of all records processed◦ Print count of all errors encountered◦ Print batch totals or count of all records within

specific groups or batches

11-32

Page 33: Data Validation

Meaning of comma and decimal point in numbers in United States is reversed in some other nations

4,123.45 in United States represented as 4.123,45 in other nations

To change representation of numbers in COBOL use SPECIAL-NAMES paragraph

11-33

Page 34: Data Validation

** If you wanted to switch the , and . aroundEnvironment Division.Configuration Section.Special-Names.Decimal-Point is Comma.

Number 4123,45 stored in field with PIC 9999V99

When moved to report-item with PIC 9.999,99 is displayed as 4.123,45

11-34

Page 35: Data Validation

Data Exception◦ Performing operations on field containing

blanks or other nonnumeric characters Arithmetic operation Comparison

◦ Failing to initialize subscript or index Divide Exception - Attempting to divide by zero Addressing Error

◦ Referring to array or table entry with value in subscript or index that exceeds number of entries in table

◦ Improperly coding nested PERFORMs or exiting from paragraph being performed

11-35

Page 36: Data Validation

Operation Error◦ Attempting to access file with READ or WRITE

before opening it or after closing it Specification Error

◦ Attempting to access input area after AT END condition

Illegal Character in Numeric Field◦ May be caused by type mismatch between actual

data and PIC clause◦ Field size specified in PIC clause may not match

actual size of field in record, leading to invalid (nonnumeric) characters from another field being treated as part of numeric field

11-36

Page 37: Data Validation

Two types of program errors◦ Syntax errors◦ Logic errors

Error control procedures can minimize errors but not eliminate them entirely

Page 456 – way to input data file name from users instead of always hard-coding it.

READ…INTO Statement page 456 INITIALIZE statement

11-37

Page 38: Data Validation

Types of error control procedures◦ Range tests◦ Limit tests◦ Format tests◦ Tests for missing data◦ Sequence checks

11-38

Page 39: Data Validation

Other methods to validate data◦ INSPECT statement◦ Condition-names◦ EVALUATE verb◦ Verifying input data

11-39

Page 40: Data Validation

Handling input errors◦ Stop the run◦ Fill erroneous fields with spaces or zeros◦ Stop the run if number of errors excessive◦ Print control listings or audit trails to be checked

11-40