Effective coding techniques by asafwewe


More Info
									                               Effective coding techniques

There have always been, and always will be, 'better' and 'worse' ways of coding a COBOL program.
Code may be either difficult to read (terse, containing 'spaghetti logic' or uncommented) or easy to
follow (showing a clear logical flow, and containing comments where appropriate). Programming
standards can ensure that code produced is relatively simple to understand, and therefore, to

It can be far more difficult to code for efficiency, that is, to make sure that a program runs as
quickly as possible. It is surprisingly easy to make a program either more or less efficient, and
without detailed knowledge it is often difficult to determine from the source code whether a
program has been well or poorly written in this respect. This document passes on advice from our
developers on some of the factors that can determine this.

Building the Application
If you have programs that consist of modules that call other modules, perhaps with more than one
top-level program calling the same subroutine, then it is very easy to get multiple copies of code
loaded into memory. Avoid this by building the sub-module as a DLL (on Wintel platforms) or a
shared object (on UNIX platforms). This means that only one copy of this shared subroutine will be

Program Structure
Write your program as a set of PERFORMed sections. The last sentence in the first section should be
a STOP RUN, EXIT PROGRAM or GOBACK. A lot of programs use PERFORMed paragraphs inside just
one section; this is inefficient.

GO TO to pass control to a paragraph within the current section is efficient (though this verb may or
may not be allowed, depending on the programming standards in force). GO TO DEPENDING is fast
and efficient (providing all the target paragraphs are in the current section), but unfortunately can
also result in code that is difficult to follow. Using GO TO to jump to a paragraph outside the current
section is to be avoided.

Data definition
Always try to align your data! This is an important way of increasing efficiency that is hardly ever
appreciated. You should align 2 byte data items on 2 byte boundaries, and 4 byte items on 4 byte
boundaries. Increasingly, it pays to align larger data items on 8 byte boundaries. Make sure that
alphanumeric items are a multiple of 4 or 8 bytes. Misaligned data takes longer to load and store
and may require more instructions. When a misaligned item is an operand and the target of
arithmetic (ie "ADD A TO B") then you get the penalty twice.

When creating tables (OCCURS), if possible make each occurring element a multiple of 4 bytes long
(so put in a filler at the end if necessary). If this is a table on which you are going to SEARCH, make
sure the values are in order (ASCENDING or DESCENDING KEY), so that you can use SEARCH ALL.

If you have numeric fields on which you need to do calculation, do not use the default (PIC 9(n)
DISPLAY). PIC 9(n) COMP is also inefficient on Intel platforms, as the bytes are stored the wrong
way round; use a COMP-5 format instead.

Certain arithmetic verbs are efficient (ADD and SUBTRACT) whereas others are less so (MULTIPLY,
DIVIDE and particularly DIVIDE...REMAINDER), however you may not have much choice here. Make
sure that you avoid unnecessary evaluation if you can. Suppose you have code that says

This will result in the evaluation being done for every WHEN clause. Instead, say

                                              Page 1 of 2
                              Effective coding techniques

Use EVALUATE whenever possible. What would be a multiple IF…ELSE…IF can be handled far more
elegantly by EVALUATE TRUE, and there is no loss of efficiency. (It is also a lot easier to read and

Compiler directives
Using certain directives can make a program more efficient:

NOCHECK turn off runtime checking of things such as subscripts going out of range. If the program
has been written properly, such checking should be unnecessary.

OPT optimises the code. The default is OPT"2"; OPT"3" performs more optimisation, particularly if
the verbs STRING, UNSTRING and/or INSPECT are used in the program. OPT"4" gives further
optimisation on Intel platforms. Both OPT"3" and OPT"4" result in longer compilation times, but as a
program may run thousands of times for each new compilation, this is normally not an issue.

We hope that this article has shown how a little knowledge of how COBOL works can increase the
efficiency of a program, and for very little extra effort at the coding and compilation stages.

                                             Page 2 of 2

To top