COBOL Std 5 Program Structure

From Webhuis wiki
Revision as of 11:13, 25 March 2026 by Martin (talk | contribs) (Created page with "== 5. Program Structure == === 5.1 Division Order === 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. <code>CONSOLE IS scherm</code>). # DATA DIVISION — FILE SECTION, WORKING-STORAGE SECTION, LINKAGE SECTION (if a subprogram). # PROCEDURE DIVISION — all executable logic. === 5.2 Mandatory IDENTIFICATION D...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

5. Program Structure[edit]

5.1 Division Order[edit]

Every program must contain all four divisions in this sequence:

  1. IDENTIFICATION DIVISION — program identity and metadata.
  2. ENVIRONMENT DIVISION — platform bindings, file assignments, special names (e.g. CONSOLE IS scherm).
  3. DATA DIVISION — FILE SECTION, WORKING-STORAGE SECTION, LINKAGE SECTION (if a subprogram).
  4. 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:

  1. Program identification filler (version string, program name).
  2. Database connection parameters (if a DAL program).
  3. Switch / flag fields (SW- prefix).
  4. Counter fields (CTR- prefix).
  5. Error-handling fields (ERR- prefix).
  6. Business data fields.
  7. SQL host variable groups (DB- prefix, via EXEC SQL INCLUDE or COPY).

5.4 PROCEDURE DIVISION Structure[edit]

The procedure division must follow this paragraph-series order:

  1. A000 — entry paragraph; EVALUATE on method verb or input flag; dispatches to lower series.
  2. R-series — business logic (algorithm, loop bodies, report formatting).
  3. S-series — database and system routines (connect, cursor, fetch, insert, disconnect).
  4. D-series — validation sub-routines.
  5. E-series — error handling.
  6. 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.



← Back to index