Key Takeaways — Chapter 9: Copybooks and Code Reuse
Core Concepts
-
Copybooks are COBOL's primary code reuse mechanism. The COPY statement inserts external text into the source at compile time, enabling shared definitions across hundreds of programs.
-
REPLACING enables parameterization. Pseudo-text replacement (
==:PREFIX:== BY ==ACCT==) transforms a single copybook into a template that serves multiple contexts. -
Copybooks enforce consistency. When 347 programs share a record layout via one copybook, every program agrees on field names, types, and offsets.
-
A copybook change requires recompilation. Every program that COPYs a modified copybook must be recompiled — plan accordingly with build systems and dependency tracking.
Design Patterns
-
Record Layout Copybooks — The most common pattern. Define the complete 01-level record structure with 88-level conditions and trailing FILLER.
-
Working-Storage Group Copybooks — Centralize common working-storage fields (dates, counters, flags, constants) shared across programs.
-
Paragraph Library Copybooks — Encapsulate standard procedure code (error logging, date formatting). Document all prerequisites in the header.
-
Parameterized Copybooks — Use replacement tokens (
:PREFIX:,:TAG:) for copybooks that will be COPYed multiple times with different substitutions.
Best Practices
-
One copybook, one purpose. Keep copybooks focused on a single concern.
-
Document the contract. Every copybook needs a header comment: name, version, date, purpose, consuming programs, prerequisites.
-
Use consistent naming prefixes. All fields in ACCT-REC start with
ACCT-. All fields in CLM-REC start withCLM-. This prevents name collisions. -
Leave FILLER for growth. Pad record layouts to round sizes with generous FILLER. Future field additions become backward-compatible.
-
Limit nesting depth. One or two levels of nested COPY is manageable. Beyond that, complexity becomes a liability.
Common Pitfalls
-
Shadow copybooks — The same name in multiple libraries; concatenation order determines which version the compiler uses.
-
Forgotten recompilation — Changing a copybook without recompiling all consuming programs leads to data corruption.
-
Identifier replacement surprises — Identifier REPLACING only matches complete words; use pseudo-text for partial matches.
-
Oversized copybooks — A 500-line do-everything copybook is harder to maintain than five focused 100-line copybooks.
Modernization Connection
-
Copybooks as interface definitions. They describe data precisely enough to generate JSON schemas, Java classes, or database DDL — bridging mainframe and distributed worlds.
-
Single source of truth. Treat the copybook as the canonical definition; derive all other representations from it.