keys and their importants--01

41
COBOL Programming Standards Author :A J Marston Date Created : September , 19 84 Date Revised : February, 1993 Version : 01.006.000 CONTENTS 1. Introduction 2. Source Code Documentation 3. Program Identification 4. Program Data 4.1 Record Definitions 4.2 Print Line Definitions 4.3 Record Mnemoni cs 4.4 Item Name s 4.5 Working Storag e Definiti ons 4.6 Subscripts and Indices 4.7 Conditi on Names 4.8 Flags/In dicator s/Switche s 5. Program Code 5.1 Intr oductio n 5.2 Progra m Structure 5.2.1 Module Hierarchy 5.2.2 Module Nami ng 5.3 Sections 5.4 Par agr aphs 5.5 Sentences and Stateme nts 5.6 Conditi onal Proces sing 5.7 Hints, Notes and Warnin gs 5.7.1 PERFORM <section >> 5.7.2 PERFORM ... THROUGH ...   5.7.3 The EXIT statement   5.7.4 The GO TO statemen t   5.7.5 The ALTER statemen t  

Upload: kavithamphil

Post on 06-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 1/41

COBOL Programming Standards

Author : A J Marston

Date Created : September, 1984

Date Revised : February, 1993

Version : 01.006.000

CONTENTS

• 1. Introduction• 2. Source Code Documentation• 3. Program Identification• 4. Program Data

○ 4.1 Record Definitions○ 4.2 Print Line Definitions○ 4.3 Record Mnemonics

○ 4.4 Item Names○ 4.5 Working Storage Definitions○ 4.6 Subscripts and Indices○ 4.7 Condition Names○ 4.8 Flags/Indicators/Switches

• 5. Program Code○ 5.1 Introduction○ 5.2 Program Structure

5.2.1 Module Hierarchy 5.2.2 Module Naming

○ 5.3 Sections○ 5.4 Paragraphs○ 5.5 Sentences and Statements○ 5.6 Conditional Processing○ 5.7 Hints, Notes and Warnings

5.7.1 PERFORM <section>> 5.7.2 PERFORM ... THROUGH ...  5.7.3 The EXIT statement  5.7.4 The GO TO statement  5.7.5 The ALTER statement 

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 2/41

5.7.6 MOVE CORRESPONDING ..  5.7.7 The COMPUTE statement  5.7.8 AUTOREADS in VPLUS ..  5.7.9 The SORT statement  5.7.10 Defining and using Print Files 5.7.11 The ACCEPT statement 

• 6. Sample Online Procedures○ 6.1 Introduction○ 6.2 Standard VPLUS macros○ 6.3 Examples

6.3.1 Sample screen processing 6.3.2 Validating screen arrays 6.3.3 Handling numeric values

• 7. Sample Batch Procedures○ 7.1 Report Printing○ 7.2 Control Breaks with sorted files

• 8. Programming Hints○ 8.1 Multi-line Enquiry screens

○ 8.2 Multi-line Input/Update screens

1. Introduction

Nowadays it is common practice to use a particular method of Structured Design beforeproducing the code that will fulfil a particular function. No matter what DesignMethodology is used, the whole object of using structured programming techniques islost if the actual code produced is badly written. Not only must the code fulfil its desiredfunction, and perform it efficiently, it must also bemaintainable by all those

programmers who follow in your wake. In the life of any program more time is spent onmaintenance and enhancement than was ever spent on the original implementation.

COBOL is a flexible, free-format language that has very few internal constraints. It doesnot enforce any particular structural method, instead it allows the individual to adoptwhatever structure their particular level of mentality can imagine.

The author of this document has over a decade's worth of experience in writing COBOLprograms, and has encountered many different standards, each with its own set of strong as well as weak points. Some are too rigid and unnecessarily restrictive, whileothers are too flexible and open to misinterpretation. Those which do not encourage or promote efficient, readable and maintainable code have been discarded, and theremainder have been reviewed and modified in the light of experience, common sense

and logic.In those installations where programmers are allowed to adopt whatever style takestheir fancy it is sometimes necessary to be familiar with an individual's style before it ispossible to work on one of their programs. If a program has been worked on by severaldifferent programmers the mixture in styles can sometimes be very confusing. However,in those installations where proper standards and a common style of coding have beenadopted it should be possible for any programmer to work on any program withoutknowing the identity of the previous authors.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 3/41

This is known as "ego-less programming", and is something which this document isintended to encourage and promote. These standards are meant to show how code canbe written in such a way as to be more readable, easier to understand and thereforemore maintainable. A well-written program should read like a piece of prose, not abunch of gobbledygook.

2. Source Code Documentation

I am a great believer in having my source code liberally sprinkled with documentation inthe form of comment lines (those with an asterisk in column seven). Any program whichdoes not contain comments is immediately suspect - it may actually work, but how easyis it to maintain or modify? There may be separate written documentation, but there redistinct advantages in producing self- documenting code:

• Separate documentation (if it exists at all!) has a habit of becoming `misplaced', whereas theprogram's source code is always available, either on magnetic disc or tape, or in a filedlisting.

• Separate documentation has a habit of not being updated to reflect changes made to thesource code. All too often documentation updates are left till last, or not done at all.

• It is sometimes quite difficult to identify which piece of code is responsible for performing aparticular function outlined in the documentation.

How can self-documenting code be produced?

• Meaningful data names in the data division.

• Meaningful section/paragraph names in the procedure division.

• Plenty of comment lines to explain to the reader what is happening, and why.

It helps if all comments are in lower-case, to differentiate from actual commands whichshould always be in upper-case. Leave only one space between the asterisk in columnseven and the start of the comment - more spaces are laborious to enter in the firstplace, and certainly don't make the comments easier to read.

There is one acid test to prove that your program is adequately documented - can youreconstruct all the written documentation from your source code? If the answer is NO,then you have failed. There are some products available nowadays which will do the jobfor you - they read your source code and produce all the separate documentation thatyou require. It should be obvious, therefore, that products such as these cannot extractinformation which is not there to begin with.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 4/41

3. Program Identification

The IDENTIFICATION DIVISION of any COBOL program must contain at least theprogram name. This is also the best place in which to record other useful informationwhich will serve as an introduction to the program as a whole. Such `useful information'

can be summarised as follows:

a. PROGRAM NAME - this should follow the company's naming conventions, it should beunique for each program, and should also be linked with the name of the source file.

b. AUTHOR - so we will know who to blame when things go wrong!

c. DATE-WRITTEN - this gives us the age of the original version.

d. DATE-COMPILED - this is automatically inserted by the COBOL compiler, and shows theage of the current version of this program.

e. Program function - what is its purpose? Is it a stand-alone program, or part of a suite of 

programs? When is it run?

f. Database access - what databases are used, and in what modes?

g. Dataset access - what datasets are used, and how are they accessed (read, write, updateor delete)?

h. Non-database file access (MPE, KSAM) - identify which files are accessed, and how.

i. Print files - what reports are produced, and what special stationery is required?

 j. VPLUS processing - what is the name of the forms file, and what are the names of the

individual forms?

k. Subroutine usage - identify any subroutines called by this program, giving the name and abrief description.

l. Amendment history - each time a program is changed you should insert a note giving theamendment date, a brief description of the amendment, plus your name.

Items (a) (b) (c) and (d) can be specified using the standard COBOL IdentificationDivision statements. The remainder should be included as comment lines.

Even though all of the above information can be obtained by close examination of thecode, it saves a lot of time (and time is money!) by having it laid out at the start of each

program listing.

4. Program Data

4.1 Record Definitions

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 5/41

The DATA DIVISION of any program is a mixture of individual record names subdividedinto numerous item names, which in turn can be subdivided into various sub-items.

Items which are not subdivided are known as elementary items. Items which aresubdivided are known as group items, with the record name being equivalent to thehighest level of group item.

In COBOL all data items are defined in the following manner: <level-number> <data-name> <picture clause> 

where

 <level-

number> is a two-digit number which starts at 01 for record names and ascends up to 49

for all data items which are to be considered part of that record definition.

 <data-name>  is the name by which that particular data area can be referenced from within the

program.

 <pictureclause>  describes the format of that particular data-name (alphabetic, numeric, etc.).

The picture clause is required only for elementary items - record names and groupitems assume a picture of PIC X(n) where `n' is the combined length of all thesubordinate items within that group definition.

It is therefore possible to construct a record definition as follows:

01 RECORD-NAME.02 DATA-NAME-1-ALPHA PIC X(2).02 DATA-NAME-2.03 DATA-NAME-3-NUMERIC PIC 99.

03 DATA-NAME-4.04 DATA-NAME-5-ALPHA PIC X(2).04 DATA-NAME-6-NUMERIC PIC 9(5).02 DATA-NAME-7-ALPHA PIC X(6).

This layout, however, is totally unacceptable in a structured program because it is noteasy to determine which elementary items go with which group items. It would be easier to read if some sort of indentation was used to differentiate between the various levelnumbers, and if there was some space between the data name and the picture clause.

This example shows the effect of indenting the data names but not the level numbers:-

01 RECORD-NAME.05 DATA-NAME-1-ALPHA PIC X(2).05 DATA-NAME-2.10 DATA-NAME-3-NUMERIC PIC 99.10 DATA-NAME-4.15 DATA-NAME-5-ALPHA PIC X(2).15 DATA-NAME-6-NUMERIC PIC 9(5).05 DATA-NAME-7-ALPHA PIC X(6).

This example shows the effect of indenting the level numbers, but not the data names:-

01 RECORD-NAME.05 DATA-NAME-1-ALPHA PIC X(2).

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 6/41

05 DATA-NAME-2.10 DATA-NAME-3-NUMERIC PIC 99.10 DATA-NAME-4.

15 DATA-NAME-5-ALPHA PIC X(2).15 DATA-NAME-6-NUMERIC PIC 9(5).

05 DATA-NAME-7-ALPHA PIC X(6).

The following example is the best because it indents both the level number and itsassociated dataname. Note that each different level number is aligned in a separatecolumn, making it easier to spot any mistakes.

01 RECORD-NAME.05 DATA-NAME-1-ALPHA PIC X(2).05 DATA-NAME-2.

10 DATA-NAME-3-NUMERIC PIC 99.10 DATA-NAME-4.

15 DATA-NAME-5-ALPHA PIC X(2).15 DATA-NAME-6-NUMERIC PIC 9(5).

05 DATA-NAME-7-ALPHA PIC X(6).

Note that all the picture clauses are aligned in the same place, usually at column 40.

Where a data name flows into this column the most common practice is to put thepicture clause on the following line, thus keeping the alignment consistent, eg:

05 DATA-NAME-2.10 DATA-NAME-2A.

15 THIS-IS-ANOTHER-LONG-DATANAMEPIC X(2).

15 THIS-IS-YET-ANOTHER-LONG-DATANAMEPIC 9(5).

The guidelines for record layouts can be summarised as follows:-

a. Level numbers should be incremented by more than one in order to leave room for futureadditions of group items without having to renumber a whole section (increments of 01, 03,

05, etc are acceptable, but 01, 05, 10, etc is even better).

b. Level 01 should start in column 8, and all following numbers should be indented so thateach different level number is aligned in a different column.

c. The indentation between one level number and the next should be 4 columns.

d. There should be 2 spaces between a level number and its associated data name.

The effect of items (c) and (d) is that the data names for one level are aligned in thesame column as the level numbers for the next level. This may seem irrelevant, but Ithink that it looks better. When using the QEDIT text editor in full-screen mode it is

possible to define the tab stops at columns 8,12,16,20,24,28 etc, therefore the same tabsettings can be used for both level numbers and data names.

4.2 Print Line Definitions

Now for some advice on how to describe headings for batch reports. Here are threeways of describing the same print line:

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 7/41

Example 1:

01 HEADING PIC X(132) VALUE"ITEM DESCRIPTION QUANTITY UNIT PRIC

- "E TOTAL PRICE VAT".

Example 2:

01 HEADING.05 FILLER PIC X(4) VALUE

"ITEM".05 FILLER PIC X(6) VALUE

SPACES.05 FILLER PIC X(11) VALUE

"DESCRIPTION".05 FILLER PIC X(19) VALUE

SPACES.05 FILLER PIC X(8) VALUE

"QUANTITY".05 FILLER PIC X(2) VALUE

SPACES.

05 FILLER PIC X(10) VALUE"UNIT PRICE".05 FILLER PIC X(2) VALUE

SPACES.05 FILLER PIC X(11) VALUE

"TOTAL PRICE".05 FILLER PIC X(1) VALUE

SPACE.05 FILLER PIC X(3) VALUE

"VAT".

Example 3:

01 HEADING.05 FILLER PIC X(10) VALUE "ITEM".

05 FILLER PIC X(30) VALUE "DESCRIPTION".05 FILLER PIC X(10) VALUE "QUANTITY".05 FILLER PIC X(12) VALUE "UNIT PRICE".05 FILLER PIC X(12) VALUE "TOTAL PRICE".05 FILLER PIC X(03) VALUE "VAT".

Of these three examples you should see that the third is more `readable' and thereforeeasier to maintain.

4.3 Record Mnemonics

Even though COBOL allows the same data-name to be used more than once in

different record definitions (each occurrence can be uniquely addressed by theconstruct  <data-name> OF <record-name> ) it is common practice to make eachoccurrence unique by adding a record mnemonic as either a prefix or a suffix. Eachrecord definition should be allocated its own mnemonic which should then be attachedto all data items within that record.

Please refer to the section on Naming Standards in the manual on Project DevelopmentStandards for more details on how to assign prefix values for IMAGE datasets andVPLUS screen buffers.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 8/41

If a suffix is used it produces code which looks like the following:-

01 ACCOUNT-RECORD.05 ACCOUNT-TYPE-ACC PIC X(2).05 ACCOUNT-NUMBER-ACC.

10 GROUP-ACC PIC 99.10 CLASSIFICATION-ACC.

15 CLASS-CODE-ACC PIC X(2).15 CLASS-NUMBER-ACC PIC 9(5).

05 CLIENT-NAME-ACC PIC X(6).

However, I think a prefix produces better code, as follows:-

01 ACCOUNT-RECORD.05 ACC-ACCOUNT-TYPE PIC X(2).05 ACC-ACCOUNT-NUMBER.

10 ACC-GROUP PIC 99.10 ACC-CLASSIFICATION.

15 ACC-CLASS-CODE PIC X(2).15 ACC-CLASS-NUMBER PIC 9(5).

05 ACC-CLIENT-NAME PIC X(6).

In the procedure division this would produce code similar to this:-MOVE ACC-ACCOUNT-TYPE TO PRT-ACCOUNT-TYPE.MOVE ACC-ACCOUNT-NUMBER TO PRT-ACCOUNT-NUMBER.MOVE ACC-GROUP TO PRT-GROUP.MOVE ACC-<etc> TO PRT-<etc> 

The use of a prefix also makes it easier to search through your source code (with your particular text editor) to list all those places in a program where a record is referenced:

LIST "ACC-"LIST "MOVE ACC-"LIST "TO ACC-"

4.4 Item Names

COBOL allows data names to be up to 30 characters in length. However, mostprograms which run on the HP3000 tend to have some sort of access to either IMAGEdatabases or VPLUS screens, where the length of data items is limited to 16 and 15characters respectively.

Item names should convey some sort of description as to their content, and in order notto exceed this 15-character limit it is sometimes necessary to use abbreviated terms.Whatever abbreviation is chosen it is important that the same one is used throughoutthe system. For example, the following combination of item names within the samesystem is very sloppy:-

LINE-NUMBER  ACCOUNT-NBR INVOICE-NUM DOC-NO

Where the same item appears in more than one place within the system (eg: in anIMAGE dataset, or a VPLUS screen) it is a very good idea to keep to the same spelling.

Where the item is subsequently defined within a program's data division (eg: in thedataset buffer, the screen buffer, or a print buffer) it is also a very good idea to keep to

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 9/41

the same spelling. The only difference should be in the prefix, which acts as an identifier for the particular buffer.

The only exception to this guideline is where an item occurs more than once on aVPLUS screen. As the OCCURS clause does not exist within FORMSPEC it isnecessary to give each occurrence of the item a unique name. This is done by

appending a 2 digit sequence number (starting at "01") to each occurrence of that itemname. This suffix can be dropped within the COBOL definition as the OCCURS clausecan be used instead. There should then be a direct relationship between the hard-codednumber in the forms file and the occurrence number within the COBOL array, asdemonstrated in the following example:

  VPLUS: ITEM-NAME01,ITEM-NAME02,,,,,,,ITEM-NAME09,ITEM-NAME10

COBOL: ITEM-NAME PIC X(?) OCCURS 10.

If it becomes necessary within your program to identify one of these field names (eg: topass to the SCVSETERROR routine after a failure in some validation) it can beconstructed as ITEM-NAMEnn where nn is the occurrence number.

4.5 Working Storage Definitions

Data definitions within a program's WORKING-STORAGE section can be a mixture of dataset buffers, screen buffers, and general work areas.

Each item name should be prefixed by a different mnemonic depending on the recorddefinition in which it appears. IMAGE dataset buffers and VPLUS screen buffers shouldhave been allocated their own unique mnemonics, but what of the general work areas?

Some standards say that all WORKING-STORAGE definitions must be prefixed with theletter `W', but in very large sections it can be a long-winded process to find an individual

definition.Other standards say that the prefix should be two characters in order to denote thegeneral usage, as in the following:

 WA- accumulators WC- constants WI- indicators/flags/switches WM- modifiers/subscripts WT- tables/arrays

However, I much prefer to see groupings of items which are related logically, rather thanfunctionally, as in the following:

01 T01-COMPONENT-TABLE.

05 T01-MAX PIC 99 VALUE 20. <constant> 05 T01-CURR PIC S9(4) COMP. <subscript> 05 T01-LAST PIC S9(4) COMP. <subscript> 05 T01-TABLE-ENTRY OCCURS 20.

10 T01-CODE PIC X(6).10 T01-DESCRIPTION PIC X(30).10 T01-VALUE PIC S9(7)V99 COMP.

05 T01-TOTAL-VALUE PIC S9(7)V99 COMP. <accumulator> 

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 10/41

Even though each item has a different function, they are all logically related, as they areconcerned with that particular table and no other. It also makes it easier to searchthrough the source code to find any reference to this data area simply by specifying asearch string of "T01".

The following definition is also acceptable, but it has a distinct advantage when using

the COBOL'85 compiler, which allows a group of data items to be reset to blanks or spaces with a single statement.

01 T01-MAX PIC S9(4) COMP VALUE 20.

01 T01-COMPONENT-TABLE.05 T01-CURR PIC S9(4) COMP.05 T01-LAST PIC S9(4) COMP.05 T01-TABLE-ENTRY OCCURS 20.

10 T01-CODE PIC X(6).10 T01-DESCRIPTION PIC X(30).10 T01-VALUE PIC S9(7)V99 COMP.

05 T01-TOTAL-VALUE PIC S9(7)V99 COMP.

Although the item T01-MAX has been taken out of the original layout and changed froman 05 level to an 01 level it has still retained the T01- prefix to show that it is still relatedlogically. Thus the statement INITIALIZE T01-COMPONENT-TABLE will correctly reset tozeros/spaces the values of all T01- items except for T01-MAX, which must remain at 20.

As a general rule, then, the WORKING-STORAGE section should be laid out in logicalgroups with prefixes as follows:

Tnn- tables/arrays Wnn- other

These should be defined in their proper sequence so that it is easy to locate anyindividual data item when searching through a program listing. Any copy librarymembers for dataset/file buffers should be correctly sequenced by their respective

prefix values. This avoids the need to produce a cross-reference listing which incurs aconsiderable overhead, both in compilation time and printing time.

4.6 Subscripts and Indices

Some people like to define global subscripts/indices in the form SUB1, SUB2....SUBn(or INDX1, INDX2,,,INDXn). This is bad practice because there is no logical relationshipwith any particular table/array, and the names do not convey any sort of meaning.

It can take a long time to debug a program if the same subscript is accidentally used toreference two different tables at the same time, or two different subscripts are

accidentally used to reference the same table at the same time.This is not a problem if the name of the subscript/index item is constructed in theformat <prefix>-<usage> where:-

 <prefix>  indicates the table to which it has been associated,

 <usage>  indicates how this particular item is used.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 11/41

As can be seen in the previous section the subscript item used to access T01-TABLE-

ENTRY is named T01-CURR . This cannot easily be confused with T01-MAX, which is used toindicate the maximum number of entries the table may contain, and T01-LAST whichindicates the actual number of entries in the table at present.

4.7 Condition Names

Condition names are used to show an expected range of values for a particular dataitem. Once a condition-name has been defined, that particular name can be usedinstead of testing the data item for a specific value (or range of values). Take thefollowing definition in a program's working-storage section:

05 IMAGE-STATUS PIC S9(4) COMP.88 IMAGE-OK VALUE ZERO.88 IMAGE-ERROR VALUE -9999 THRU -1,

+1 THRU +9999.88 IMAGE-BEG-OF-FILE VALUE 10 12.88 IMAGE-END-OF-FILE VALUE 11 13.

88 IMAGE-BEG-OF-CHAIN VALUE 14.88 IMAGE-END-OF-CHAIN VALUE 15.88 IMAGE-DSET-FULL VALUE 16.88 IMAGE-NO-ENTRY VALUE 17.88 IMAGE-LOCK-FAILURE VALUE 20 THRU 25

The contents of IMAGE-STATUS can be determined in two ways:

IF IMAGE-STATUS = ZERO becomes IF IMAGE-OK 

IF IMAGE-STATUS NOT = ZERO becomes

or 

IF NOT IMAGE-OK 

IF IMAGE-ERROR 

With COBOL'85 it is now possible to set a condition by referring to the name instead of the actual value, eg:

 MOVE 0 TO IMAGE-STATUS

is the same as

SET IMAGE-OK TO TRUE

There are only 2 simple guidelines to follow when defining condition-names:

a. Use a meaningful description for each condition;

b. Use a prefix which links the condition-name with the particular data item for which it isdefined. This makes it easier to locate the reference in the working-storage section, and inmost cases should make the condition being tested completely obvious (eg.IMAGE-OK , KSAM-OK or  VPLUS-OK ).

4.8 Flags/Indicators/Switches

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 12/41

As a general rule any program that uses its own set of switches or indicators is badlywritten. Most conditions can be handled by data areas that have already been defined(eg: the status areas for VPLUS, IMAGE and KSAM).

If it does become necessary to use a separate indicator then please follow these simpleguidelines:-

a. Use meaningful data names (not SW1, SW2, SW3, etc).

b. Do not use an indicator for more than one purpose.

Here are a few more observations regarding flags and switches:-

• In an online program it is not necessary to define a flag which shows the success or failureof a validation routine as the item VIEW-NUMERRS is already available for this purpose. Thishas the condition names VEDIT-ERRORS and NO-VEDIT-ERRORS.

• Do not use a flag to test for a condition if that condition can be obtained from other means,as in the following example:

• IF LINE-COUNT > LINE-MAX (ie: the current page is full)• MOVE 1 TO NEW-PAGE-REQUIRED.• ........• IF NEW-PAGE-REQUIRED = 1• PERFORM XA-START-NEW-PAGE.

can be replaced by:

IF LINE-COUNT > LINE-MAXPERFORM XA-START-NEW-PAGE.

• When reading a serial file it is not necessary to define a flag to show when end-of-file hasbeen encountered, as the same result can be achieved by setting the file's record area toHIGH-VALUES, as in the following example:-

•  MOVE LOW-VALUES TO ABC-REC.• PERFORM UNTIL ABC-REC = HIGH-VALUES• READ ABC• AT END• MOVE HIGH-VALUES TO ABC-REC• NOT AT END• PERFORM CA-PROCESS-ABC-REC• END-READ• END-PERFORM.

5. Program Code

5.1 Introduction

The PROCEDURE DIVISION in any COBOL program contains one or moreSECTIONS. Each SECTION contains one or more PARAGRAPHS; each PARAGRAPH

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 13/41

contains one or more SENTENCES; and each SENTENCE contains one or moreSTATEMENTS.

• A SECTION is identified by having a unique name starting in column 8 followed by thereserved-word `SECTION'. It ends with the start of the next section, and NOT when an`EXIT' statement is encountered.

• A PARAGRAPH is identified by having a unique name starting in column 8 (the name mustbe unique within a section, but may be duplicated in other sections). It ends with the start of the next paragraph. There need be no relationship between a SECTION name and itsassociated PARAGRAPH name(s).

• A SENTENCE starts in column 12, can continue over several lines, and is terminated by afull stop.

• A STATEMENT contains a single COBOL verb, a subject name, and an optional objectname (eg: MOVE A TO B, ADD C TO D, PERFORM E).

Control can be passed from one point in a program to another by means of the followingstatements:

PERFORM <section>.PERFORM <paragraph>.PERFORM <paragraph1> THRU <paragraph2>.GO TO <paragraph>.

These guidelines are extremely flexible, but without additional guidelines the resultingcode can be as manageable as a plate of limp spaghetti - please read on for your elucidation and edification.

5.2 Program Structure5.2.1 Module Hierarchy

It is common practice in modern programming to split the code into logical sections or modules. Each section/module performs a particular function, and is invoked by thestatement:

PERFORM <section-name>.

Each section/module can, in its turn, invoke other sections to perform lower levelfunctions. This produces a modular hierarchy or structure, hence the term `structuredprogramming'. It is usual to plan the structure of a program at the design stage beforewriting any code, in order to produce a structure similar to that shown in Figure 1:

Figure 1 - Module Hierarchy

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 14/41

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 15/41

Where the initial design produces a box which is a combination of ITERATION/SEQUENCE or ITERATION/SELECTION most methodologies insist thatthe structure be preserved by splitting the two and performing the ITERATION part at alower level.

5.2.2 Module Naming

It is common practice to give each module some sort of mnemonic to denote its positionin the module hierarchy, both horizontal and vertical. In the example given in section5.2.1 each module is named from left-to-right and top-to-bottom, but there is norelationship between the mnemonics of modules which have some sort of logicalassociation.

There are two other methods of arriving at mnemonic names, and these are discussedbelow:

a. POSITIONAL - the mnemonic has two alphabetic characters, the first denotes the LEVEL(top to bottom) while the second denotes the SEQUENCE (left to right). Using the samehierarchy as shown in Figure 1 this is demonstrated in Figure 2:

Figure 2 - Module Hierarchy (positional)

b. FUNCTIONAL - the first character denotes the sequence of the top-level function, while thesecond and subsequent characters denote a combination of level-within-function andsequence-within-level. This is demonstrated in Figure 3:

Figure 3 - Module Hierarchy (functional)

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 16/41

Now let us discuss the relative merits of both methods.

It is common sense when coding the procedure division to place the sections inmnemonic sequence - this makes it easier to find your way through the listing.In method (b) this means that each leg within the program structure forms a contiguousblock, whereas inmethod (a) the same sections are spread out all over the place. This isshown more clearly in Figure 4:

Figure 4 - Positional vs. Functional naming

Positional Functional

 

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 17/41

Using method (b) it is possible to increase or decrease the number of sections in aparticular processing leg without having to rename any other sections in order tomaintain the naming sequence. See how easy it is in method (b) to split section "DA"into "DAA" and "DAB", then try and make the corresponding change in method (a).

Another advantage of method (b) is that when a program becomes too large for a singleunit and needs to be segmented or split into subprograms it is easy to identify whichmodules are logically related and therefore eligible for separation. Compare thosemodules marked above in method (b) with the corresponding modules from method (a).

In a large, complex program with many modules method (b) may produce mnemonicswhich are large and cumbersome - how can this be overcome? Common sense to therescue - identify a sub-function with a large number of modules and start naming themusing a different letter from the alphabet, as shown in Figure 5:

Figure 5 - Restarting the prefix character 

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 18/41

For a sorted report which has a large number of control breaks it is acceptable tointroduce a number into the mnemonic, and to increment this number for each lower level instead of increasing the length of the mnemonic by adding another character.This would produce a structure chart something like the one in Figure 6:

Figure 6 - Numerical Sequencing

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 19/41

With this method all the processing for a particular sort level would be held incontiguous sections as the sequence would be as follows:-

R, R1, R1A, R1B, R2, R2A, R2B, R3, R3A, R3B, R4, R4A, R4B, R5

Some standards have a rule that if a section has more than one parent (ie: can becalled from more than one section at a higher level) then it should be allocated a prefixwhich represents a general-purpose routine, usually at the bottom end of the alphabet(eg: X, Y or Z). This is fine if it is a small section with limited processing, but what if it is

 just the start of a significant piece of coding?

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 20/41

Take the following example - A database is comprised of customers who are groupedinto countries, which in turn are grouped into regions. A program processes the detailsfor customers at the end of each month, but the selection criteria can be one of thefollowing:

1. A single customer,

2. A single country, for all customers within that country,

3. A single region, for all countries within that region,

4. All regions (ie: all customers).

This would produce a structure chart something like the one shown in Figure 7:

Figure 7 - A Module with multiple parents

Notice that the selection processing is handled by modules which have the "C" prefix,while the actual customer processing (regardless of the selection method) has aseparate prefix. This makes tracing your way through the source code a lot easier,which is more important than conforming to inflexible rules with their artificial restraints.

5.3 Sections

The guidelines for sections can be summarised as follows:-

a. Each section should start on a new page.

b. Each section name should be prefixed by a unique mnemonic containing one or morealphanumeric characters to denote that section's position in the program's hierarchy of sections.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 21/41

c. Each section name should be a short but meaningful description of the function of thatsection (eg: CA-SELECT-COMPONENT, DEA-UPDATE-COMPONENT).

d. The entire PROCEDURE DIVISION should be arranged in mnemonic sequence so thatindividual sections can be found more easily.

e. Control should be passed from one section to another by means of the PERFORM  <section> statement, and not the GO TO <paragraph> statement, nor the PERFORM 

 <para1> THRU <para2> statement.

f. Each section must have a single entry point (the first paragraph) and a single exit point (thelast paragraph).

g. Each section should be limited to perform one basic function, whether it be HIGH-LEVEL(eg: deciding which selection of lower level procedures to perform) or LOW-LEVEL (eg:reading from or adding to a data file).

h. Each section should be short enough to be contained on a single page of listing paper.

Anything longer than this should be broken down into a series of sub-sections, each with itsown separate function.

i. Between the section-name and first paragraph-name there should be a brief comment asto the overall function of that particular section (leave more detailed comments until you getto the actual code). It helps to make this description more distinctive if it is enclosed by aline of asterisks immediately before and after, producing a `box' effect.

5.4 Paragraphs

The guidelines for paragraphs can be summarised as follows:-

a. Each paragraph name should be a short but meaningful description of the function of thatparagraph.

b. Each paragraph name should be prefixed by the mnemonic that has been previouslyassigned to that section, plus an ascending sequence number, (eg: BA-10-READ-NEXT, BA-20-UPDATE).

c. This sequence number should be in increments of ten, starting at ten for each new section.This leaves adequate room for future additions without having to renumber existingparagraphs.

d. Two digits are adequate for the sequence number - if any more are required it means that

you have already broken the guideline regarding the size of your section.

e. The last paragraph should contain nothing but the EXIT statement, and need not have asequence number (eg: BA-99-EXIT or BA-EXIT).

f. If you need to pass control to another point within the same section use the GO TO

 <paragraph-name> statement. Do NOT use the PERFORM <paragraph-name> statement.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 22/41

g. If you need to pass control to another section use the PERFORM <section-

name> statement. Do NOT use the GO TO statement on anything other than a paragraph-name within the current section.

h. If you feel it necessary to insert a comment (or two) about the paragraph, please put itAFTER the paragraph name and BEFORE the first line of code.

5.5 Sentences and Statements

The guidelines for sentences and statements can be summarised as follows:-

a. Each sentence should start in column 12.

b. Each statement within a sentence (if there is more than one) should start on a separateline. This means that statements can be added or deleted quite easily, without having tochange other lines unnecessarily.

c. If a statement is too long to fit on one line DO NOT split a data-name across two lines -instead move the whole name (along with the TO, FROM , BY or GIVING portion of the verb)onto the next line, indenting by four character positions.

d. Do not use weird spacing between each word - one is enough! There is only one exceptionto this guideline that I find acceptable, and that is when there is a group of  MOVE ..... TO..... statements. In this case it makes the code more readable if all the TO portions arealigned under a common column, as in the following example:

e. MOVE "AA" TO ACC-ACCOUNT-TYPE.f. MOVE ABC-GROUP TO ACC-GROUP.g. MOVE ABC-CLASSIFICATION TO ACC-CLASSIFICATION.h. MOVE ABC-NAME TO ACC-NAME.

guidelines (c) and (d) come in handy when searching a source file for a particular references to a data-name, as with:

LIST "MOVE ABC-GROUP"LIST "TO ACC-GROUP"

This is not so easy to do if the data-name is split in two, or if more than one space is used

between words.

i. Do not put a space line between individual statements. It is permissible, however, to have aspace line between groups of statements in order to show a logical separation.

 j. Even though it should be obvious WHAT a piece of code does, it may not be obvious WHY

it does it, or WHY it is necessary to do it at that particular point in the program. To aid themaintenance programmer some sort of comment may be worth its weight in gold. Don'tworry if the comment stretches over several lines - a single- line comment is worthless if itis incomplete.

5.6 Conditional Processing

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 23/41

This is where a lot of uneducated programmers come unstuck! Even though COBOLallows the following:

IF <condition> {THEN} <statement-1> ELSE <statement-2> {END-IF}.

There are some basic guidelines which can be applied in order to make the code morereadable and easier to maintain. These are:

a. Each portion (condition, ELSE, statement-1, statement-2, END-IF) should be on a separateline. This allows for future additions or deletions without having to modify more lines than isnecessary.

b. The word ELSE should be aligned in exactly the same column as the IF to which it isassociated. This makes the association more obvious in the listing, especially with multipleor nested IFs.

c. COBOL'85 allows each condition to be terminated with an END-IF. Its use should beencouraged as it makes it absolutely clear where each condition is supposed to end, thusavoiding the possibility of confusion and mistakes. Like the ELSE, the END-IFshould be

aligned in exactly the same column as IF with which it is associated.

d. Statement-1 and statement-2 should be indented, usually by four character positions. Thisallows the IF, ELSE and END-IF to be more distinctive in the listing.

This now gives us the following construction:

IF <condition> <statement-1> 

ELSE<statement-2> 

END-IF.

Here are some extra guidelines for nested IFs:

e. For each level of nested IF indent all associated lines by four characters. This gives thefollowing:

f. IF <condition-1> g. IF <condition-2> h. <statement-1> i. ELSEj. <statement-2> k. END-IFl. ELSE

  m. <statement-3> n. END-IF.

o. Don't ever use more than three levels of nested IF - they are extremely difficult to debugand maintain.

p. Remember that each ELSE is paired with the IF that immediately precedes it in the code,not necessarily the one under which it is aligned. Take the following example:

q. IF <condition-1> r. IF <condition-2> 

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 24/41

s. <statement-2> t. ELSEu. <statement-1>.

According to the indentation <statement-1> is supposed to be executedif  <condition-1> is false, but COBOL follows its own rules andexecutes <statement1> if  <condition-1> is true and <condition-2> is false. Thistype of error is more avoidable if theEND-IF is used, as in the following example:

IF <condition-1> IF <condition-2> 

<statement-2> END-IF

ELSE<statement-1> 

END-IF.

or... IF <condition1> IF <condition2> 

<statement-2> ELSE

<statement-1> END-IF

END-IF.

v. In the case where an ELSE is immediately followed by an IF without any interveningstatements (ie: where only one out of a series of conditions will be TRUE) it is not

necessary to indent at each new IF otherwise you will quickly fall off the page. Consider the following example:

IF X-VALUE = 1<statememt-1> 

ELSEIF X-VALUE = 2

<statement-2> ELSEIF X-VALUE = 3

<statement-3> ELSEIF X-VALUE = 4

<statement-4> ELSEIF X-VALUE = 5

<statement-5> etc.

IF X-VALUE = 1<statememt-1> 

ELSEIF X-VALUE = 2

<statement-2> ELSE

IF X-VALUE = 3<statement-3> 

ELSEIF X-VALUE = 4

<statement-4> ELSE

IF X-VALUE = 5<statement-5> 

etc.

With the arrival of COBOL'85 this should be written as follows:

EVALUATE X-VALUEWHEN 1 <statement-1> WHEN 2 <statement-2> WHEN 3 <statement-3> WHEN 4 <statement-4> WHEN 5 <statement-5> 

WHEN OTHER .....END-EVALUATE.

Here are even more guidelines for complex conditions:

w. Enclose each individual condition in parentheses.

x. If several conditions combine to form a group condition, (ie. all conditions have to be true inorder to make the group condition true) then enclose the whole group in parentheses aswell.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 25/41

y. By having each condition on a separate line, and by careful alignment of ANDs and ORs, itis possible to make absolutely clear that conditions are linked or are alternatives.

These guidelines should produce something like this:

IF ((A = 1 OR 2 OR 3)AND

(B NOT = 4))OR ((C = "A" OR "Z")

OR (D < E))<statement> 

ENDIF.

This example, however, is rapidly approaching the stage at which it becomes toounwieldy to be maintainable. Don't be afraid to split a complex condition into itscomponent parts, even if it involves the use of the GO TO statement. Don't try toprove how clever you can be - keep it simple and straightforward.

5.7 Hints, Notes and Warnings

5.7.1 PERFORM <section>

Even though COBOL allows the PERFORM command to be used on paragraphs as well assections, its use should be limited to sections only. If necessary make the offendingparagraph into a self-contained section, or an in-line PERFORM (if it is small enough). Thisguideline then avoids the possibility that someone may split the paragraph in two,thereby causing the PERFORM to terminate at the new paragraph name.

Please note that if the word SECTION is missed out on a section name it will be treatedas a paragraph. As there is usually nothing but comments between the section name

and the next paragraph it means that no code will actually be executed bythat PERFORM statement. Unfortunately the code WILL be included in the PERFORM of thepreceding section. This usually causes lots of confusion.

5.7.2 PERFORM ... THROUGH ...

Avoid the use of PERFORM <procedure-A> THROUGH <procedure-Z> . It is good practice toname each procedure (section) explicitly - this makes it easier to search a source file for all occurrences of PERFORM <sectionname> , and also avoids the possibility that someonemay add a new section between existing sections without realising that it now falls

within the bounds of the PERFORM <A> THROUGH <C> statement. A program can producesome very peculiar results if a section is `accidentally' performed out of sequence, andis not easy to debug.

5.7.3 The EXIT statement

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 26/41

The EXIT verb in COBOL does not produce an EXIT instruction in the object program - itis ignored, but is maintained for documentation purposes only. Whenever a sectionis PERFORM ed COBOL will generate its own (hidden) EXIT instruction when the nextsection-name is encountered. If a section-name in your source file has theword SECTION missing, COBOL will treat it as a continuation of the preceding section -

this can cause some very peculiar results (refer to PERFORM <section>).

5.7.4 The GO TO statement

Do not use the GO TO verb to transfer control to a point which is outside the currentsection as any implied EXIT points will be ignored. This means that the program will gowhere it thinks it should go, which is probably NOT where it is supposed to go.

5.7.5 The ALTER statement

DO NOT USE THE ALTER STATEMENT! If you need to change the processingsequence due to a certain condition, then use an alternative set of PERFORM or GOTO statements - trying to debug a program where the instruction as it appears in thelisting may not be the actual instruction encountered by the program at run time is avery frustrating experience. If it makes the maintenance programmer's job more difficultthen DO NOT USE IT!

5.7.6 MOVE CORRESPONDING

This may seem to be an economical way of writing source code as this single statement

will represent several actual MOVE's. However, in the event of an error caused by anillegal ASCII digit (or similar) the diagnostic trace will not be able to identify whichelementary item within the group of items actually generated the fault, which means thatit will take longer to identify and solve the problem.

5.7.7 The COMPUTE statement

The COMPUTE verb should be used only for complex arithmetic operations, and not for simple additions or subtractions. For example, use ADD 1 TO VALUE instead of COMPUTE

 VALUE = VALUE + 1 as it is more efficient in machine resources.

The data items within a COMPUTE statement should, if possible, all have the same datatype. If there is a mixture of COMP, COMP-3 andDISPLAY fields they will be converted (for the duration of the COMPUTE operation) to a common type before the calculation can takeplace.

Be aware that the number of decimal places used in evaluating an arithmetic expressionis determined by the maximum number of decimal places within the expression. Thishas implications when doing any divisions, especially when calculating percentages

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 27/41

which have an implied division by 100. For example, to apply a rate of 17.25% you havethe choice of either:

a. Including the division by 100 within the calculation by specifying * 17.25 / 100, or 

b. Performing the division by 100 separately and specifying * 0.1725 within the calculation.

If none of the items within the calculation has at least 4 decimal places then theintermediate result from (a) above will not be accurate, whereas method (b) avoids thisproblem by ensuring that the result of the division is stored in a field which has thecorrect number of decimal places.

5.7.8 AUTOREADS in VPLUS

It has always been the standard within Hewlett-Packard's block-mode screenprocessing that the user either presses the ENTER key to process the current screen of 

data, or presses one of the other function keys to do something else.The AUTOREAD facility was provided to cater for those special circumstances where itis necessary to capture the current screen contents without the user being able to pressthe ENTER key - for example, when using the HELP subsystem.

This facility was NOT provided so that the user could bypass the ENTER key altogether,and should not be used as such.

5.7.9 The SORT statement

A SORT can be performed in one of the following ways:

a. Internally, using the construct:

b. SORT .... INPUT PROCEDURE IS .....c. OUTPUT PROCEDURE IS ....

d. Internally, using the construct:

e. SORT .... USING <file> GIVING <file> 

f. Externally, by performing the extract/sort/report phases as separate processes.

Method (a) may be the fastest, but it requires the most memory as the datarequirements of the input, sort and output procedures are all made available at thesame time, which can sometimes cause a stack overflow. This construct shouldtherefore be avoided.

Method (b) ensures that the input procedure (used to extract data onto an intermediatefile) is completed before the sort is commenced, and that the sort is completed beforethe output procedure (which processes the sorted file) is commenced. This is slightlyslower than method (a), but reduces the risk of a stack overflow.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 28/41

Method (c) is a further improvement on method (b) insofar as each of the procedures isa separate program. This is useful if either of the input or output procedures isespecially complex as the code is kept entirely separate. If an error occurs then any of the later steps can be rerun without having to reprocess all the earlier steps. If differentoptions are required in the output procedure then they should be provided in separateprograms rather than additional and optional code within a single program.

5.7.10 Defining and using Print Files

Problems can occur with print files if they are not defined correctly within theenvironment division of a COBOL program. An example of the correct definition is asfollows:-

SELECT <internal-name> ASSIGN TO "external-name,,,LP(CCTL)"

The LP(CCTL) suffix causes the file to be created with the default device class of "LP"and with the carriage-control option turned on. Without it the file would be created as a

temporary disk file and would not be printed, and would be lost at the end of the job/session. This has been known to cause the client great confusion when he streamsa job and the expected print file does not appear. The way out of this predicament is toamend the jobstream(s) to include a file equate which changes the file's characteristicsfrom a temporary disk file to a spool file.

The program should use the WRITE <record> AFTER/BEFORE n/TOP-OF-PAGE statementto add records to the print file which will automatically insert the correct carriage-controlinstructions. The practice of defining the file as a serial disk file with an extra character at the beginning to contain the hard-coded carriage-control value should be avoided.

5.7.11 The ACCEPT statement

If a program requires a parameter value before it can continue with its processing it isnormal practice to acquire this from the $STDIN file by means of the ACCEPT

 <dataname> verb.

Before each ACCEPT statement there should be a corresponding DISPLAY statement inorder to identify that some input data is required, and to indicate the format/content thatit should take. For example:

DISPLAY "Enter selection date (DDMMYY): " NO ADVANCING. ACCEPT W01-RUN-DATE.DISPLAY W01-RUN-DATE.

This should prevent the situation where the program waits for input from the user whilethe user waits for instructions from the program.

If the program is liable to be run in a jobstream then it would be a good idea to followthe  ACCEPT <dataname> with a DISPLAY <dataname> . The $STDLIST output will thenshow not only what parameter value that was requested, but also the value that wassupplied.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 29/41

6. Sample Online Procedures

6.1 Introduction

These standardised COBOL procedures use the VPLUS communication area which isdefined in COMAREA in the standard copy library (STANDARD.LIB.STANDARD).

The macro definitions can be found in the macro file STDMACRO.LIB.STANDARD.

In most situations these procedures replace the need to call all those VPLUS intrinsicsindividually - all the common calls are grouped together and processed in the correctsequence, as and when required. The only intrinsics not included are VPUTBUFFERand VGETBUFFER, because they require the address of a data buffer which is uniquefor each different form.

These procedures assume the following programming standards:

a. No appending forms are used, only forms families.

b. Each form will contain a standard heading on the top two lines, to be filled with various dataat run time.

c. The identity of the form to be loaded and processed is hard-coded in each program, andnot taken from any settings in the formsfile. If any form has a value in NFNAME it will beignored.

d. Only those terminals which support function key labels will be used. Function key labels willbe hard-coded, and not taken from any formsfile definition.

e. Function keys 6,7 and 8 have preset values, as follows:

○ f6 = PRINT (to print current form)○ f7 = HELP (see UHELP subroutine)○ f8 = EXIT (terminate current transaction).

f. All other function keys may be used to select different processing options as and whenthey become available - however, a meaningful description must be placed in thecorresponding label area.

g. Any function key WITHOUT a label is deemed to be invalid, and will display an error message if selected.

h. The window line is never blank - it should always contain some sort of instruction or informative message. A default message will be used if none is supplied.

6.2 Standard VPLUS Macros

The following macros are the most frequently used for VPLUS screen processing. Themacros are obtained from file STDMACRO.[NM]LIB, and are documented in the manualentitled Library of Standard COBOL Macros. The subroutines that they call aredocumented in the manual entitled Library of Standard Utilities.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 30/41

%VGETBUFFER(Para#,Buffer#

)

Transfers screen buffer to the program's data buffer.

%VPUTBUFFER(Para#,Buffer#

)

Transfers program's data buffer to the screen buffer.

%SAVINITFORM(Para#,Form#) Loads and initialises a new form.

%ACCEPTENTER(Para#) Displays the current form, window line and function key

labels, then waits for the user to press either the ENTER key

or one of the labelled function keys.

%ACCEPTFUNCTION(Para#) Similar to %ACCEPTENTER, but will reject the ENTER key.

%SCVSETERROR(Para#) Flags field VIEW-FIELD-NAME in error, using the message

in VIEW-WINDOW.

%SDVPUTFIELD(Para#) Updates the value of a single field in the current screen

buffer without affecting the data in any of the other fields.

%BLINKMSG(Para#) Displays a message in the window line with the blink option

set, and locks the keyboard until the next %ACCEPTENTER

or %ACCEPTFUNCTION.

6.3 Examples6.3.1 Sample screen processing

Each screen should be processed separately, as follows:

C-SCREEN-MA01 SECTION.************************************************************* << description goes here....************************************************************C-10-INIT.

%SAVINITFORM(C-10#,MA01#).

C-20-LOAD-BUFFER.

<< There are 2 ways of loading a value into the screen:

either: %VGETBUFFER(C-20#,MA01-BUFFER#).MOVE <value> TO MA01-<item>.%VPUTBUFFER(C-20#,MA01-BUFFER#).

or: MOVE <value> TO VIEW-FIELD-DATA.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 31/41

MOVE <item-name> TO VIEW-FIELD-NAME.%SDVPUTFIELD(C-20#).

<< If you wish to use function keys f1 to f5 it is necessary<< to load a value into the corresponding label:

C-30-ACCEPT.

MOVE "<label>" TO VIEW-LABEL(n).MOVE "OTHER OPTIONS" TO VIEW-LABEL(5).

%ACCEPTENTER(C-30#).

C-40-KEYS.

EVALUATE TRUEWHEN F0 PERFORM CA-ENTER-KEY

IF VEDIT-ERRORSGO TO C-30-ACCEPT

END-IF

WHEN Fn <process function key n> WHEN F5 %USELECT(C-30-ACCEPT#,C-EXIT#)WHEN OTHER GO TO C-EXIT

END-EVALUATE.

C-EXIT.EXIT.

Here is a sample section which is invoked when the user presses the ENTER key. If thedata is valid the user must press another key to add this data to the database, or hemay change any of the data and have it validated again.

CA-ENTER-KEY SECTION.******************************************************************

* ENTER key has been pressed - validate data and process it.******************************************************************CA-10-VALIDATE.

%VGETBUFFER(CA-10#,MA01-BUFFER#).

PERFORM CAA-VALIDATE-MA01.IF VEDIT-ERRORS

GO TO CA-EXIT.

CA-20-CONFIRM.

MOVE "CANCEL" TO VIEW-LABEL(4).

* User confirms this data by pressing function key 5%CONFIRMDATA(CA-20#,5#,CA-10-VALIDATE#).

IF NOT F5MOVE SPACES TO VIEW-LABEL(4)GO TO CA-EXIT.

CA-30-PROCESS.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 32/41

* Processing - please wait%BLINKMSG(CA-30#).

PERFORM CAB-PROCESS-DATA.IF IMAGE-LOCK-FAILURE

GO TO CA-20-CONFIRM END-IF.

CA-EXIT.EXIT.

Here is a sample validation section. Notice that processing stops on the first error,otherwise the contents of the window line may not relate to the field where the cursor ispositioned.

CAA-VALIDATE-MA01 SECTION.CAA-10-ITEM-1.

%DBGET7(CAA-10#,BASE#,SET#,ARG#).

IF IMAGE-NO-ENTRYMOVE "<message number>" TO VIEW-WINDOW MOVE "<item-name>" TO VIEW-FIELD-NAME%SCVSETERROR(CAA-10#)GO TO CAA-EXIT

END-IF.

MOVE <database item> TO MA01-<item>.

CAA-20-ITEM-2.

<< Similar to CAA-10

CAA-99-PUTBUFFER.

%VPUTBUFFER(CAA-99#,MA01-BUFFER#).

CAA-EXIT.EXIT.

6.3.2 Validating screen arrays

Because appending forms are not used there may be occasions when a screencontains an array of items which the program has to validate individually. If one of theseitems is invalid it is important to flag the correct field in error on the screen. Each of 

these fields has to be identified by its actual field number for use bythe VSETERROR intrinsic. However, the standard screen handling routines allow a field tobe identified by its name as the VGETFIELDINFO intrinsic is used to convert this into anumber.

This allows the following code to be used to validate item arrays:

a. Append the occurrence number to each item name in the form definition:eg: ITEM01, ITEM02,...,ITEM13 etc.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 33/41

b. Use a subscript in the format PIC 99.

c. Validate as follows:

d. IF ITEM(subscript) is invalid e. MOVE "<message number>" TO VIEW-WINDOW 

f. STRING "ITEM" <subscript> DELIMITED BY SIZEg. INTO VIEW-FIELD-NAMEh. %SCVSETERROR(Para#)i. GO TO <exit paragraph>.

6.3.3 Handling numeric values

DISPLAYING numeric values:

a. All display-only fields should have their types set to CHAR to avoidcausing VFIELDEDIT errors on values which cannot be amended.

b. The item in the screen buffer should be defined as follows:

c. 01 MA01-BUFFER.d. 03 MA01-HDG PIC X(??).e. 03 MA01-ITEM1 PIC X(??).f. 03 MA01-VALUE1 PIC Z(5)9.99-.g. 03 MA01-VALUE2 PIC Z(4)9.999- BLANK ZERO.h. etc.

i. The procedure division statements should be:

j. MOVE VALUE1 <PIC S9(7)V99 COMP> TO MA01-VALUE1.k. MOVE VALUE2 <PIC S9(6)V999 COMP> TO MA01-VALUE2.

ACCEPTING numeric values:

a. All numeric input fields must have the correct type, eg: NUM2 for two decimalplaces, NUM3 for 3 decimal places, etc. There must also be a FINISH statement to SET thefield to itself in order to strip leading zeros, strip commas, adjust for the correct number of decimal places, and to right-justify the value.

b. The buffer definitions are as above.

c. Use the UNUMBER routines to move the values from the screen buffer to the internalareas, for example:

d. %UNUMBER2(MA01-VALUE1#,VALUE1#).e. %UNUMBER2(MA01-VALUE2#,VALUE2#).

7. Sample Batch Procedures

7.1 Report Printing

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 34/41

Producing batch reports from COBOL programs is not such a tedious task if you followprocedures such as these:

a. Set up the following areas in WORKING-STORAGE:

b. 01 PAGE-COUNT PIC 9(4) COMP VALUE ZERO.

c. 01 MAX-LINES PIC 9(4) COMP VALUE 60.d. 01 LINE-COUNT PIC 9(4) COMP VALUE 99.e. 01 LINE-ADVANCE PIC 9(4) COMP VALUE ZERO.f. 01 LINE-GROUP PIC 9(4) COMP VALUE ZERO.g. 01 PRINT-LINE PIC X(132).

○ PAGE-COUNT gives you a running total of the number of pages produced so far - italways starts at zero.

○ MAX-LINES indicates the maximum number of lines to be used on each page - thisexcludes the top and bottom margins, but includes all page headings. If you ever needto change the page size then this is the only field that needs amending.

○ LINE-COUNT is used to indicate how many lines have actually been used on thecurrent page. It starts at 99 to simulate the condition that the current page is full and thevery next line should start on a new page.

○ LINE-ADVANCE is used to indicate what spacing should be used between the last lineprinted and the current line.

○ LINE-GROUP is used to indicate that you wish to print a number of lines as a group onthe same page, not split over two pages.

○ PRINT-LINE is a temporary storage area used by the standard print module.

h. Set up PROCEDURE DIVISION sections similar to the following:

i. ZA-PRINT-LINE SECTION.j. **************************************************************k. * Standard module to print a line.l. **************************************************************

 m. ZA-10-PAGE-FULL.n.o. * If there is not enough room on the current page for thisp. * line (or group of lines) then start a new page.q. IF (LINE-COUNT + LINE-ADVANCE + LINE-GROUP) > MAX-LINESr. PERFORM ZAA-PRINT-HEADING.s.

t. * Now print the current line.u. WRITE PRINT-REC FROM PRINT-LINE AFTER LINE-ADVANCE. v.w. * Increment LINE-COUNT and reset LINE-GROUPx. ADD LINE-ADVANCE TO LINE-COUNT.y. MOVE ZERO TO LINE-GROUP.z.aa. ZA-EXIT.

  bb. EXIT.cc.$PAGE

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 35/41

dd. ZAA-PRINT-HEADING SECTION.ee.**************************************************************ff.* Standard module to print page headings.gg.**************************************************************hh. ZAA-10-NEW-PAGE.ii.jj. ADD 1 TO PAGE-COUNT.kk. MOVE PAGE-COUNT TO H1-PAGE-NO.ll.

  mm. IF PAGE-COUNT = 1nn.* Do not put a blank page at the start of the fileoo. WRITE PRINT-REC FROM HDG-1 AFTER 0

  pp. ELSEqq. WRITE PRINT-REC FROM HDG-1 AFTER TOP-OF-PAGErr. END-IF.ss.tt. WRITE PRINT-REC FROM HDG-2 AFTER 1.uu. WRITE PRINT-REC FROM HDG-3 AFTER 2.

  vv. WRITE PRINT-REC FROM HDG-4 AFTER 2.ww.

xx. MOVE 6 TO LINE-COUNT. < this is the position after HDG-4 > yy.zz. MOVE 3 TO LINE-ADVANCE. < the 1st detail line will always > aaa. < be 3 lines below the headings > 

 bbb.ccc. ZAA-EXIT.ddd. EXIT.

eee.Use them in the following manner:

○ You don't need to print the headings on the 1st page after opening the print file, this isdone automatically when the first line is printed (this is why LINE-COUNT starts off with avalue of 99).

○ If it is possible for the program to produce a null report (ie: no report lines weregenerated) this can be detected in the termination module by the fact that PAGE-COUNT is still zero. A separate flag is not required.

○ When printing detail lines at the lowest level (with single spacing, for example) use thefollowing commands:

○ MOVE <detail-line> TO PRINT-LINE.○ PERFORM ZA-PRINT-LINE.○ MOVE 1 TO LINE-ADVANCE.

Note that LINE-ADVANCE is set AFTER the call to module ZA - this is to allow for 

those cases where the 1st detail line in the current series does not immediatelyfollow the preceding line (a group heading, for example).

○ In the case where a group heading needs to be printed <n1> lines after the last line,and with <n2> lines before the 1st detail, use the following commands:

○ MOVE <group-heading> TO PRINT-LINE.○ MOVE <n1> TO LINE-ADVANCE.○ PERFORM ZA-PRINT-LINE.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 36/41

○ MOVE <n2> TO LINE-ADVANCE.

○ In those cases where a group heading must start on a fresh page you do not need tocall module ZAA - it is sufficient to set LINE-COUNT to 99 before calling module ZA.

7.2 Control Breaks with sorted files

Here is a structured method to process a sorted file which has three levels of data.There is a separate section for each level (or control break). With this structure it isrelatively easy to handle any number of control breaks.

FILE SECTION.*SD SORT-FILE.

01 SORT-REC.03 SR-LEVEL-1 PIC X(20).03 SR-LEVEL-2 PIC X(10).

03 SR-LEVEL-3 PIC XX.03 SR-AMOUNT PIC S9(7)V99 COMP.*WORKING-STORAGE SECTION.*01 W01-LEVEL-1 PIC X(20).01 W01-LEVEL-1-TOTAL PIC S9(16)V99 COMP.

01 W02-LEVEL-2 PIC X(10).01 W02-LEVEL-2-TOTAL PIC S9(16)V99 COMP.

01 W03-LEVEL-3 PIC XX.01 W03-LEVEL-3-TOTAL PIC S9(16)V99 COMP.

01 W04-REPORT-TOTAL PIC S9(16)V99 COMP.

Note that in the following sample code the section prefix starts with a single letter rather than a group of 4 or 5 letters. This is because the report procedure is usually acompletely separate step (just like the data extract which built the sorted file) and is notreally subordinate to any other processing. The subsequent sections expand this prefixwith the addition of a level number rather than an ever-increasing set of alphabeticcharacters. Thus level 4 has a prefix of F4- rather than FABCD-. Any other sections whichare performed at a particular level should then follow the normal method of appendingalpha characters to the prefix, eg: F2A-, F2B-, etc.

F-REPORT SECTION.**************************************************************

* Report extracted records in LEVEL1/2/3 sequence.**************************************************************F-10-READ-FIRST.

RETURN SORT-FILEAT END MOVE HIGH-VALUES TO SORT-REC.

F-20-PROCESS-LEVEL1.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 37/41

MOVE ZERO TO W04-REPORT-TOTAL.

PERFORM F1-REPORT-LEVEL1UNTIL SORT-REC = HIGH-VALUES. << end of file >> 

F-30-REPORT-END.

<< print report totals >> 

F-EXIT.EXIT.

$PAGEF1-REPORT-LEVEL1 SECTION.*************************************************************** Process change of LEVEL-1.**************************************************************F1-10-START-LEVEL1.

* Start each LEVEL-1 on a new pageMOVE 99 TO LINE-COUNT.

PERFORM F1A-LEVEL-1-START.

F1-20-PROCESS-LEVEL2.

MOVE ZERO TO W01-LEVEL-1-TOTAL.MOVE SR-LEVEL-1 TO W01-LEVEL-1.

PERFORM F2-REPORT-LEVEL2UNTIL SORT-REC = HIGH-VALUES << end of file >> OR SR-LEVEL-1 NOT = W01-LEVEL-1.

F1-30-END-DEPOT.

PERFORM F1B-LEVEL-1-END.

ADD W01-LEVEL-1-TOTAL TO W04-REPORT-TOTAL.

F1-EXIT.EXIT.

$PAGEF2-REPORT-LEVEL2 SECTION.*************************************************************** Process change of LEVEL-2.**************************************************************F2-10-START-LEVEL2.

<< perform processing for start of LEVEL-2 >> 

F2-20-PROCESS-LEVEL3.

MOVE ZERO TO W02-LEVEL-2-TOTAL.MOVE SR-LEVEL-2 TO W02-LEVEL-2.

PERFORM F3-REPORT-LEVEL3UNTIL SORT-REC = HIGH-VALUES << end of file >> OR SR-LEVEL-1 NOT = W01-LEVEL-1OR SR-LEVEL-2 NOT = W02-LEVEL-2.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 38/41

F2-30-END-LEVEL2.

<< perform any processing for end of LEVEL-2 >> 

ADD W02-LEVEL-2-TOTAL TO W01-LEVEL-1-TOTAL.

F2-EXIT.EXIT.

$PAGEF3-REPORT-LEVEL3 SECTION.*************************************************************** Process change of LEVEL-3.**************************************************************F3-10-START-LEVEL3.

<< perform processing for start of LEVEL-3 >> 

F3-20-PROCESS-LEVEL3.

MOVE ZERO TO W03-LEVEL-3-TOTAL.MOVE SR-LEVEL-3 TO W03-LEVEL-3.

PERFORM F4-ACCUMULATE-LEVEL4UNTIL SORT-REC = HIGH-VALUES << end of file >> OR SR-LEVEL-1 NOT = W01-LEVEL-1OR SR-LEVEL-2 NOT = W02-LEVEL-2OR SR-LEVEL-3 NOT = W03-LEVEL-3.

F3-30-END-LEVEL3.

<< perform any processing for end of LEVEL-3 >> 

ADD W03-LEVEL-3-TOTAL TO W02-LEVEL-2-TOTAL.

F3-EXIT.EXIT.

$PAGEF4-ACCUMULATE-LEVEL4 SECTION.*************************************************************** Accumulate all amounts into W03-LEVEL-3-TOTAL.**************************************************************F4-10-ACCUMULATE.

ADD SR-AMOUNT TO W03-LEVEL-3-TOTAL.

F4-20-READ-NEXT.

RETURN SORT-FILE AT ENDMOVE HIGH-VALUES TO SORT-REC.

F4-EXIT.EXIT.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 39/41

8. Programming Hints

8.1 Multi-line Enquiry screens

It is quite common for some enquiry screens to be constructed using repeating data, eg:to list all invoices for a customer, or to list lines within an invoice. In these cases multiple

records are accessed (usually from a detail chain on an IMAGE database), and eachrecord is loaded into one line on the screen. Here are some tips that may prove useful:

FORMSPEC definition

Each detail line on the screen will contain several items of data, and it would seemlogical to define each data item as a separate field on the screen. However, this couldlead to the form containing a very large number of fields, as in the following example:

DOCNO_01 DATE_01. DESCRIPTION_01................ VALUE_01.... VAT_01.DOCNO_02 DATE_02. DESCRIPTION_02................ VALUE_02.... VAT_02.

" " " " "" " " " "

DOCNO_10 DATE_10. DESCRIPTION_10................ VALUE_10.... VAT_10.

An easier option would be to define each line as a single field on the screen and havethe program split each line down into its component parts, as in the following example:

LINE_01..............................................................LINE_02.............................................................." " " " "" " " " "

LINE_10..............................................................

This cuts down the time used to build and maintain the screen as the 10 lines contain just 10 fields instead of 50, and also cuts down the size of the form within the formsfileas there are fewer fields that require start/end delimiters. The only overhead is withinthe program as the definition for each line must now contain the spaces between each

data item.

Overflow data

It has been known that the amount of data available for each entity is too large to fitonto a single 80-column line on the screen. One way around this problem would be totake up two lines for each entity, but this would cut in half the number of entities thatcould be shown on a single screen.

Another option would be to stick to the single line per entity and split the amount of datato be displayed for each entity into two or more sets. By providing the user with aspecial function key he can then "toggle" between the different sets of data. Bychanging the column headings from hard-coded text on the form into a data field it

would then be possible for the program to change the headings to suit the data beingdisplayed.

When the program reads the data for each entity it should load it into an intermediatestorage area and not directly into the screen buffer. There should then be a separatesection to transfer each different set of data from this intermediate area into the screenbuffer depending on the setting of the "toggle" switch.

Paging between screens

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 40/41

As it is only possible to display one screenfull of data at a time it is usual practice tosupply function keys which allow the user to move forwards to the next page or backwards to the previous page. This is easily done when reading from an IMAGEdetail dataset as it is possible to read both forward and backwards, and to reposition atany record within the chain.

Various methods have been used to implement the paging functions, but the mostsuccessful one follows these lines:

a. When reading from the detail chain into a program array additional areas are required tostore the addresses of the first and last records on the current screen.

b. The section which loads the next page should do the following:

○ Reposition the chain pointer (using %DBGET4) to the address of the last record on thecurrent screen.

○ Do a forward chained read (%DBGET5).○ Load details into the screen buffer from the top down.

c. The section which loads the previous page should do the following:

○ Reposition the chain pointer to the address of the first record on the current screen.○ Do a backward chained read (%DBGET6).○ Load details into the screen buffer from the bottom up.

8.2 Multi-line Input/Update screens

Where an input/update function has room for an array of entities on the screen it can bequite complicated to allow the user to access the whole of the array to add, amend or delete different records. One very versatile method is to make the bulk of the array into

display-only fields (similar to the enquiry function in the previous section) and to restrictall modifications to the last line on the screen (known as the "action" line).

Two additional fields will be normally be required on this "action" line - one to identifythe record to be manipulated, and another to identify the mode (Add, Change or Delete).

This means that only one record at a time can me manipulated, but the display-onlyblock above the "action" line would have room to show the details of other records, withfunction key options to load the previous or next page of records as in the enquiryfunction.

The program code to handle each action mode would be as follows:

Add Validate for a new record, add to database, transfer details to next available blank

line in the display area, or scroll the existing details up to make room on the last

display line.

Update Load details of the selected record into the action line, allow the user to make

amendments, then overwrite details in the screen area with the new values.

8/3/2019 Keys and Their Importants--01

http://slidepdf.com/reader/full/keys-and-their-importants-01 41/41

Delete Load details of the selected record into the action line, and if confirmed delete the

record from the database and blank out the corresponding line in the display area.

It would be necessary to store the IMAGE record addresses for all the records in thecurrent screen, as well as the first and last records, as the Update and Delete

procedures would be required to re-read the selected records (using %DBGET4) beforethey could be updated or deleted.

If either of the first/last records in the current screen were deleted the stored recordaddresses used for the next/previous page functions would have to be modified toreflect the actual first/last records after the deletion has taken place, otherwisethe %DBGET4would be given a record address that does not exist, and would cause theprogram to abort.

- END -