Friday, January 29, 2010

COBOL CODING

Good COBOL Coding practices for better Modularity and Performance.

1.

Do not use 66 level. Planning of data definition should make this redundant

2.

Do not use 77 level. Collect all these together and group under a 01 level.

3.

Use 88 level appropriately. For conditional names.

4.

All level numbers increment by an odd number in order to allow for the insertion of extra levels during modifications; this reduces the need for wholesale renumbering of the levels with all the problems that this entails.

01

LEVEL-1.

05

LEVEL-2.

10

LEVEL-3

PIC 9(02) COMP-3 VALUE ZEROES.

5.

PARAGRAPHs or SECTIONs should be used; they should not be mixed except when the PARAGRAPH names are used to subdivide the SECTION. If this is used then only the SECTIONs should be performed.

6.

It corresponds to the sequence/logical position in the structure of the code. This enables the location to be easily ascertained in a listing and also allows the user to see where the program has progressed to in the event of any problems.

7.

The SORT and MERGE statements should be avoided if at all possible, it is more efficient to use an external sort, e.g. SYNCSORT/DFSORT, rather than calling the internal sort which also uses one of these sorts. This is because with an internal sort, control is passed back to the program after each record reducing the speed of the sort. It is also easier to debug a COBOL program than it is to debug the dump from an internal sort abend. If SORT routines are used then the maximum key length is 4092 bytes with no maximum number of keys. To increase the efficiency of the sort, keep the key fields at the start of the record and as contiguous as possible.

8.

GOTO should never be used; a structured program should not need to use this command. If however because of coding or processing constraints, then the only possible use is to transfer control to the end of the SECTION or PARAGRAPH being processed. It MUST NOT be used in any other case.

9.

Nested IF statements should be avoided and consideration should be given to the use of EVALUATE as an alternative. The other option is to restructure the logic of the program so that the if statement is either eliminated or simplified. The WHEN OTHER must always be coded; if this is not present then if none of the prior conditions are satisfied then the program will fall through with no processing occurring. The WHEN OTHER statement should always be the last option; if it is not then all conditions following are irrelevant as they will not be tested.

10.

Care should be taken when using STRING and UNSTRING as this may hide data structures. When using these operations then the ON OVERFLOW clause should always be used; if this is not used then you are not notified of the incomplete operation and control passes to the next sequential statement.

11.

All structures are terminating with END-XXX.

12.

The READ and WRITE PARAGRAPH/SECTIONS should be grouped together

13.

For each file there should be a separate READ PARAGRAPH/SECTION with an ‘AT END’ clause; this should be performed in all cases and the AT-END condition tested for sequential files or the return code. For each file there should be a separate READ section. This is to reduce the size of the load module as the machine instructions generated for the execution of a perform are less than that for a READ; it also means that the file is only read in the one place rather than it scattered throughout the program, easing maintenance.

14.

An initial value of spaces or zero can be given to an item either in the DATA DIVISION or the PROCEDURE DIVISION. In the latter division then there are two options for doing this, to move an initial value to the individual items or to use in COBOL II onwards, the INITIALIZE statement. The use of the VALUE field of the data division should be used to give an initial value to constants.

15.

For variables that are used in working storage the INITIALIZE statement should be used. The INITIALIZE statement is limited in that it will only move spaces (to alphanumeric) or zeros (to numeric) depending on the item definition. All numeric types are supported for this statement, zoned, packed, binary. If any other value is required as an initial value then this must be moved separately, it is still the case that this should be done in the initialization PARAGRAPH or SECTION that is performed prior to processing and at any other time as required.

16.

How the table has been defined in the DATA DIVISION controls the access method used to retrieve the data. If an index has been defined then the method used is ‘SEARCH ALL’ if the data is in key sequence (and BATCH), else use SEARCH; if there are more than 10 entries, and the table will be heavily accessed consider sorting the data; if no index is present then use PERFORM VARYING to retrieve the data.

17.

An index is more efficient than a subscript; this is because a factor of the element size for the table is built into the index, ensuring that the displacement from the start of the table is known for the element and does not have to be calculated at run time as it does with subscripts. This increases the speed at which the elements can be accessed when an index is used. If a table is to be searched sequentially, then if possible put the occurrences that are more likely to satisfy the requirements when a table is checked at the start. If the table is to be searched using a binary search, then the items must be stored in ASCENDING/DESCENDING key sequence.

18.

Static call should be used when size is not an issue but speed is, or the program is called often. Or else use Dynamic call.

19.

The following keywords must not be used:
NEXT SENTENCE
ADD CORR, MOVE CORR
MULTIPLY
GO TO

No comments: