Skip to content

Cobol Coding Styles

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

Home -> Development 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.

    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.
        . . . 

A wide range of coding conventions have been developed in different organizations.

Clone this wiki locally