Skip to content

Cobol Coding Styles

Dave Nicolette edited this page Jan 30, 2021 · 7 revisions

Home -> Developer Guide -> Implementation Notes ->

Cobol's source coding rules and developers' personal preferences have changed over the years. Cobol Check cannot have an "opinion" about any of these differences in coding style. It cannot overlook any tokens when parsing a source program because of such differences in style.

When you make changes to Cobol Check, please take care to ensure the new and previously-working functionality handles all the various coding styles that may be found in an enterprise environment.

Some of the stylistic differences for Procedure Division code are illustrated below:

Style 1: All capital letters and a period after each sentence

This is the original style of Cobol source code. In early versions of Cobol, lower-case letters were not supported, and a period was mandatory as the statement terminator in the Procedure Division.

    CALC-EXT-PRICE.
        MULTIPLY UNIT-PRICE(LINE-ITEM) 
            BY QUANTITY(LINE-ITEM) 
            GIVING EXTENDED-PRICE(LINE-ITEM).
        MOVE LINE-ITEM-DATA(LINE-ITEM) TO OUT-LINE-ITEM.
        IF LINE-ITEM IS GREATER THAN MAX-LINE-ITEMS
            WRITE OUTPUT-FILE FROM OUTPUT-RECORD.
    NEXT-PARAGRAPH.
        . . . 

Style 2: All capital letters with periods optional.

At one point in Cobol's development, the requirement to terminate each sentence with a period was lifted. Compilers still support the original style, but they also allow a style in which a period is required only at the end of each paragraph and each section (if sections are used). When the end of a statement might be ambiguous, explicit END-xxx keywords are available.

Some organizations adopted this standard for new code while leaving existing programs alone. In other organizations, developers have been free to code in any style, and they have different individual preferences. In general the Cobol code that Cobol Check generates follows this style, although this may change as we continue developing the tool. It is not necessary to mimic the coding style of the program into which Cobol Check inserts test code. This is not a compiler setting, but a coding convention.

    CALC-EXT-PRICE.
        MULTIPLY UNIT-PRICE(LINE-ITEM) 
            BY QUANTITY(LINE-ITEM) 
            GIVING EXTENDED-PRICE(LINE-ITEM)
        END-MULTIPLY
        MOVE LINE-ITEM-DATA(LINE-ITEM) TO OUT-LINE-ITEM
        IF LINE-ITEM IS GREATER THAN MAX-LINE-ITEMS
            WRITE OUTPUT-FILE FROM OUTPUT-RECORD
        END-IF 
        .
    NEXT-PARAGRAPH.
        . . . 

Style 3: Mixed case

More recent versions of Cobol support mixed case source code. Cobol is not case sensitive; it is case agnostic. The following Cobol identifiers all refer to the same Data Division item:

        ACCOUNT-NUMBER 
        account-number 
        AccountNumber
        accountNumber
        aCcOuNt-NuMbEr 

The sample snippet in this style using a camel case naming convention:

    CalculateExtendedPrice.
        multiply unitPrice(lineItem) 
            by quantity(lineItem) 
            giving extendedPrice(lineItem)
        end-multiply
        move lineItemData(lineItem) to outLineItem
        if lineItem is greater than maxLineItems
            write outputFile from outputRecord
        end-if
        .
    NextParagraph.
        . . . 

The sample snippet in this style using dashes in Data Division item references, capitals for Cobol keywords, and symbols for greater than and less than phrases:

    Calculate-Extended-Price.
        MULTIPLY unit-price(line-item) 
            BY quantity(line-item) 
            GIVING extended-price(line-item)
        END-MULTIPLY
        MOVE line-item-data(line-item) TO out-line-item
        IF line-item > max-line-items
            WRITE output-file FROM output-record
        END-IF
        .
    Next-Paragraph.
        . . . 

Style 4: Free-form source code

Contemporary Cobol compilers support free-form source code, in which the programmer need not conform to the rules about Area A, Area B, and reserved columns for line continuation, comments, line numbers, or identifying information. However, in mainframe systems the original special meanings of particular columns in each source line still prevail. Therefore, Cobol Check assumes the programs it parses follow the original Cobol specification. Capitalization and periods are a matter of coding style, but this is not.

Clone this wiki locally