Reading, Debugging & Maintaining Legacy Code

COBOL Intermediate/Advanced

Chapter 9 · Reading, Debugging & Maintaining Legacy Code

Fundamentals' own Chapter 1 established the real scale — some 200 billion lines of COBOL still running, 80% of all business programs. The honest, genuinely dominant reality of real COBOL work today follows directly from that: far more of it is reading and maintaining code someone else wrote decades ago than writing anything new.

GO TO & the Real Structured-Programming Shift

Real code older than the industry's own shift toward structured programming leans heavily on GO TO, jumping freely between paragraphs rather than using Chapter 5's own real PERFORM/EVALUATE structures. That shift traces to a genuinely famous real letter — Edsger Dijkstra's "Go To Statement Considered Harmful," published in the March 1968 Communications of the ACM (originally titled "A Case Against the Goto Statement," changed by editor Niklaus Wirth). Donald Knuth's own real, wry remark on the resulting backlash: "Dr. Goto cheerfully complained that he was always being eliminated."

Why This Genuinely Matters for Reading Old Code Code written before, or slow to adopt, this real shift can require tracing a real tangle of GO TO statements jumping between paragraphs in an order that has nothing to do with how the source is actually laid out on the page — genuinely harder to follow than Chapter 5's own structured PERFORM-based style.

ALTER: A Real, Now-Discouraged Trap

Truly old code can hide something genuinely worse than a plain GO TO — the real ALTER statement, which modifies a GO TO's own target at runtime:

PROCESS-PARA. GO TO STEP-A. STEP-A. DISPLAY "PROCESSING STEP A". ALTER PROCESS-PARA TO PROCEED TO STEP-B. STEP-B. DISPLAY "PROCESSING STEP B".
A Genuinely Disorienting Real Behavior The first time PROCESS-PARA runs, its GO TO sends control to STEP-A — which then genuinely rewrites that same GO TO statement so the next time PROCESS-PARA runs, it goes to STEP-B instead. The exact same line of source code does something different the second time, with the real change made from somewhere else in the program entirely.

ALTER is real, but genuinely discouraged — it actively encourages unstructured logic, modern compilers can flag it with a real diagnostic, and it's outright disallowed in real recursive programs, methods, or threaded code. The documented fix reuses exactly what Fundamentals' own Chapter 5 already covered:

01 WS-CURRENT-STEP PIC X(1) VALUE "A". PROCESS-PARA. EVALUATE WS-CURRENT-STEP WHEN "A" DISPLAY "PROCESSING STEP A" MOVE "B" TO WS-CURRENT-STEP WHEN "B" DISPLAY "PROCESSING STEP B" END-EVALUATE.

Cryptic Names & Undocumented Logic

Real historical field-length limits pushed decades of COBOL toward heavily abbreviated, genuinely cryptic field names — a real practical reading skill for legacy work is inferring a field's actual purpose from how it's used across a program, since the name itself often won't say.

The Real Debugging Line Indicator

A real, literal D in a fixed-format line's own indicator column (column 7) marks that line as debugging-only — genuinely present in the source, but otherwise ignored unless the program is compiled with debugging lines specifically enabled. Finding a stray D-marked line in old code is a real, direct clue that a previous maintainer left tracing or diagnostic output behind deliberately, not by accident.

A Real, Practical Approach to Modernization

Given the real scale of code involved, wholesale rewriting is rarely the genuinely practical choice. A far more common real approach wraps existing, working COBOL behind a modern interface — Chapter 4's own embedded SQL or Chapter 7's own CICS transaction, for instance — modernizing what's visible at the edges while leaving proven, working internal logic genuinely untouched.

Hands-On Exercises

Exercise 1

Using this chapter's own real ALTER example, explain in your own words why reading PROCESS-PARA's own source code once, without knowing STEP-A also contains a real ALTER statement, would give a genuinely misleading picture of what the program actually does the second time it runs.

📄 View solution
Exercise 2

Using this chapter's own real EVALUATE-based rewrite, explain in your own words why WS-CURRENT-STEP genuinely needs to exist as a real, separate WORKING-STORAGE field for the rewritten version to reproduce the original ALTER version's own real behavior.

📄 View solution
Exercise 3

Using this chapter's own real material, explain in your own words why wrapping existing, working COBOL behind a modern interface is genuinely often preferred over a full rewrite, given the real scale of COBOL code already established in Fundamentals Chapter 1.

📄 View solution

Chapter 9 Quick Reference

  • GO TO-heavy legacy code — real pre-structured-programming style, predating or slow to adopt Dijkstra's real 1968 critique
  • ALTER — real, genuinely discouraged; rewrites a GO TO's own target at runtime; documented fix is EVALUATE
  • Cryptic field names — a real legacy of historical field-length limits; infer purpose from usage, not the name alone
  • Column-7 'D' — the real debugging line indicator; ignored unless debugging lines are specifically enabled at compile time
  • Wrapping over rewriting — a real, practical modernization pattern given COBOL's own documented scale