Key Takeaways — Chapter 9: Copybooks and Code Reuse

Core Concepts

  1. 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.

  2. REPLACING enables parameterization. Pseudo-text replacement (==:PREFIX:== BY ==ACCT==) transforms a single copybook into a template that serves multiple contexts.

  3. Copybooks enforce consistency. When 347 programs share a record layout via one copybook, every program agrees on field names, types, and offsets.

  4. 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

  1. Record Layout Copybooks — The most common pattern. Define the complete 01-level record structure with 88-level conditions and trailing FILLER.

  2. Working-Storage Group Copybooks — Centralize common working-storage fields (dates, counters, flags, constants) shared across programs.

  3. Paragraph Library Copybooks — Encapsulate standard procedure code (error logging, date formatting). Document all prerequisites in the header.

  4. Parameterized Copybooks — Use replacement tokens (:PREFIX:, :TAG:) for copybooks that will be COPYed multiple times with different substitutions.

Best Practices

  1. One copybook, one purpose. Keep copybooks focused on a single concern.

  2. Document the contract. Every copybook needs a header comment: name, version, date, purpose, consuming programs, prerequisites.

  3. Use consistent naming prefixes. All fields in ACCT-REC start with ACCT-. All fields in CLM-REC start with CLM-. This prevents name collisions.

  4. Leave FILLER for growth. Pad record layouts to round sizes with generous FILLER. Future field additions become backward-compatible.

  5. Limit nesting depth. One or two levels of nested COPY is manageable. Beyond that, complexity becomes a liability.

Common Pitfalls

  1. Shadow copybooks — The same name in multiple libraries; concatenation order determines which version the compiler uses.

  2. Forgotten recompilation — Changing a copybook without recompiling all consuming programs leads to data corruption.

  3. Identifier replacement surprises — Identifier REPLACING only matches complete words; use pseudo-text for partial matches.

  4. Oversized copybooks — A 500-line do-everything copybook is harder to maintain than five focused 100-line copybooks.

Modernization Connection

  1. Copybooks as interface definitions. They describe data precisely enough to generate JSON schemas, Java classes, or database DDL — bridging mainframe and distributed worlds.

  2. Single source of truth. Treat the copybook as the canonical definition; derive all other representations from it.