Professional Documents
Culture Documents
Version 1.0
December 2005
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, on any form or by any
means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of HSBC
HOLDINGS PLC.
Developer Handbook Version 1.0
COBOL Coding Standard December 2005
Revision History
Date Version Status Comments
Sep 2005 0.1 Draft Initial draft for internal review.
Nov 2005 0.2 Draft Draft with revisions for comments.
Dec 2005 0.3 Draft Draft with revisions for comments.
Dec 2005 1.0 Final Same as version 0.3 which has been reviewed by ESD
Regional Domain Owners and Software Architecture
Board (SAB) members.
Document Control
Role Name e-mail Telephone
Owner ESD Global Domain Annie L Y LOK/SM IT +852 22881188
APH/HBAP/HSBC
Preparer Jimmy Choi, Alvin Law
Reviewed By
Reviewer Domain Unit / Region Date
Jeff Wolfers Group IT Architecture
Adrian Musson Software Architecture Board
Nancy Wong Group Messaging Framework
Gerald Vaughan Data Architecture
John Flood Enterprise Presentation Services
Debbie Talamo Integration Technologies
Tony Sullivan Conceptual Application Architecture
Louise Galbraith Development Methods & Methodologies
Colin Milton-Haynes Security Architecture
Table of Contents
1. INTRODUCTION .....................................................................................................................1
1.1. OVERVIEW ............................................................................................................................1
1.2. OBJECTIVES AND INTENDED AUDIENCE ................................................................................1
1.3. DOCUMENT STRUCTURE .......................................................................................................1
1.4. RELATED DOCUMENTS .........................................................................................................1
2. CODING STANDARDS ...........................................................................................................2
2.1. STRUCTURE ..........................................................................................................................2
2.1.1. Terminology ...................................................................................................................2
2.1.2. Naming Conventions ......................................................................................................3
2.1.3. Program Structure...........................................................................................................6
2.1.4. Subprogram Structure.....................................................................................................8
2.1.5. Subroutine Structure .....................................................................................................10
2.1.6. Sequence .......................................................................................................................10
2.1.7. Spacing and new page ..................................................................................................14
2.1.8. Indentation ....................................................................................................................14
2.1.9. Use of END-Verbs........................................................................................................15
2.1.10. Position of source code program amendments .............................................................16
2.2. DOCUMENTATION ...............................................................................................................18
2.2.1. Overview.......................................................................................................................18
2.2.2. Program Documentation...............................................................................................18
2.2.3. Subprogram Documentation.........................................................................................22
2.2.4. Subroutine Documentation ...........................................................................................23
2.2.5. Comments in code ........................................................................................................24
2.3. MACRO COPYBOOKS ..........................................................................................................25
2.3.1. Data Macros..................................................................................................................25
2.3.2. Procedure Coding Macros ............................................................................................32
2.4. PROGRAM DIVISIONS CODING RULES .................................................................................33
2.4.1. General Coding Standards ............................................................................................33
2.4.2. Identification Division Coding Rules ...........................................................................34
2.4.3. Environment Division Coding Rules............................................................................35
2.4.4. Data Division Coding Rules .........................................................................................36
2.4.5. Procedure Division Coding Rules ................................................................................39
2.5. PROGRAM INTERFACES .......................................................................................................54
2.5.1. Processing External Data..............................................................................................54
2.5.2. Intercommunication between Programs .......................................................................55
2.5.3. Program Messages ........................................................................................................56
2.6. ERROR HANDLING ..............................................................................................................58
2.7. ONLINE PROGRAMMING CONSIDERATIONS .........................................................................59
2.7.1. General standard ...........................................................................................................59
2.8. CODING FOR PERFORMANCE ...............................................................................................61
2.8.1. General COBOL Program ............................................................................................61
2.8.2. Online COBOL Program ..............................................................................................66
A. REFERENCES ........................................................................................................................67
1. Introduction
1.1. Overview
This document describes the standards and guidelines for COBOL programming. With a set
of global development standards and guidelines for COBOL programming, we expect to
facilitate COBOL program developers to produce high quality, easily maintained and more
reusable COBOL codes that work seamlessly with the GOLD Standard.
This document does not describe any basic COBOL syntax and operation. It also does not
cover the coding practice such as customization/localization, package release & upgrade
methodologies for the GOLD and Evergreen Strategy. Such contents can be found in
COBOL Coding Practice document.
These development standards and guidelines are defined to promote high quality COBOL
programs in terms of:
• Maintainability
• Reusability
• Efficiency, and
• Understandability & readability
Target audiences are project managers, system analysts and programmers who work on
COBOL programming projects.
The document firstly describes proper format of source programs layout in a well-structured,
standardized and straightforward manner. Then, it describes the documentation standard of
macros copybooks and programs. Program coding rules, error handling and coding for
performance are then discussed to provide guidelines to optimize programs for the future
maintenances, developments and reuses.
The COBOL Coding Practice document describes the customization/localization for the
GOLD and Evergreen Strategy. Modular programming and package release & upgrade
methodologies are also discussed to provide guidelines for COBOL developers to work with
the GOLD Standard.
2. Coding Standards
2.1. Structure
2.1.1. Terminology
The terms 'program', 'main program, 'subprogram' and ‘subroutine’/'routine' are used
throughout this section. These terms are defined below.
Program
A program is an entity which can be executed (from a JCL 'EXEC' statement or in
response to the receipt of a CICS screen or task code from a terminal, for example).
In general case, a program comprises a 'main program' and several separately compiled
'subprograms', all of them are bound (link-edited) together.
Main Program
The main program controls overall flow of program logic. In an LE/370 environment, the
first COBOL program loaded into the run unit is the main program.
Subprogram
A subprogram is a separately compiled logical unit of code which is designed to perform
one (or more) functions of the program. A subprogram is invoked with either a static or
dynamic CALL statement. In CICS system, when control is transferred from active
program to an external program, but the program remains active and control can be
returned to it, the program to which the control is transferred is call a subprogram. In CICS
system, the subprogram is invoked with either a static CALL statement, or a dynamic
CALL statement, or a LINK statement. A subprogram is also often called “module”.
Subroutine/Routine
“Subroutine” and “Routine” are commonly used interchangeably. A subroutine is
equivalent to a section or a paragraph in the Procedure Division of a main program or a
subprogram. Each subroutine is designed to perform a distinct processing function (eg,
validate input data, calculate interest amount due, update savings account details, display
an error message).
Example:
SSSD0001 - SSS system daily program 0001
SSSS0001 - SSS system sub-program 0001
SSSNAB01 - SSS system online program AB01
Example:
SSSZZZZG - SSS system coding macro
SSSZZZZR - SSS system coding macro for customization
SSSZZZZW - SSS system working storage macro
SSSZZZZX - SSS system working storage macro for customization
SSSZZZZQ - SSS system working storage file assign statement macro of
working storage SSSZZZZW
SSSZZZZF - SSS system working storage file description macro of working
storage SSSZZZZW
SSSZZZZL - SSS system interface macro used as a parameter in the linkage
section
The following table demonstrates global naming methodology. The table shows the
relationships between sub-program and it’s macro, coding macro and it’s
corresponding macro, file working storage and it’s corresponding macros. Same
XXXXn denotes the same name fragment of the names of related program entities.
The example here contains a daily program that calls a sub-program, and contains a
procedure coding macro. The program contains one working storage and one file
input:
A working SSS4XXXX4W
storage macro
possibly be used
by more than one
program
SSS5XXX5[X]F
(For file
definition,
character [X] is
optional for
denoting
input(I)/Output(O)
An interface SSS6XXXX6L
macro possibly
be used by more
than one program
During the Design phase of the Risk Based Project Management (RBPM), a program is
normally divided into a set of subprograms which are arranged in a hierarchy defining their
logical and execution-time relationships, as illustrated by the structure chart in Figure 2.4.
The main program at the top of the hierarchy (level 1) defines the overall flow of the
program logic. This main program calls subprograms at level 2 to perform certain
functions. These level 2 subprograms in turn call level 3 subprograms, etc.
At the lowest level are various common subprograms which perform widely used basic
functions. These subprograms may be called from anywhere in the hierarchy.
- defines the overall flow of the program logic and contains the major logical
decisions,
- has only one entry point (the first statement in the Procedure Division),
- has only one exit point (the last statement, i.e. GOBACK, in the Procedure Division).
The execution flow between subprograms is restricted to a scheme in which control enters
the called subprogram at its single entry point, and leaves from its one exit point. Control
is always returned to the calling subprogram.
Size of subprogram should restrict to reasonable number of statements and program developer
should not implement too many codes in a subprogram.
Level 1
Level 2
Level 3
In the same way as a program is divided into a number of subprograms, each subprogram
should be further divided into a set of carefully structured routines, with a mainline routine
directing the execution of groups of independent processing routines performing intricate,
peripheral or exception processing.
These routines should also be structured into hierarchies (as illustrated by the subprogram
Hierarchy Chart in Figure 2.5).
The mainline routine at the top of the hierarchy (level 1) defines the overall flow of the
logic. This routine invokes routines at level 2 to perform certain processing. These level 2
routines in turn invoke level 3 routines etc.
At the lowest level are various common routines which perform widely used basic
processing functions (e.g., read a record, display an error message). These routines may be
invoked from anywhere in the hierarchy.
The mainline routine which controls the second-level routines are executed in order. It:
- should be short and simple (usually no more than 50 procedural statements),
- should contain the only normal exit point of the subprogram (i.e., a GOBACK as the
last statement).
The processing routines should be broken down into as many functional levels as
necessary, depending on the complexity of the subprogram. Each routine should:
- contain only one entry point which is, the first statement in the paragraph,
- return to the higher level routine through the only one exit point.
Level 1
MAINLINE-RTNE
Mailine Routine
Level 2
A000-INIT-RTNE B000-PROC-RTNE C000-TERM-RTNE
Processing Routine
A111-INIT- Level 5
CONV-A-RTNE Processing Routine
The paragraph or section must be given a meaningful name which gives an indication of
the function it performs and, if possible, indicates its physical position in the source listing
(in relation to other paragraphs/sections). A suggested format is:
2.1.6. Sequence
An important feature of well-developed program is that the program can be easily, quickly
and efficiently maintained.
Source code is the most important piece of documentation about program. Therefore, it
must be well structured, well documented (refer to section 2.2) and, above all, easy to
understand.
Each main program and subprogram should be laid out in such a way that it is easy to
locate any piece of code.
To increase efficiency:
• the most frequently referenced data items should be placed at the beginning
of Working-Storage
• data names of constant value should be grouped together
• data names whose values vary during execution should also be grouped
together and should be separated from those of constant value.
Within the PROCEDURE DIVISION, the structure of the program must be reflected in the
code. That is, routines (ie, paragraphs) should be ordered in such a way that each routine
follows its associated PERFORM statement.
The common routines, which are PERFORMed by many other routines, should appear
near the end of the source code.
The correct sequence of routines in the source code is illustrated in Figure 2.6.
The mainline routine at the top of the hierarchy (MAINLINE) appears first in the source
code. This is followed by the first level-2 routine invoked (A00-INIT-RTNE), which in
turn is followed by the level-3 routine invoked (A110-INIT-FLD-A-RTNE), etc.
At the end of the source code are the three common routines (Y100-READ-WRITE-
RTNE, Y200-WRITE-REC-RTNE and Y30-EXCPT-RTNE).
For efficiency, all frequently used routines should be grouped together, apart from
infrequently used routines.
HIERARCHICAL STRUCTURE
Level 1
MAINLINE-RTNE
Mailine Routine
Level 2
A000-INIT-RTNE B000-PROC-RTNE C000-TERM-RTNE
Processing Routine
A111-INIT- Level 5
CONV-A-RTNE Processing Routine
Source Code
Either EJECT statement or a slash “/” (which both force page breaks) should be used
liberally to bring out the structure of the program and to make it easier to read. It is
recommended that the EJECT statement or a slash should be defined previous to following
parts:
• Divisions
• File Descriptions (FD) and Sort Descriptions (SD) statements
• Significant groupings in Working-Storage
• Procedure Division routines
Each logical set of instructions should be followed by at least one blank line or one empty
comment.
Comments and procedural code should be followed by at least one blank line or one empty
comment.
Although both EJECT statements or a slash “/” can be used for spacing and new page,
don’t intermix them in a program as it will introduce confusion.
2.1.8. Indentation
01 CUST-REC-WORK.
05 CUST-NUM PIC 9(5).
05 CUST-ADDR.
10 CUST-ADDR-1 PIC X(20).
10 CUST-ADDR-2 PIC X(20).
10 CUST-ADDR-3 PIC X(20).
Clauses and statements in the Procedure Division should be indented to make the logic
easier to follow and to show the levels of nesting as in Figure 2.8.
If A = ITEM-1
If B = ITEM-2
……………
……………
ELSE
……………
END-IF
ELSE
……………
END-IF
• END-ADD
• END-COMPLETE
• END-COMPUTE
• END-DIVIDE
• END-IF
• END-PERFORM
• END-READ
• END-WRITE
:
Among all END-verbs, END-IF is one of the most commonly used. END-IF should be
added after all IF clauses to terminate the scope of IF clause, for example:
IF A = 1
MOVE B TO C
ELSE
MOVE A TO C
END-IF
MOVE D TO E
All five arithmetic verbs include an END-verb scope terminator that can be used to
terminate the range of the [NOT] ON SIZE ERROR conditionals. For example, the END-
COMPUTE in the example below terminates the scope of the NOT ON SIZE ERROR
clause, whereas MOVE A TO D is execute unconditionally.
COMPUTE A = B + C
ON SIZE ERROR
MOVE ZERO to A
END-COMPUTE
MOVE A TO D
COMPUTE A = B + C
ON SIZE ERROR
MOVE ZERO to A (Without/Missing a period ‘.’)
MOVE A TO D
Absence of the END-COMPUTE scope terminator results in the last statement above,
MOVE A TO D, being executed conditionally based on the ON SIZE ERROR condition,
since no period follows the statement MOVE ZERO TO A. This is so despite the deceiving
alignment of the statements in the above program segment.
END-Verbs should be used in place of period “.” as scope terminators when there are
conditional statements (e.g. under IF or [NOT] ON SIZE ERROR) associated with the
program statement.
Column Item
1 Enhancement Identification number (**)
7 Comment symbol (*)
8 Division headers
Section headers
Paragraph headers
FD and SD statements
01 and 77 level numbers
10 05 level number
12 SELECT statementsClauses of FD/SD
statements
01 and 77 data item names
EJECT statements
Coding statements
COPY statements
36 ASSIGN, PIC, REDEFINES, VALUE
41 Comment text (see note below)
73 Enhancement Identification number (**)
Note: Lengthy comments (i.e., those coded at the beginning of a routine) may
start at column 12 rather than column 41.
2.2. Documentation
2.2.1. Overview
• Program Documentation
• Subprogram Documentation
• Subroutine Documentation
• Comments in Code
Note that the presentation of a program is greatly enhanced (and program maintenance is
easier) if comments are coded in lower case.
Programs must be fully documented in form of comment lines (i.e., lines of code
containing an asterisk (*) in column 7) at the beginning of main programs. The
information to be included in program documentation (most of which can be extracted
from program specification) is illustrated in Figure 2.10.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
********************************* Top of Data **********************************
IDENTIFICATION DIVISION.
PROGRAM-ID. program-name
DATE-WRITTEN. dd/mm/ccyy
DATE-COMPILED.
*REMARKS.
*
* ABSTRACT
*
* description of the program's main functions
*
EJECT
*
*
* AMENDMENT HISTORY
*
* for each amendment made to the program (chronological
* sequence):
*
* DATE dd/mm/ccyy (amendment date)
*
* PPCR nnnnn (PPCR number)
*
* PROGRAMMER name of programmer making amendment
*
* ABSTRACT brief description of amendment
*
EJECT
*
* OPERATING INSTRUCTIONS
*
* information the operator needs to run the program
*
* 1. CONTROL CARDS
*
* JCL statements required for normal operation, plus
* parameter details (values and meanings), if required
*
* 2. FILES
*
* for each file: ddname, usage, source/destination of data,
* file organization, record types,
* dependencies
* OPTIONAL
* INPUT FILES
* input file 1
* ......
*
* INTERMEDIATE FILES
* intermediate file 1
* ......
*
* OUTPUT FILES
* output file 1
* ......
*
* 3. CONTINGENCY SETUP
*
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-370.
OBJECT-COMPUTER. IBM-370.
SPECIAL-NAMES.
C01 IS SKIP-TO-NEW-PAGE
CONSOLE IS LOG.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
COPY ddname1Q.
COPY ddname2Q.
.
.
.
EJECT
DATA DIVISION.
FILE SECTION.
*
* function of file1
*
COPY ddname1F.
*
* function of file2
*
COPY ddname2F.
.
.
EJECT
WORKING-STORAGE SECTION.
EJECT
PROCEDURE DIVISION.
MAINLINE SECTION.
.
.
.
.
EJECT
.
.
.
.
EJECT
.
.
In addition to provide a description of the subprogram’s function, the entry and exit
conditions should be documented as follows:
ENTRY CONDITIONS - Description of all conditions that must be in effect at the time
when the subprogram receives control. For example:
- Calling sequence
- Parameters passed
- Files that must be open
- Common areas that must be set up.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
IDENTIFICATION DIVISION.
PROGRAM-ID. subprogram-name.
DATE-WRITTEN. dd/mm/ccyy
DATE-COMPILED.
* REMARKS
*
* ABSTRACT
*
* FUNCTION: A description of what the subprogram does.
*
* ENTRY CONDITIONS: Details of how to invoke the subprogram,
* plus a description of all conditions which
* must pertain at the time it is called,
* for example:
*
* - calling sequence
* - parameters passed
* - flags which must be set
* - files that must be open
* - common areas that must be set up
*
* EXIT CONDITIONS : A description of everything external to
* the subprogram which may be changed as a
* result of executing it, for example:
*
* - entry parameters altered
* - return codes set
* - files altered
* - common areas changed
EJECT
Documentation should be provided in the form of comment lines at the beginning of each
routine (i.e., paragraph) in the program. The information to be included is illustrated
below.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
EJECT
subname-RTNE.
*
* FUNCTION - Details of what the routine does
* How it is called
* Parameters passed
* Anything unusual about the routine
* Return codes set, etc.
*
.
. subroutine coding
.
Use comments extensively to explain the processing and purpose of sections of code.
All sections of code should be easy to understand. COBOL allows variable names and
section names of up to thirty characters in length, so these can be made reasonably
“standard English”. If there is any possibility of confusion then a comment block must be
included to make things clearer.
One blank line or one empty comment line should be used to separate the comments
imbedded in code from the instructions. For example:
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* If account has been inactive for more than 24 months, *
* write details to Statistics File *
IF WS-ACCOUNT INACTIVE
PERFORM C111-WRITE-STAT-RTNE
THRU C111-WRITE-STAT-EXIT
END-IF.
01 level - mmmmmmm-WORK
05 level - mmmmmmm-WORK-REC-xx
other levels - mmmmmmm-a....a
where:
mmmmmmm -macro name minus final character. (For the macro naming
conventions, please refer to COBOL Coding Practice in
details)
Figure 2.14 Use abbreviations defined in Group Metadata Repository (GMR) Glossary
Note: The ddname of alternate index is xxxxxxxxn, and the format of an alternate path
is a …. An, where a…..a is the full dataset name and n is the path number.
1. Without Key
FD mmmmmmm
RECORD CONTAINS …
BLOCK CONTAINS x RECORDS
LABEL RECORDS …
DATA RECORD IS …
*
* comments
*
01 mmmmmmm-REC-zzz-nn.
2. With Key
FD mmmmmmm
RECORD CONTAINS …
BLOCK CONTAINS x RECORDS
LABEL RECORDS …
DATA RECORD IS …
*
* comments
*
01 mmmmmmm-REC-zzz-nn.
Note: The format of a data item name containing the number of times a record is
repeated in an ‘OCCURS .. DEPENDING ON ..’ clause (if present) is mmmmmmm-LNG-
nn.
Data Macros Containing Data Structure Definition Statements (Records and Common Areas)
*
* comments
*
05 mmmmmmm-WORK-REC-nn.
*
* comments
*
10 mmmmmmm-a….a.
R
15 mmmmmmm-a….a. PIC …
15 mmmmmmm-a….a. PIC …
:
:
*
* comments
*
05 mmmmmmm-WORK-REC-nn REDEFINES
KEY:
mmmmmmm - macro name minus last character (i.e., the W). (For the naming
conventions, please refer to COBOL Coding Practice in details)
Using procedure coding macro is a means to allow code customization on global / vendor
base code. The global / vendor base code can specify the inclusion of a piece of coding to
be provided by customer codes by copying a coding macro. This retains the base code
version unchanged while the processing logics can be tailored to suit the customization
need by different pieces of codes. For details on program change management, please
refer to Coding Practice section.
When implemented by the business units, it provides a quick and efficient way to move
profitable localized enhancements to the Global level and distribute globally.
In the procedure coding macros, only single function or similar functions paragraphs
should be covered in one procedure coding macro.
• IBM-only features
The IBM COBOL compiler is fully compatible with the highest level of American
National Standards Institute (ANSI) COBOL. It also includes a number of IBM
extensions, some of which represent features not approved by ANSI COBOL.
In order to confine the use of COBOL to ANSI standard, normally, only those
IBM extensions that appear in the following list may be included in programs.
The use of any other IBM-only feature must have the approval of the Project
Manager.
• USAGE IS COMP-3
• CONSOLE IS LOG
• ENTRY literal-1 [USING identifier-1 identifier-2 .....]
• EXIT PROGRAM
• GOBACK
• EXHIBIT
• READY TRACE
• RESET TRACE
• EJECT
• *
• CURRENT-DATE
• UPSI-n
• SELECT filename COPY statement
As mentioned in the Documentation section, the following must be present in the order
given, separated by blank lines or empty comment lines.
• PROGRAM-ID
• Copyright statement.
• AUTHOR
• INSTALLATION
• DATE-WRITTEN
• DATE-COMPILED
• Copyright statement
Include the macro 'COPYRGTC' after the program name to generate the
'Copyright' clause. The format of the 'COPY' statement and the generated
clause is shown as below:
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
IDENTIFICATION DIVISION.
PROGRAM-ID progname
• Remark Section
Document the program at the end of Identification Division in the Remarks section
(group of comment lines started with “*REMARKS”), as described and illustrated
earlier in the section.
• Division layout
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
OBJECT-COMPUTER. objectname.
SOURCE-COMPUTER. sourcename.
SPECIAL-NAMES.
C01 IS SKIP-TO-NEW-PAGE.
CONSOLE IS LOG.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
COPY ddname1Q.
COPY ddname2Q.
Note:
1. Make sure the division header is spelt correctly and without hyphen, the
necessary periods are included, and the hyphens are in the correct place in the
other headers and keywords.
• Special Names
Define the following mnemonic-names in the Special-Names paragraph and use them
throughout program, in order to provide better documentation, in place of the
corresponding IBM function-names:
Function-name Mnemonic-name
C01 IS SKIP-TO-NEW-PAGE
CONSOLE IS LOG
• Input/Output
If asked to set up macro (for a new file), follow the instructions presented earlier in
this section. Two I/O areas should be used for every dataset defined.
Either code RESERVE 1 ALTERNATE AREA or omit the RESERVE clause.
• Division layout
The Data Division should be coded in the format shown below:
DATA DIVISION.
FILE SECTION.
* function of file1
COPY ddname1F.
* function of file1
COPY ddname2F.
EJECT
WORKING-STORAGE SECTION.
*01 recname1.
COPY macname1.
*01 recname2.
COPY macname2.
.
.
.
77 FILLER PIC X(30) VALUE
'END OF WORKING STORAGE SECTION'.
EJECT
LINKAGE SECTION.
.
Figure 2.21 Example of data division
Note:
1. Make sure the division header is spelt correctly and without hyphen, the
required periods are included, the hyphen is in a correct place in 'WORKING-
STORAGE'.
2. Each data item definition statement must be terminated by a period.
• File Section
a. Code a level 77 data item, with the value indicated below, as the first entry in the
Working-Storage section:
Also code a level 77 data item, with the value given below, at the very end of the
Working-Storage section:
These two items will help you to find the beginning and end of Working-Storage
in an MVS memory dump.
b. For efficiency:
- place the most frequently referenced data items at the beginning of the section
- group data names of constant value together, and apart from those whose
values vary during execution.
Give each item a meaningful name that conforms to the standards laid down for
data item names.
Assign date related data items a name according to the standards for data items
defined in data macros (these are described in Section 2.3.1).
05 WS-MARITAL-STATUS PIC 9.
88 WS-MARITAL-MARRIED VALUE 1.
88 WS-MARITAL-SINGLE VALUE 2.
88 WS-MARITAL-DIVORCED VALUE 3.
88 WS-MARITAL-WIDOWED VALUE 4.
The type of literal in a condition entry must be consistent with the data type of
the conditional variable, otherwise it will be flagged in error. In the above
example, if WS-MARITAL-STATUS is changed to PIC X, then all 88-level
condition entries must be changed to alphanumeric literals ("1","2", "3", "4").
f. Initialize all data items using the VALUE clause. The VALUE clause defines the
initial state of a program only. After sub-program is called from the main
program, data value should be setup by using SET clause or MOVE clause to
make sure a latest value is maintained.
g. Use the 'COPY' statement to obtain pre-written record description (data structure
definition) statements from a macro in library. Do not hardcode these statements
in programs.
For example:
01 “prefix”-REC
05 “prefix”-REC-TYPE PIC X.
88 “prefix”-REC-HEADER VALUE ‘H’.
88 “prefix”-REC-FOOTER VALUE ‘F’.
05 “prefix”-REC-NO PIC 9(3).
If a file B’s layout is the same as file A, then the macro can be re-used by using
the copy replacing method to replace “prefix” by FILEB.
Mainline Routine
Controls second-level routines in order. It should:
- be short and simple (usually no more than 50 statements)
- define overall flow of program logic and contain major logical decisions
- invoke all second-level processing routines by PERFORM statements.
Processing Routines
These routines should be broken down into as many functional levels as necessary,
depending on complexity of program. Each routine should:
- have a meaningful name
- contain only one entry point which is the first statement in the routine
- contain only one exit point, which should be the last statement in the routine
- perform a distinct function
- invoke only lower level routines
- return to higher level routine after the last statement in the routine
For performance reasons, all frequently used routines should be placed near the
routines that use them.
Common Routines
These routines should be the lowest level routines since all higher level routines have
access to them. Frequently executed routines (e.g., input and output routines) should
be grouped together.
There should be only one functional read or write routine for each file, which is
frequently accessed.
Do not use too many trivial routines that contains only one or two lines as they:
- increase the size of programs (several bytes used each time a new routine is
invoked)
- make program logic difficult to follow.
It is far better to code several small routines that are functionally related together,
e.g., one routine to open files and check status.
In either case, each routine must have one entry point and one exit point with names
in following format:
a. Entry Point
sname-RTNE - subroutine
sname SECTION - section
b. Exit Point
where:
xxxx - is a prefix in form of one alphabetic character followed by a number (e.g.,
C100). Subroutines should be coded in prefix order so that they can be
located easily in program listing.
The above naming conventions are illustrated in Figure 2.23 to Figure 2.24.
MAINLINE-RTNE Level 1
A110-INIT- A120-INIT-
Level 4
FLD-A-RTNE FLD-B-RTNE
A111-INIT-
Level 5
CONV-A-RTNE
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
PROCEDURE DIVISION.
MAINLINE SECTION.
PERFORM A000--INIT-RTNE
THRU A000-INIT-EXIT.
PERFORM B000-PROC-RTNE
THRU B000-PROC-EXIT.
PERFORM C000-TERM-RTNE
THRU C000-TERM-EXIT.
MAINLINE-EXIT.
GOBACK.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - INITIALIZATION ROUTINE
* REMARKS - OPEN FILES A, B
*
A000-INIT-RTNE.
PERFORM A100-INIT-TABLE-RTNE
THRU A100-INIT-TABLE-EXIT.
PERFORM A200-READ-FILE-RTNE
THRU A200-READ-FILE-EXIT.
A000-INIT-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - INITIALIZATION TABLE ROUTINE
* REMARKS - INITIALIZE TABLES USED IN THE PROGRAM
*
A100-INIT-TABLE-RTNE.
PERFORM A110-INIT-FLD-A-RTNE
THRU A110-INIT-FLD-A-EXIT.
PERFORM A120-INIT-FLD-B-RTNE
THRU A120-INIT-FLD-B-EXIT.
A100-INIT-TABLE-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - INITIALIZATION FIELDS A ROUTINE
* REMARKS - INITIALIZE ALL FIELDS USED FOR FILE A
*
A110-INIT-FLD-A-RTNE.
.
.
PERFORM A110-INIT-CONV-A-RTNE
THRU A110-INIT-CONV-A-EXIT.
.
.
A110-INIT-FLD-A-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - INITIALIZATION CONVERTED FIELDS A ROUTINE
* REMARKS - INITIALIZE ALL CONVERTED FIELDS USED FOR FILE A
*
A111-INIT-CONV-A-RTNE.
.
.
.
A111-INIT-CONV-A-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - INITIALIZATION FIELDS B ROUTINE
* REMARKS - INITIALIZE ALL FIELDS USED FOR FILE B
*
A120-INIT-FLD-B-RTNE.
.
.
.
A120-INIT-FLD-B-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - READ FILE ROUTINE
* REMARKS - READ FILES
*
A200-READ-FILE-RTNE.
.
.
.
A200-READ-FILE-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - PROCESS ROUTINE
* REMARKS - PROCESSING DATA
*
B000-PROC-RTNE.
...
PERFORM B100-CALC-RTNE
THRU B100-CALC-EXIT.
...
B000-PROC-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - CALCULATION ROUTINE
* REMARKS - DO CALCUATION FOR THE DATA
*
B100-CALC-RTNE.
.
.
.
B100-CALC-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - TERMINATION ROUTINE
* REMARKS - HANDLE THE ACTION BEFORE PROGRAM TERMINATE
*
C000-TERM-RTNE.
...
PERFORM C100-FORMT-MSG-RTNE
THRU C100-FORMT-MSG-EXIT.
...
C000-TERM-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - FORMAT MESSAGE ROUTINE
* REMARKS - FORMAT MESSAGE USED IN THE PROGRAM
*
C100-FORMT-MSG-RTNE.
.
.
.
C100-FORMT-MSG-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - READ RECORD ROUTINE
* REMARKS - READ RECORDS FROM TARGET FILE TO SAVE AREA
*
Y100-READ-REC-RTNE.
.
.
.
Y100-READ-REC-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - WRITE RECORD ROUTINE
* REMARKS - WRITE RECORDS FROM SAVE AREA TO TARGET FILE
*
Y200-WRITE-REC-RTNE.
.
.
.
Y200-WRITE-REC-EXIT.
EXIT.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
* SUBROUTINE - EXCEPTIONAL ROUTINE
* REMARKS - HANDLE PROGRAM EXCEPTIONAL SITUATION
*
Y300-EXCPT-RTNE.
.
.
.
Y300-EXCPT-EXIT.
EXIT.
There should be one OPEN file routine and one CLOSE file routine for a program
and, where possible, all files should be opened in one statement and closed in one
statement.
Opening (closing) more than one file in same statement is faster than using a separate
statement for each.
All routines must be invoked by the PERFORM statement. The following rules must
be adhered to:
For example:
B000-PROC-RTNE.
.
.
.
PERFORM B100-CALC-RTNE
THRU B100-CALC-EXIT.
.
.
.
B000-PROC-EXIT.
EXIT.
- Perform initial read of all input files in initialization routine. Check for empty
files and missing header records condition before entering the processing
routine.
1. 'IF' statements
Do not use more than three levels of nested IF statements. Use EVALUATE
instead for conditional determination. For example:
EVALUATE variable-a
WHEN 'A'
PERFORM C111-A-RTNE
THRU C111-A-EXIT
WHEN 'B'
PERFORM C112-B-RTNE
THRU C112-B-EXIT
WHEN 'C'
PERFORM C113-C-RTNE
THRU C113-C-EXIT
WHEN 'D'
PERFORM C114-D-RTNE
THRU C114-D-EXIT
WHEN OTHER
PERFORM C115-E-RTNE
THRU C115-E-EXIT
END-EVALUATE
2. Comparison statement
For program clarity and readability, the comparison symbols ‘>’ ‘<’, ‘>=’,
‘<=’, ‘=’ are more preferable to corresponding full words comparison
operators GREATER THAN, LESS THAN, NOT LESS THAN, etc.
3. Logical statement
Incorrect usage:
IF NOT MARITIAL-STATUS-NOT-MARRIED
ADD 1 TO MARRIED-CNT
END-IF
Correct usage:
IF MARITIAL-STATUS-MARRIED
ADD 1 TO MARRIED-CNT
END-IF
where
05 MARITAL-STATUS PIC X(1).
88 MARITIAL-STATUS-MARRIED VALUE '0'.
Note: Assume only two status values defined for marital status.
Minimize number of use of 'GO TO' statement, by using the 'ELSE' clause to
express alternative actions. For example:
READ-RTNE.
:
:
IF condition-1
action-1
GO TO READ-EXIT
END-IF.
action-2.
READ-EXIT.
EXIT.
READ-RTNE.
:
:
IF condition-1
action-1
ELSE
action-2
END-IF.
:
:
READ-EXIT.
EXIT.
Never use 'GO TO' to pass control to a statement outside the subroutine (or
section).
Use indexing for table searching. The use of index-names in referencing tables is
more efficient than the use of subscripts. The use of direct indexes is faster than the
use of relative indexes.
IF WT-COUNT(1) = WC-MAXIMUM
not
SET WI-INDEX TO 1
IF WT-COUNT(WI-INDEX) = WC-MAXIMUM
not
IF WT-COUNT(WA-VALUE) = WC-MAXIMUM
Use SEARCH if table has 50 or fewer entries. If the table is sorted and contains over
50 entries, SEARCH ALL may be faster. Always put the most frequently hit entries
at the beginning of a table with any size, if possible, use the SEARCH statement.
2.4.5.10. Moves
If a display field is to be moved to two packed fields, the following should be coded:
MOVE UNPACK1 TO PACK1
MOVE PACK1 TO PACK2
2.4.5.11. EVALUATE
An EVALUATE should be considered if the construct would require more than three
IF statements.
EVALUATE statement should be structured so that the most likely event occurs first.
For example:
PERFORM C113-C-RTNE
THRU C113-C-EXIT
WHEN OTHER
PERFORM C114-D-RTNE
THRU C114-D-EXIT
END-EVALUATE
2.4.5.12. STRING/UNSTRING
Both are time-consuming verb because it invokes a complex subroutine. You may
use a character-by-character MOVE loop to replace STRING while it is more
efficient.
Use literals in Procedure Division only when the value of the literal is fixed and
guaranteed never to change. For example: 1 to increment a count, 12 to represent the
number of months in a year, 'Y' and 'N' to represent 'yes' and 'no'.
Use the COMPUTE statement for all arithmetic calculations involving more than 2
data items/literals.
The COMPUTE statement generates more efficient coding than using individual
arithmetic statements, because the compiler can keep track of internal work areas and
does not have to store the results of intermediate calculations.
2. In the program termination, display a message with input and output record
counts. This message should be an “information only” message. The message
format can be referred to section 2.5.3
• DB2
COBOL/370 supports processing of QSAM and VSAM files only (it does not
support ISAM and BDAM files).
QSAM
With COBOL/370, file record length for a QSAM file must match that specified in
the program.
Mismatches in attributes for file organization, record format (fixed or variable), code
set, or record length cause the OPEN statement to fail with a file status code of 39.
VSAM
With COBOL/370, RECORDSIZE defined in VSAM files associated with IDCAMS
must match with the program.
For all files, status key must be checked after each OPEN statement.
Use 'READ INTO' option to access a record in input buffer and move it to a
Working-Storage area, process the record in Working-Storage, use the 'WRITE
FROM' option to move the record from the Working-Storage area to output buffer
before releasing it to output file.
• Closing Files
All files must be closed before the program is terminated. The file status key must be
checked after each CLOSE statement.
This subsection describes the methods that enable inter-program communication between
separately compiled programs.
The program that calls another program is referred to as 'calling program' and the
program it calls is referred to as 'called program'.
• Calling Subprograms
Any of the following methods can be used to transfer control from one COBOL/370
program to another COBOL/370 program:
- Static calls
- Dynamic calls
- Calls to nested programs
Static and dynamic calls can also be made between COBOL/370 and VS COBOL II
programs. These calls can also be made between COBOL/370 and OS/VS COBOL
programs, in a non-CICS environment. In a CICS environment, EXEC CICS LINK
must be used to transfer control between COBOL/370 and OS/VS COBOL
programs.
A static call is used to invoke a separately compiled program that is link edited into
the same load module as calling program.
A dynamic call is used to invoke a separately compiled program that has been link
edited into a separate load module from calling program. The subprogram module is
loaded into storage at the first time it is called.
For more details, please refer to COBOL Coding Practice section 2.3.
• Message destinations
Program generated messages may also be routed to one, or both, of the following
destinations:
• LOG (System Console Log) must only be used for urgent messages which are
really necessary and which require immediate action from operator.
• Message format
• Console messages
Only urgent messages which require immediate action from the operator may be
output to operator’s console.
• Message control
Large volume of messages should be avoided. Message counter and logic should be
implemented to avoid possible system deterioration caused by large volume of
messages.
Use debugging statements only in test version of program. Remove these statements before
releasing the program to production.
When an unrecoverable application error condition is hit in batch processing that should not
be continued to run in a normal condition, e.g. empty file input on a master account file, this
is an indication that some investigation is required before the program can proceed. In this
case, the program should be abended (abnormal end) with an appropriate error condition
code (e.g. COND CODE 16) to alert the operators to investigate or call the programmers for
investigation. Appropriate error messages (refer to section 2.5.3) can be issued to provide
additional information to facilitate problem investigations.
2. Use command level coding only. Always use CICS Command Level coding in your
CICS program. Below example is a way to call up a READ file request:
EXEC CICS
READ FILE(‘FILEA’)
INTO(FILEA-WORK-REC)
RIDFLD(FILEA-WORK-REC-KEY)
EQUAL
END-EXEC
3. Use operating system services with extremely care. Directly issuing an operating
system service request may cause all CICS activities to stop and all transactions to
suffer response delays.
5. Prevent deadlocks, which can occur if two transactions attempt to update the same
resources by ensuring both transactions update the resources in the same order. If
each transaction updates two files, ‘A’ and ‘B’ for example, update file ‘A’ before
‘B’ is the updating order. Both transactions should follow this updating order.
8. Issue only one physical SEND per screen. It is very inefficient to build a screen in
parts and send each part with a separate command.
9. Use ‘eye-catcher’ fields at the beginning and the end of the CWA and TWA, to help
identify the start and end of these areas in a partition dump.
10. Task initiated by CICS START command can execute simultaneously with started
task. In contrast, programs invoked by LINK run as a part of a single task.
1. Arithmetic expressions
• Declare integer items as COMP-3 with nine or fewer digits to afford quick
conversion to floating-point data.
• Define COMP-3 with odd number of digits (For details, please refer to
Performance Tuning section of the IBM COBOL manual in Appendix A
Reference item [2])
• Use COMP in subscripts. COMP is the best format for items used as
subscripts, in GO TO … DEPENDING, or as OCCURS … DEPENDING
objects.
• Use same numeric usage in moves, compares and arithmetic calculations and
same number of decimals for “ADD” and “SUBTRACT”.
Try to avoid repeated system calls if such calls can be replaced by a single call. For
example:
Instead, define a working data field in the same format as CURRENT-DATE and use
a single “MOVE” statement to obtain the date value.
4. EVALUATE
5. I/O operation
Use single OPEN instead of separate OPEN statements in COBOL program. OPEN
is a very time-consuming operation and should be executed only once or a very few
times. Use one OPEN statement to open files that are used together.
Pay close attention to table-handling operations, especially if they are a major part of
an application. Several techniques can help to improve efficiency of these operations
and can also influence the optimizer. The return for your efforts can be significant.
The following two guidelines affect your choice of how to refer to table elements:
original reference to a table element is more efficient with indexes than with
subscripts, even if subscripts are COMP field. The value of an index has the
element size which is factored into it, whereas the value of a subscript must
be multiplied by the element size when the subscript is used. The index
already contains the displacement from the start of table, and this value does
not have to be calculated at run time. However, subscripting might be easier
to understand and maintain.
Whether you use indexes or subscripts, the following guidelines can help you to get
better performance in terms of how you code them:
When you index or subscript tables, it is most efficient if all the tables have
the same element length. With equal element lengths, the stride for the last
dimension of the tables will be equal. The optimizer can then reuse the
rightmost index or subscript computed for one table. If both the element
lengths and the number of occurrences in each dimension are equal, then the
strides for dimensions other than the last one are also equal, resulting in
greater commonality between their subscript computations. The optimizer can
then reuse indexes or subscripts other than the rightmost.
• Avoid errors in references by coding index and subscript checks into your
program.
If you need to validate your indexes and subscripts, it might be faster to code
your own checks in your COBOL program than to use the SSRANGE
compiler option.
You can also improve the efficiency of tables in situations covered by the following
guidelines:
When you use subscripts to address a table, use a COMP signed data item
with eight or fewer digits. In some cases, using four or fewer digits for the
data item might also improve processing time.
For tables with variable-length items, you can improve the code for
OCCURS DEPENDING ON (ODO). To avoid unnecessary conversions
each time the variable-length items are referenced, specify COMP for
OCCURS . . . DEPENDING ON objects.
If the table is searched sequentially, put data values most likely to satisfy the
search criteria at the beginning of the table. If the table is searched using a
binary search algorithm, put the data values in the table sorted alphabetically
on the search key field.
• the most frequently referenced data items should be placed at the beginning
of Working-Storage. This helps to minimize paging.
For efficiency, all frequently used routines should be grouped together, apart from
infrequently used routines.
11. INITIALIZE
INITIALIZE on a group item should be used with caution. It can cause a large
processor overhead. Consideration should be given to other forms of initialising the
data such as:
If A-REC had 100 items, 50 numeric and 50 alpha then the above example would
generate 51 individual moves whereas an initialize of the whole group would
generate 100 moves.
Please note that INITIALIZE does not initialize the FILLER fields within the record.
Try to use VALUE or MOVE clause instead to INITIALIZE to setup the initial value
of data items.
If possible, perform delta add/change record processing rather than full volume while
I/O can be minimized. In addition, when performing lookup routines for large
volume of data, try to save the lookup key and used this to compare with next record
key. I/O will be saved while the lookup key is the same since the saved working
storage lookup value can be re-used.
If possible, use Syncsort sorts rather COBOL sorts as Syncsort sorts are parameter-
driven and easy for future maintenance.
Online COBOL programs share the same performance coding tips with general COBOL
programs together with batch programs. In addition, main performance characteristics to
be considered when designing an online COBOL system is listed below.
• TS Queues
TS Queues are used as main storage for short lived data. There is also an auxiliary
storage queues for long lived or large volume data for fast access.
More detailed discussions on tuning the use of TS can be found in the IBM CICS
Performance Guide (Appendix A Reference item [8]).
• CICS START/LINK
Task initiated by CICS START command can execute simultaneously with started
task. In contrast, programs invoked by LINK run as a part of a single task.
Allocations that specify a number of storage areas should be set to fill all pages used.
Storage size allocations should be defined at a size that optimizes page usage.
Allocation of storage areas that are exactly divisible by page size will completely fill
pages.
A. References
[1] IT Handbook: Volume 2, section 11, 16, 41, 46 and 51, APHIT
[2] IBM COBOL for MVS &VM Version 1 Release 2 – Performance Tuning
[3] CDU COE Local Procedures
[4] ADVANCED COBOL, McGRAW-HILL
[5] STRUCTURED COBOL PROGRAMMING, Fifth edition, JOHN WILEY & SONS, Inc.
[6] THE NEW COBOL, An Illustrated Guide, McGRAW-HILL, 1987, by A.S. Philippakis;
Leonard J. Kazmier
[7] Optimization Techniques by Computer Associates
[8] IBM CICS Performance Guide:
http://publibfp.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/dfht3a04/CCONTENTS