Michael Coughlan

Home > Other > Michael Coughlan > Page 52


  01 SharedRec IS EXTERNAL.

  02 Stock-Id PIC 9(7).

  02 Manf-Id PIC X(5).

  Even though the module only requires access to the Stock-Id, the programmer has to create a dummy Manf-Id

  data item also. A maintenance programmer who was trying to understand how this subprogram worked might spend quite a bit of time trying to figure out the role of the dummy Manf-Id. A naming dependency problem might occur later.

  Suppose a programmer writing a subprogram for the system to validate customer records discovers that the

  seven-digit Customer-Id uses a check digit for validation; so, the programmer decides to use the check-digit validation subprogram that has already been written. To use the subprogram, the programmer must pass the number to be validated through the shared data item. This requires the use of the following declaration:

  01 SharedRec IS EXTERNAL.

  02 Stock-Id PIC 9(7).

  02 Manf-Id PIC X(5).

  Again, a maintenance programmer examining the ValidateCustomerRecord subprogram might wonder why the

  program includes references to Stock-Id and Manf-Id when it is about validating customer records. The maintenance programmer might also wonder why the subprogram has the statement MOVE Customer-Id TO Stock-Id when these

  are clearly two very different items.

  Using IS EXTERNAL Data Items

  Even though using the IS EXTERNAL phrase to create a shared data item has many drawbacks, it may still be preferable to alternative solutions. For instance, sometimes a data item may need to be accessed by many of the subprograms in a system. In that case, your alternatives are to use the IS EXTERNAL phrase to allow the data item to be seen by 1Myers G. Composite/structured design. New York: Van Nostrand Reinhold; 1978.

  423

  Chapter 16 ■ Creating Large SyStemS

  any subprogram that requires it or to pass the data item as a parameter. The problem with passing the data item as a parameter is that many of the subprograms that do not require access to the data item then only serve as conduits through which the data item is passed to a subordinate subprogram. This kind of data is called tramp data.

  Tramp data has a number of drawbacks. It widens the parameter list for subprograms that don’t directly use the data; it exposes those subprograms to unnecessary data, which increases the risk that the data will be compromised; and it unnecessarily complicates the code of those subprograms. Figure 16-8 illustrates the problem. In this system, the data item used by ProgG, ProgI, and ProgK is created in ProgJ. Because none of the subprograms that use the data item are directly called by ProgJ, the data item has to be passed up and down the calling chain as tramp data.

  Figure 16-8. The problem of tramp data. Connecting arrows show the direction of calls. Circle arrows show the direction of data flow

  If you have to use the IS EXTERNAL phrase, there are some things you can do to ameliorate the problems. First, to eliminate the need to create dummy structures, and to reduce exposure to unnecessary data, you should use IS

  EXTERNAL only with elementary data items. Second, only one subprogram should be permitted to assign a value to an IS EXTERNAL data item. All other subprograms should only be allowed to read that value.

  The COPY Verb

  The COPY verb is a library statement that includes prewritten library source code in a COBOL program or a

  subprogram. It is generally used when creating large software systems. These systems are subject to a number of problems that the COPY verb helps to alleviate. For instance, many of the files in a large software system are processed by more than one program. One issue with this is that if each programmer who creates a program or subprogram is allowed to define the files and records used, then there is a strong possibility that in some cases they will get the definitions wrong. They may make errors in defining the key fields (Indexed files); the file organization; the type of access allowed; or the number, type, and size of the fields in a record. At the very least, these kinds of errors will likely result in the failure of the program that contains the erroneous descriptions; but if the program writes to a file, 424

  Chapter 16 ■ Creating Large SyStemS

  bugs may result that are much harder to find. For instance, if one program writes to a file using an incorrect record description while other programs read from the file using the correct description, a crash may occur in one of the correct subprograms rather than in the one that actually has the problem.

  In a large software system, when file, record, or other data descriptions are common to a number of programs, it is very important that those descriptions be described in a central source text library under the control of a copy librarian. In such a system, only the copy librarian has permission to change the data definitions, but any programmer who needs to use the data resource can copy its description into their program using the COPY verb. Using copy libraries makes it more difficult for programmers to make ad hoc changes to file and record formats and makes implementation simpler by reducing the amount of coding required and by eliminating transcription errors. For instance, when a number of programs need to access the same file, the relevant file and record descriptions can be copied from a copy library instead of each programmer having to type their own (and possibly get them wrong).

  The COPY verb can also make some maintenance tasks easier and safer. For instance, if a record description in a copy library is changed, then all that is required for that change to take effect is for each affected program to be recompiled.

  The COPY Metalanguage

  The metalanguage for the COPY verb is given in Figure 16-9. Text can be copied from the copy file or copy library and inserted into the program source code as is, or text words in the copied text can be replaced by the text specified in the REPLACING phrase. If REPLACING is used, then the items before the word BY are the text-matching arguments used to identify the text words in the copied text that should be replaced by the text specified.

  Figure 16-9. COPY verb metalanguage

  How COPY Works

  The COPY verb operates in an unusual way. Whereas other COBOL statements are executed at runtime, a COPY

  statement is executed at compile time. A COPY statement allows programmers to include in their programs the text of frequently used file, record, or other data descriptions. The included text is copied from a copy file or a copy library.

  The COPY statement is similar to the #include used in C or C++.

  When a COPY statement is used in a COBOL program, the source-code text is copied into the program from a copy file or from a copy library before the program is compiled. A copy file is a file containing a segment of COBOL code.

  A copy library is a collection of code segments, each of which can be referenced using a name. Each client program that wants to use the items described in the copy library uses the COPY verb to include the descriptions it requires.

  When COPY statements copy source code into a program, the code can be included without change or the text can be changed as it is copied into the program. The ability to change the code as it is being included greatly adds to the versatility of the COPY verb.

  425

  Chapter 16 ■ Creating Large SyStemS

  How the REPLACING Phrase Works

  When the REPLACING phrase is used, as the text is copied from the copy file, each properly matched occurrence of Pseudo-Text1, Identifier1, Literal1, and COBOL-Word1 in the library text is replaced by the corresponding

  Pseudo-Text2, Identifier2, Literal2, or COBOL-Word2 in the REPLACING phrase:

  • Pseudo-Text is any COBOL text enclosed in double equal signs (for example, ==ADD 1==).

  It allows you to replace a series of words or characters as opposed to individual items.

  • COBOL-Word is any single COBOL reserved word.

  For the purposes of matching, the REPLACING phrase operates on text words. A text word may be defined

  as follows:

  • Any literal, including op
ening and closing quotes

  • Any separator except a space, a pseudo-text delimiter (==), a comma, or a semicolon

  • Any other sequence of contiguous characters bounded by separators, except comment lines

  COPY Examples

  It can be very difficult to get a feel for how REPLACING works by reading textual descriptions alone, so this section presents a number of examples that I hope help your understanding. Listing 16-7 is a simple example that shows how you can use the COPY statement to copy a record description from a copy file. It also shows how to copy a table description from a copy file in a copy library. The REPLACING phrase is used with the second COPY statement to change the size of the table when the text is copied. Don’t look for any significant meaning in this program—it simply shows how you can the COPY statement to include text in your program source code.

  Listing 16-7. Using the COPY Statement to Include Text

  IDENTIFICATION DIVISION.

  PROGRAM-ID. Listing16-7.

  AUTHOR. Michael Coughlan.

  ENVIRONMENT DIVISION.

  FILE-CONTROL.

  SELECT StudentFile ASSIGN TO "STUDENTS.DAT"

  ORGANIZATION IS LINE SEQUENTIAL.

  DATA DIVISION.

  FILE SECTION.

  FD StudentFile.

  COPY StudentRec.

  WORKING-STORAGE SECTION.

  01 Idx PIC 9(3).

  01 NameTable.

  COPY StudentNameTable IN EG-Lib

  REPLACING XYZ BY 120.

  426

  Chapter 16 ■ Creating Large SyStemS

  PROCEDURE DIVISION.

  BeginProg.

  OPEN INPUT StudentFile

  READ StudentFile

  AT END SET EndOfSF TO TRUE

  END-READ

  PERFORM VARYING Idx FROM 1 BY 1 UNTIL EndOfSF

  MOVE Surname TO StudSurname(Idx)

  DISPLAY StudentNumber SPACE StudentName SPACE CourseCode

  READ StudentFile

  AT END SET EndOfSF TO TRUE

  END-READ

  END-PERFORM

  CLOSE StudentFile

  STOP RUN.

  Listing 16-8 is a program used as a container for a number of COPY..REPLACING examples. I inserted comments into the program to indicate the purpose of the particular example, and the output shows that the replacements have been made.

  Listing 16-8. COPY Statements with REPLACEMENT Text

  IDENTIFICATION DIVISION.

  PROGRAM-ID. Listing16-8

  AUTHOR. Michael Coughlan.

  DATA DIVISION.

  WORKING-STORAGE SECTION.

  01 CopyData.

  COPY Copybook1

  REPLACING S BY 15.

  * Changes the size of a data item

  COPY Copybook2 REPLACING ==V99== BY ====.

  * Changes the type of a data item to an integer

  COPY Copybook3 REPLACING "CustKey" BY "MyValue".

  COPY Copybook3 REPLACING CustKey BY NewKey.

  * demonstrates the difference between a literal and a COBOL-Word

  COPY Copybook3 REPLACING CustKey BY

  ==CustAddress.

  03 Adr1 PIC X(10).

  03 Adr2 PIC X(10).

  03 Adr3 PIC X(10).

  02 CustId==.

  427

  Chapter 16 ■ Creating Large SyStemS

  *Changes the CustKey declaration to add some new data items.

  *After REPLACEMENT the included text will be -

  * 02 CustAddress.

  * 03 Adr1 PIC X(10).

  * 03 Adr2 PIC X(10).

  * 03 Adr3 PIC X(10).

  * 02 CustId PIC X(7) VALUE "CustKey".

  PROCEDURE DIVISION.

  BeginProg.

  MOVE "123456789012345678901234567890" TO CustomerName

  DISPLAY "CustomerName - " CustomerName

  MOVE 1234.56 TO CustomerOrder

  DISPLAY "CustomerOrder - " CustomerOrder

  DISPLAY "CustKey value changed to - " CustKey

  DISPLAY "NewKey value - " NewKey

  MOVE "Dublin" TO Adr3

  DISPLAY "CustId value - "CustId

  STOP RUN.

  Summary

  This chapter introduced you to the COBOL elements required when you create a large software system. You learned about subprograms and how to create both contained and external subprograms. The chapter discussed the COBOL

  parameter-passing mechanisms and introduced the LINKAGE SECTION. You learned about state memory and saw how to use the IS INITIAL phrase or the CANCEL verb to create a subprogram that does not have state memory. The chapter covered the need for some shared data items in a system partitioned into subprograms and introduced the IS GLOBAL

  and IS EXTERNAL clauses.

  The final section explored the benefits of holding file, record, and other data descriptions in a centralized library.

  You also learned about the COPY verb, which allows you to include such descriptions in your program’s source code.

  You saw how to use the COPY verb to include the text from a copy file or copy library in your program.

  The next chapter returns to the subject of file handling. You learn about COBOL’s direct-access file organizations: relative files and indexed files. These direct-access file organizations are more versatile than sequential files, and to take advantage of that versatility, COBOL introduces a number of new verbs and makes changes to some of the file-handling verbs with which you are already familiar. Chapter 17 introduces the DELETE, REWRITE, and START verbs and the concepts of the key of reference and the next record pointer. The chapter concludes with a discussion of the advantages and disadvantages of all the COBOL file organizations and when to use one rather than another.

  428

  Chapter 16 ■ Creating Large SyStemS

  prOGraMMING eXerCISe

  ah! exercise time again. if only i had shares in a 2B pencil company.

  Introduction

  it has long been suspected that compatibility of Zodiac signs (also called star signs or birth signs) is a strong indicator of sexual and emotional compatibility. By processing the information in the married persons Date of Birth file, the program you write will test this hypothesis empirically. For each record in the file, the program will use the couple’s dates of birth to identify their signs and discover whether those signs are compatible.

  the program should display the following items:

  • the count of the total number of records in the file

  • the count of the total number of valid records (that is, records where neither is a cusp birth)

  • the count of the number of compatible pairs, and the percentage of the total valid records that

  this represents

  • the count of the number of incompatible pairs, and the percentage of the total valid records that

  this represents

  every Zodiac sign is compatible with itself and five other signs. a chance selection of life partner should therefore result in 50% of the pairings having compatible Zodiac signs. a significant deviation either way would be of interest, but if significantly more than 50% of the pairings have compatible signs, you would have to conclude that Zodiac signs are a good indicator of compatibility.

  The File

  the Census Office has made available to you the married persons Date of Birth file (Listing16-9MPDOB.Dat).

  this file consists of information extracted from the most recent census. the file is an unordered sequential file; each record contains the dates of birth of a married couple. the records have the following description:

  Field

  Type

  Length

  Value

  MaleDOB

  9

  8

  Date in mmddyyyy format

  FemaleDOB

  9

  8

  Date in mmddyyyy format

  The Problem of the Cusp

  in astrology, people whose birth dates fall near the changeover from one sign to the next are
said to be “born on the cusp.” the problem is that these persons may exhibit characteristics from both signs. if this is true, then being born on the cusp may distort the compatibility results. to prevent this, the program should treat as invalid all records where one or both of the dates of birth fall on the cusp.

  The Zodiac Table

  the Zodiac table is given next. it contains the SignName, SignType, StartDate, and EndDate of each sign. the cusp is defined as a two-day gap between the EndDate of one sign and the StartDate of the next and is built into the dates shown in the table.

  429

  Chapter 16 ■ Creating Large SyStemS

  SignType indicates sign compatibility where

  • air and Fire signs are compatible with themselves and with each other.

  • earth and Water signs are compatible with themselves and with each other.

  The Zodiac Table

  SignCode

  Sign

  SignType

  StartDate EndDate

  1

  Aquarius

  Air

  01-22

  02-18

  2

  Pisces

  Water

  02-21

  03-19

  3

  Aries

  Fire

  03-22

  04-19

  4

  Taurus

  Earth

  04-22

  05-20

  5

  Gemini

  Air

  05-23

  06-20

  6

  Cancer

  Water

  06-23

  07-22

  7

  Leo

  Fire

  07-25

  08-22

  8

 

‹ Prev