Why COBOL still, and what it is good at
COBOL was designed so that business people could read it. That makes it wordy, and it also makes forty-year-old code comprehensible to someone who has never seen it before — which turns out to be worth a great deal.
The design decisions that still matter
- Fixed-point decimal arithmetic. COBOL computes money in decimal, exactly. A language using binary floating point cannot represent 0.10 exactly; COBOL's
PIC S9(9)V99 COMP-3can. For financial work this is not a preference, it is a requirement. - Data described separately from logic. Record layouts live in the DATA DIVISION and are shared between programs through copybooks. Change the layout in one place and every program picks it up on recompile.
- Verbose, English-like syntax.
ADD 1 TO WS-COUNTERis not clever, and that is the point. Someone unfamiliar with the codebase can follow it. - No pointers by default, no dynamic memory in classic style. Programs are predictable and their storage requirements are known before they run.
What it is not good at
COBOL has no rich standard library, limited string handling by modern standards, and a type system that will happily let you move a character field into a numeric one and only complain at run time — which is exactly how S0C7 abends happen. Modern COBOL has objects, JSON parsing and calls into other languages, but most production code does not use them.
A complete, small program
IDENTIFICATION DIVISION. PROGRAM-ID. PAYSUM. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT PAY-FILE ASSIGN TO INFILE ORGANIZATION IS SEQUENTIAL FILE STATUS IS WS-STATUS. DATA DIVISION. FILE SECTION. FD PAY-FILE. 01 PAY-RECORD. 05 PAY-EMP-ID PIC X(08). 05 PAY-NAME PIC X(30). 05 PAY-AMOUNT PIC S9(7)V99 COMP-3. 05 FILLER PIC X(20). WORKING-STORAGE SECTION. 01 WS-STATUS PIC XX VALUE SPACES. 01 WS-EOF-FLAG PIC X VALUE 'N'. 88 END-OF-FILE VALUE 'Y'. 01 WS-COUNT PIC 9(07) VALUE ZERO. 01 WS-TOTAL PIC S9(11)V99 COMP-3 VALUE ZERO. 01 WS-TOTAL-ED PIC ZZZ,ZZZ,ZZ9.99-. PROCEDURE DIVISION. 0000-MAIN. PERFORM 1000-OPEN PERFORM 2000-PROCESS UNTIL END-OF-FILE PERFORM 3000-CLOSE GOBACK. 1000-OPEN. OPEN INPUT PAY-FILE IF WS-STATUS NOT = '00' DISPLAY 'OPEN FAILED, STATUS ' WS-STATUS MOVE 12 TO RETURN-CODE GOBACK END-IF PERFORM 2100-READ. 2000-PROCESS. ADD 1 TO WS-COUNT ADD PAY-AMOUNT TO WS-TOTAL PERFORM 2100-READ. 2100-READ. READ PAY-FILE AT END SET END-OF-FILE TO TRUE END-READ. 3000-CLOSE. CLOSE PAY-FILE MOVE WS-TOTAL TO WS-TOTAL-ED DISPLAY 'RECORDS READ : ' WS-COUNT DISPLAY 'TOTAL AMOUNT : ' WS-TOTAL-ED.
Common mistakes
It handles decimal arithmetic, file I/O and record structure better than most modern languages, because that is what it was built for.
Consistency with the existing codebase matters more than using every feature the compiler supports.
What you will see at work
- You will read far more COBOL than you write. Reading fluently is the skill that gets you productive.
- Programs are typically 500 to 5,000 lines with numbered paragraphs. The numbering encodes call order and is a real navigation aid.
- GOBACK, STOP RUN and EXIT PROGRAM all end a program. Your site will have a convention; GOBACK is the safest general choice.
Key terms
Check your understanding.
Take this lesson's quiz and save your progress. Free.