Chapter 4: Key Takeaways -- The WORKING-STORAGE and LOCAL-STORAGE Sections
Chapter Summary
Chapter 4 transitions from the theoretical foundations of COBOL data description into the practical art of organizing a program's internal data. The WORKING-STORAGE SECTION is where every COBOL program defines its internal variables -- constants, flags, counters, accumulators, work fields, record layouts, and display fields. Learning to organize WORKING-STORAGE well is one of the most important skills a COBOL programmer can develop, because a well-structured data area directly translates into a maintainable, readable program. The chapter teaches professional organizational conventions: group related items together, use comment blocks as section dividers, place constants first, then flags, counters, work fields, record layouts, display fields, and date fields.
A central theme of this chapter is the distinction between WORKING-STORAGE and LOCAL-STORAGE. WORKING-STORAGE items are allocated once at program load time and persist for the program's entire lifetime, retaining their values between calls when used as a subprogram. LOCAL-STORAGE items, introduced in COBOL-2002, are reinitialized from their VALUE clauses on every invocation, making them essential for recursive programs, reentrant programs, and any subprogram that needs a clean slate on each call. Understanding when to use each section is a critical design decision for any program that will be called by other programs.
The chapter also provides deep coverage of practical data patterns: the INITIALIZE statement for safely resetting mixed-type records, REDEFINES for multi-format records and table initialization, 88-level condition names for self-documenting code, OCCURS for arrays, FILLER for spacing and padding, the SYNCHRONIZED clause for binary field alignment, and naming conventions that make large programs navigable. The emphasis throughout is on professional practices that prevent common production bugs, such as using INITIALIZE instead of MOVE SPACES on mixed-type records and always defining 88-level conditions for every flag field.
Key Concepts
- WORKING-STORAGE is allocated once when the program loads. Values set by VALUE clauses are initialized once and persist for the program's lifetime, including between calls to the program as a subprogram.
- LOCAL-STORAGE is reinitialized from VALUE clauses on every invocation of the program. Items without VALUE clauses are undefined at the start of each call.
- The recommended WORKING-STORAGE organization is: constants (WS-C- prefix), flags (WS-F- prefix), counters (WS-CTR- prefix), accumulators (WS-ACC- prefix), work fields (WS-WK- prefix), record layouts, display fields (WS-DSP- prefix), and date/time fields.
- The INITIALIZE statement sets alphanumeric fields to SPACES and numeric fields to ZEROS, handling mixed-type records correctly unlike MOVE SPACES or MOVE ZEROS.
- INITIALIZE does not affect FILLER items by default. Use
INITIALIZE ... WITH FILLERto include them, orINITIALIZE ... ALL TO VALUEto restore original VALUE clause settings. - INITIALIZE REPLACING allows custom values:
INITIALIZE record REPLACING ALPHANUMERIC DATA BY '*' NUMERIC DATA BY 9. - REDEFINES allows multiple data descriptions to share the same storage. Common patterns include date field decomposition, multi-format transaction records, numeric validation of text input, and compile-time table initialization.
- Level 88 condition names should be defined for every flag, status code, and enumerated value. Use SET to change condition values rather than MOVE for maintainability.
- Level 77 declares independent elementary items that are not part of any group. It is functionally identical to a level-01 elementary item; shops vary on whether they prefer 77 or 01 for standalone fields.
- Level 66 (RENAMES) provides alternative groupings of contiguous elementary items without allocating new storage. It is relatively rare in modern code.
- OCCURS defines repeating data items (arrays), accessed by subscript:
WS-MONTH-AMOUNT(3). Multi-dimensional tables use nested OCCURS. - FILLER items occupy space in a record but cannot be individually referenced. They provide spacing in print lines, labels in header lines, and padding in fixed-length records.
- The SYNCHRONIZED clause aligns COMP fields on hardware word boundaries, potentially improving performance on mainframes but adding slack bytes that change record sizes.
- Data names should be descriptive (WS-CUSTOMER-BALANCE, not WS-CB), use hyphens for readability, and follow consistent prefix conventions throughout the program.
Common Pitfalls
- MOVE SPACES to a mixed-type record. This puts space characters in numeric fields, causing data exceptions (S0C7 abends on mainframes) when those fields are used in arithmetic. Always use INITIALIZE for mixed-type records.
- REDEFINES size mismatch. If the redefining item is larger than the redefined item, it reads memory beyond the original storage area, potentially corrupting adjacent data or reading garbage.
- Forgetting to reinitialize work fields in a processing loop. Work fields from the previous iteration retain their values. Without reinitialization at the top of the loop, stale data from the prior record contaminates the current calculation.
- Numeric field overflow without detection. A PIC 9(3) counter that exceeds 999 silently wraps to 000. Use ON SIZE ERROR to catch overflows, and size fields generously.
- Using MOVE to set condition values instead of SET. While
MOVE 'Y' TO WS-EOF-FLAGworks,SET WS-EOF TO TRUEis preferred because it is self-documenting and change-resistant -- if the flag value changes in the data definition, only one place needs updating. - Confusing WORKING-STORAGE and LOCAL-STORAGE in subprograms. Placing an accumulator in LOCAL-STORAGE means it resets to zero on every call, losing all accumulated values. Placing a temporary work field in WORKING-STORAGE means it retains stale values from the previous call.
- Group-level comparisons on records containing numeric fields. Comparing a group item treats the entire group as alphanumeric, which may produce unexpected results when numeric fields are involved due to sign encoding and packed decimal representation.
- Defining display fields too small for their corresponding data fields. If WS-GROSS-PAY is PIC 9(7)V99, the corresponding display field needs at least PIC $$$,$$$,$$9.99 to avoid truncation of large values.
Quick Reference
WORKING-STORAGE vs LOCAL-STORAGE:
WORKING-STORAGE: Allocated once, persists, VALUE applied once
LOCAL-STORAGE: Reinitialized per call, VALUE applied each time
INITIALIZE Statement:
INITIALIZE WS-RECORD (spaces to X, zeros to 9)
INITIALIZE WS-RECORD WITH FILLER (includes FILLER items)
INITIALIZE WS-RECORD ALL TO VALUE (restores VALUE clauses)
INITIALIZE WS-RECORD
REPLACING ALPHANUMERIC DATA BY '*'
NUMERIC DATA BY 9
Level Numbers:
01 Record level (Area A)
02-49 Subordinate items (Area B, use 05/10/15 convention)
66 RENAMES (alternative grouping, no new storage)
77 Independent elementary item (Area A)
88 Condition name (no storage, attached to parent)
Condition Names (88-Level):
01 WS-STATUS PIC X(01).
88 ACTIVE VALUE 'A'.
88 CLOSED VALUE 'C'.
88 VALID-STATUS VALUE 'A' 'C'.
SET ACTIVE TO TRUE Sets WS-STATUS to 'A'
IF ACTIVE ... Tests if WS-STATUS = 'A'
REDEFINES Patterns:
Date: 01 WS-DATE PIC 9(8).
01 WS-PARTS REDEFINES WS-DATE.
05 WS-YEAR PIC 9(4).
05 WS-MONTH PIC 9(2).
05 WS-DAY PIC 9(2).
Table: 01 WS-VALUES.
05 FILLER PIC X(9) VALUE 'JANUARY'.
05 FILLER PIC X(9) VALUE 'FEBRUARY'.
...
01 WS-TABLE REDEFINES WS-VALUES.
05 WS-MONTH-NAME PIC X(9) OCCURS 12 TIMES.
Prefix Conventions:
WS- WORKING-STORAGE LS- LOCAL-STORAGE
WS-C- Constant WS-F- Flag
WS-CTR- Counter WS-ACC- Accumulator
WS-WK- Work field WS-DSP- Display field
LK- LINKAGE SECTION
What's Next
Chapter 5 introduces basic input and output with the DISPLAY and ACCEPT statements. You will learn to send formatted output to the console, receive user input with validation, retrieve system date and time information, build structured report lines using the record layouts defined in WORKING-STORAGE, create interactive menu-driven programs, and use the SCREEN SECTION for terminal-based data entry forms. The data organization skills from this chapter directly feed into building effective output structures.