Exercises — Chapter 8: Paragraph and Section Design

Exercise 8.1: Paragraph Naming (Beginner)

Rename the following paragraphs using proper verb-noun naming and hierarchical numbering. Explain your naming choices.

       START-HERE.
       DO-FILE.
       PARA-A.
       PROCESS.
       ROUTINE-1.
       ROUTINE-2.
       CHECK.
       WRITE-AND-UPDATE-AND-LOG.
       MISC-STUFF.
       THE-END.

Assume the program reads customer records, validates them, applies discounts, writes output records, and produces a summary report. Assign appropriate numbers and names that communicate the program's structure.


Exercise 8.2: Top-Down Design (Intermediate)

Design the paragraph structure (names and hierarchy only — no code) for a program with the following requirements:

Program: PAYROLL-CALC — Calculates weekly payroll for all employees.

Requirements: 1. Read employee records from EMPLOYEE-FILE 2. For each employee: a. Validate employee status (must be active) b. Determine pay type (hourly or salaried) c. For hourly: calculate regular hours and overtime hours, apply rates d. For salaried: divide annual salary by 52 e. Calculate deductions (federal tax, state tax, insurance, 401k) f. Calculate net pay g. Write paycheck record h. Write pay stub detail 3. Write department subtotals when department changes (control break) 4. Write company grand totals at end

Deliverables: 1. A complete paragraph list with hierarchical numbers and verb-noun names 2. The 1000-MAIN-PROCESS paragraph showing the IPT structure 3. The 2000-PROCESS-ONE-EMPLOYEE paragraph showing the processing flow 4. Brief justification for your decomposition decisions


Exercise 8.3: Cohesion Analysis (Intermediate)

Evaluate the cohesion of each paragraph below. Rate each as HIGH, MEDIUM, or LOW cohesion, explain why, and suggest how to refactor any paragraph rated MEDIUM or LOW.

       2100-VALIDATE-AND-PROCESS.
           IF WS-AMOUNT IS NOT NUMERIC
               MOVE 'INVALID AMOUNT' TO WS-ERROR-MSG
               PERFORM 9100-LOG-ERROR
           ELSE
               COMPUTE WS-TAX = WS-AMOUNT * WS-TAX-RATE
               WRITE OUTPUT-RECORD FROM WS-DETAIL-LINE
               ADD 1 TO WS-RECORD-COUNT
           END-IF
           .

       2200-CHECK-BALANCE.
           IF WS-BALANCE < WS-MINIMUM
               SET BALANCE-BELOW-MINIMUM TO TRUE
               MOVE WS-BALANCE TO WS-RPT-BALANCE
               MOVE 'LOW BALANCE' TO WS-RPT-STATUS
           ELSE
               SET BALANCE-ABOVE-MINIMUM TO TRUE
               MOVE WS-BALANCE TO WS-RPT-BALANCE
               MOVE 'OK' TO WS-RPT-STATUS
           END-IF
           .

       2300-PRINT-HEADER.
           MOVE WS-REPORT-TITLE TO RPT-TITLE
           MOVE WS-REPORT-DATE TO RPT-DATE
           MOVE WS-PAGE-NUMBER TO RPT-PAGE
           WRITE REPORT-RECORD FROM WS-HEADER-LINE-1
           WRITE REPORT-RECORD FROM WS-HEADER-LINE-2
           WRITE REPORT-RECORD FROM WS-COLUMN-HEADERS
           .

       2400-UPDATE-ALL-TABLES.
           MOVE WS-NEW-RATE TO WS-RATE-TABLE(WS-IDX)
           ADD WS-AMOUNT TO WS-MONTHLY-TOTAL(WS-MONTH)
           MOVE WS-STATUS TO WS-STATUS-TABLE(WS-STAT-IDX)
           COMPUTE WS-AVERAGE = WS-TOTAL / WS-COUNT
           PERFORM 2500-RECALCULATE-PROJECTIONS
           .

Exercise 8.4: GO TO Refactoring (Intermediate)

Refactor the following GO TO-based code into structured COBOL without any GO TO statements. Preserve the exact behavior.

       2000-PROCESS.
           IF WS-INPUT-TYPE = 'A' GO TO 2000-ADD.
           IF WS-INPUT-TYPE = 'U' GO TO 2000-UPDATE.
           IF WS-INPUT-TYPE = 'D' GO TO 2000-DELETE.
           MOVE 'INVALID TYPE' TO WS-ERROR-MSG.
           GO TO 2000-ERROR.

       2000-ADD.
           IF WS-RECORD-EXISTS = 'Y'
               MOVE 'ALREADY EXISTS' TO WS-ERROR-MSG
               GO TO 2000-ERROR
           END-IF.
           PERFORM 3100-INSERT-RECORD.
           GO TO 2000-DONE.

       2000-UPDATE.
           IF WS-RECORD-EXISTS = 'N'
               MOVE 'NOT FOUND' TO WS-ERROR-MSG
               GO TO 2000-ERROR
           END-IF.
           PERFORM 3200-MODIFY-RECORD.
           GO TO 2000-DONE.

       2000-DELETE.
           IF WS-RECORD-EXISTS = 'N'
               MOVE 'NOT FOUND' TO WS-ERROR-MSG
               GO TO 2000-ERROR
           END-IF.
           PERFORM 3300-REMOVE-RECORD.
           GO TO 2000-DONE.

       2000-ERROR.
           PERFORM 9100-LOG-ERROR.
           ADD 1 TO WS-ERROR-COUNT.

       2000-DONE.
           ADD 1 TO WS-PROCESSED-COUNT.

Requirements: 1. Use EVALUATE or structured IF-ELSE 2. No GO TO statements 3. Same behavior for all input combinations 4. Add 88-level condition names where appropriate


Exercise 8.5: Coupling Reduction (Intermediate)

The following two paragraphs are tightly coupled. Identify all coupling points and refactor to reduce coupling using 88-level condition names and clear contracts.

       WORKING-STORAGE SECTION.
       01  WS-FLAG-1        PIC X(01).
       01  WS-FLAG-2        PIC X(01).
       01  WS-INTERNAL-CODE PIC 9(02).
       01  WS-AMT           PIC S9(07)V99.
       01  WS-RESULT        PIC S9(07)V99.

       PROCEDURE DIVISION.
       2100-PREPARE.
           MOVE 'X' TO WS-FLAG-1
           MOVE 0 TO WS-INTERNAL-CODE
           IF WS-AMT > 1000
               MOVE 'H' TO WS-FLAG-1
               MOVE 5 TO WS-INTERNAL-CODE
           ELSE IF WS-AMT > 100
               MOVE 'M' TO WS-FLAG-1
               MOVE 3 TO WS-INTERNAL-CODE
           ELSE
               MOVE 'L' TO WS-FLAG-1
               MOVE 1 TO WS-INTERNAL-CODE
           END-IF
           IF WS-AMT < 0
               MOVE 'R' TO WS-FLAG-2
           ELSE
               MOVE 'N' TO WS-FLAG-2
           END-IF
           .

       2200-CALCULATE.
      *    Must know: 'X'=invalid, 'H'=high, 'M'=medium, 'L'=low
      *    Must know: 5=5%, 3=3%, 1=1%
      *    Must know: 'R'=refund, 'N'=normal
           IF WS-FLAG-1 = 'X'
               MOVE 0 TO WS-RESULT
           ELSE
               COMPUTE WS-RESULT =
                   WS-AMT * WS-INTERNAL-CODE / 100
               IF WS-FLAG-2 = 'R'
                   MULTIPLY -1 BY WS-RESULT
               END-IF
           END-IF
           .

Exercise 8.6: Complete Program Design (Advanced)

Design and write a complete COBOL program following all the principles in this chapter.

Requirements: A library book checkout system that: 1. Reads a file of checkout requests (book ID, member ID, checkout date) 2. Validates: member must be active, member cannot have more than 5 books out, book must be available 3. For valid checkouts: update book status, set due date (14 days from checkout), write confirmation record 4. For invalid checkouts: write rejection record with reason 5. At end of file: write summary (total requests, approved, rejected, by rejection reason)

Your program must demonstrate: - IPT pattern in main paragraph - Hierarchical numbered paragraphs with verb-noun naming - 88-level condition names for all statuses - EVALUATE for routing logic - Maximum 3 levels of nesting - Each paragraph under 30 lines - High cohesion, low coupling - No GO TO (or disciplined GO TO with PERFORM THRU — your choice)


Exercise 8.7: Code Review Checklist (Beginner)

You are reviewing a colleague's COBOL program. Using the guidelines from Section 8.14, create a code review checklist with at least 15 items. For each item, provide: 1. The check to perform 2. Why it matters 3. An example of passing vs. failing

Apply your checklist to the "poorly structured" example from Section 8.13 and document all findings.


Exercise 8.8: Debate — Write and Defend a Position (Advanced)

Write a 500-word position paper on one of the following topics. Support your position with specific code examples from this chapter and your own experience.

Option A: "GO TO should be completely banned from all new COBOL programs."

Option B: "PERFORM THRU with GO TO to exit paragraphs is the most readable pattern for validation logic and should be the standard."

Option C: "Sections add unnecessary complexity and should never be used in modern COBOL programs."

For whichever position you choose, you must acknowledge at least two strong counterarguments and explain why your position is still correct despite them.