Key Takeaways — Chapter 24: Nested Programs
Structure and Syntax
-
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.
-
END PROGRAM markers are mandatory for every nested program and must nest properly — innermost closed first, like balanced parentheses.
-
Each nested program has its own IDENTIFICATION, DATA, and PROCEDURE DIVISIONS with private WORKING-STORAGE. Data is isolated by default.
Visibility and Scope
-
Default calling scope: A program can call its directly contained (child) programs. Siblings cannot call each other without the COMMON attribute.
-
The COMMON attribute makes a nested program callable by its siblings — programs at the same nesting level within the same parent.
-
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.
-
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
-
Use nested programs for internal utilities tightly coupled to a single outer program — formatting routines, local validators, step loggers.
-
Use external subprograms for reusable logic needed by multiple callers or requiring independent deployment.
-
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.
-
Use GLOBAL sparingly: Best for shared files and counters. Prefer explicit parameter passing for business data to maintain clear data flow.
-
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
- 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.