COBOL Std 5 Program Structure
5. Program Structure[edit]
5.1 Division Order[edit]
Every program must contain all four divisions in this sequence:
- IDENTIFICATION DIVISION — program identity and metadata.
- ENVIRONMENT DIVISION — platform bindings, file assignments, special names (e.g.
CONSOLE IS scherm). - DATA DIVISION — FILE SECTION, WORKING-STORAGE SECTION, LINKAGE SECTION (if a subprogram).
- PROCEDURE DIVISION — all executable logic.
5.2 Mandatory IDENTIFICATION DIVISION Entries[edit]
<syntaxhighlight lang="cobol">
IDENTIFICATION DIVISION.
PROGRAM-ID. PROG-NAME.
AUTHOR. Developer Name.
DATE-WRITTEN. YYYY-MM-DD.
DATE-COMPILED. YYYY-MM-DD.
SECURITY. INTERNAL USE ONLY.
</syntaxhighlight>
The PROGRAM-ID must match the source file name exactly (without extension).
5.3 Working-Storage Layout Order[edit]
Organise WORKING-STORAGE in this sequence:
- Program identification filler (version string, program name).
- Database connection parameters (if a DAL program).
- Switch / flag fields (
SW-prefix). - Counter fields (
CTR-prefix). - Error-handling fields (
ERR-prefix). - Business data fields.
- SQL host variable groups (
DB-prefix, viaEXEC SQL INCLUDEorCOPY).
5.4 PROCEDURE DIVISION Structure[edit]
The procedure division must follow this paragraph-series order:
- A000 — entry paragraph; EVALUATE on method verb or input flag; dispatches to lower series.
- R-series — business logic (algorithm, loop bodies, report formatting).
- S-series — database and system routines (connect, cursor, fetch, insert, disconnect).
- D-series — validation sub-routines.
- E-series — error handling.
- Z000 — termination / cleanup; always the last paragraph; contains EXIT PROGRAM or STOP RUN.
5.5 Three-Tier Architecture[edit]
For any application accessing a database, adopt the following tier separation. Each tier communicates through a shared copybook control block passed by reference.
| Tier | Program Role | Responsibilities |
|---|---|---|
| Orchestrator | *main |
Accept command-line arguments; initialise UI; dispatch to business logic; manage session lifecycle. |
| Business Logic | *gen / *proc |
Implement algorithms, loops, and decision logic. Call DAL and UI. Never contain SQL. |
| Data Access (DAL) | *dal / *db |
All SQL statements. One EXEC SQL per paragraph. Exposes a method-dispatch interface. Never contains presentation logic. |
| Presentation (UI) | *ui / *rpt |
All file I/O and console output. Manages print-file lifecycle. Never contains SQL or business rules. |
5.6 Method-Dispatch Pattern[edit]
Inter-tier calls use a method-dispatch pattern: the caller places a verb string into a control-block method field, invokes the subprogram, and inspects the result code on return.
<syntaxhighlight lang="cobol">
MOVE 'connect' TO DAL-METHODS
CALL 'primes' USING PRIMES-DAL
IF dal-method-ok
CONTINUE
ELSE
PERFORM E000-HANDLE-ERROR
END-IF
</syntaxhighlight>
The called program must implement an A000 EVALUATE block on its method field and set the result code before EXIT PROGRAM. Valid result codes are defined in §7.1.
| name = Ambox{{subst:void|Don't change anything on this line. It will change itself when you save.}} | subst = {{subst:substcheck}}
5.7 Paragraph Size[edit]
Each paragraph should perform a single, named function. Aim for 20–80 lines. Paragraphs exceeding 100 lines must be subdivided. Paragraphs under 5 lines are acceptable for dispatch stubs and result-setting.
5.8 Use of Sections[edit]
Avoid PROCEDURE DIVISION sections, except where required by the compiler: DECLARATIVES, SORT/MERGE INPUT/OUTPUT PROCEDURE. Use paragraph-level PERFORM for all other control flow. Section fall-through is a common source of subtle bugs.