Key Takeaways — Chapter 24: Nested Programs

Structure and Syntax

  1. Nested (contained) programs are complete COBOL programs defined within another program's source code. They compile as a single compilation unit and share a single load module.

  2. END PROGRAM markers are mandatory for every nested program and must nest properly — innermost closed first, like balanced parentheses.

  3. Each nested program has its own IDENTIFICATION, DATA, and PROCEDURE DIVISIONS with private WORKING-STORAGE. Data is isolated by default.

Visibility and Scope

  1. Default calling scope: A program can call its directly contained (child) programs. Siblings cannot call each other without the COMMON attribute.

  2. The COMMON attribute makes a nested program callable by its siblings — programs at the same nesting level within the same parent.

  3. The GLOBAL clause makes data items and files visible to all programs directly or indirectly contained within the declaring program. GLOBAL flows downward only — never upward to parents or sideways to siblings at the same level.

  4. GLOBAL shadowing: If a nested program defines a local variable with the same name as a GLOBAL variable in its parent, the local definition takes precedence. This can cause subtle bugs.

Design Guidelines

  1. Use nested programs for internal utilities tightly coupled to a single outer program — formatting routines, local validators, step loggers.

  2. Use external subprograms for reusable logic needed by multiple callers or requiring independent deployment.

  3. The hybrid approach — external subprograms for shared business logic, nested programs for internal plumbing — combines the strengths of both patterns and is the recommended practice.

  4. Use GLOBAL sparingly: Best for shared files and counters. Prefer explicit parameter passing for business data to maintain clear data flow.

  5. Keep nested program source files manageable: If the total file exceeds approximately 1,500-3,000 lines, consider extracting some nested programs to external subprograms.

The Big Picture

  1. Nested programs support the Readability is a Feature theme by organizing a program's internal complexity into clearly bounded, named units with isolated data, making the outer program's PROCEDURE DIVISION read as pure orchestration logic.