Exercises: Code Review and Static Analysis
Exercise 35.1: Spot the Defects (Beginner)
The following COBOL code contains at least eight defects from the common patterns discussed in this chapter. Find and document each one, explaining the risk and proposing a fix.
IDENTIFICATION DIVISION.
PROGRAM-ID. PAYROLL-CALC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-EMP-SALARY PIC 9(5)V99.
01 WS-TAX-RATE PIC 99.
01 WS-TAX-AMOUNT PIC 9(5)V99.
01 WS-NET-PAY PIC 9(5)V99.
01 WS-BONUS PIC 9(5)V99.
01 WS-TOTAL-COMP PIC 9(5)V99.
01 WS-FILE-STATUS PIC XX.
01 WS-A1 PIC X(10).
PROCEDURE DIVISION.
MAIN-PARA.
OPEN INPUT EMP-FILE
PERFORM PROCESS-EMP UNTIL EOF
STOP RUN.
PERFORM CLOSE-FILES.
PROCESS-EMP.
READ EMP-FILE INTO WS-EMP-REC
AT END SET EOF TO TRUE
IF WS-EMP-TYPE = "F"
COMPUTE WS-TAX-AMOUNT =
WS-EMP-SALARY * WS-TAX-RATE
SUBTRACT WS-TAX-AMOUNT FROM WS-EMP-SALARY
GIVING WS-NET-PAY.
IF WS-YEARS-SERVICE > 10
COMPUTE WS-BONUS = WS-EMP-SALARY * 0.10
ADD WS-BONUS TO WS-TOTAL-COMP
GO TO PROCESS-EMP.
CLOSE-FILES.
CLOSE EMP-FILE.
Exercise 35.2: Apply Coding Standards (Beginner)
Rewrite the program from Exercise 35.1 to comply with the coding standards described in Section 35.3. Your rewritten program should: - Use proper paragraph naming (NNNN-DESCRIPTIVE-NAME) - Have explicit scope terminators on all IF/PERFORM/COMPUTE - Check FILE STATUS after every I/O operation - Include ON SIZE ERROR on COMPUTE statements - Have program header documentation - Have paragraph header comments - Use 88-level conditions for flags - Eliminate the GO TO
Exercise 35.3: Cyclomatic Complexity (Intermediate)
Calculate the cyclomatic complexity for each of the following paragraphs:
Paragraph A:
3000-PROCESS-TRANSACTION.
EVALUATE TXN-TYPE
WHEN "DEP"
ADD TXN-AMOUNT TO ACCT-BALANCE
WHEN "WDR"
IF ACCT-BALANCE >= TXN-AMOUNT
SUBTRACT TXN-AMOUNT FROM ACCT-BALANCE
ELSE
MOVE "NSF" TO TXN-STATUS
END-IF
WHEN "TRF"
IF TXN-FROM-ACCT = ACCT-NUMBER
SUBTRACT TXN-AMOUNT FROM ACCT-BALANCE
END-IF
IF TXN-TO-ACCT = ACCT-NUMBER
ADD TXN-AMOUNT TO ACCT-BALANCE
END-IF
WHEN "FEE"
IF ACCT-TYPE = "P" OR ACCT-BALANCE > 5000
CONTINUE
ELSE
SUBTRACT TXN-FEE FROM ACCT-BALANCE
END-IF
WHEN OTHER
MOVE "UNKNOWN" TO TXN-STATUS
END-EVALUATE.
Paragraph B:
4000-VALIDATE-CLAIM.
IF CLM-MEMBER-ID = SPACES
MOVE "E01" TO CLM-ERROR
ELSE IF CLM-AMOUNT NOT NUMERIC
MOVE "E02" TO CLM-ERROR
ELSE IF CLM-AMOUNT = 0
MOVE "E03" TO CLM-ERROR
ELSE IF CLM-DATE < WS-POLICY-START
OR CLM-DATE > WS-POLICY-END
MOVE "E04" TO CLM-ERROR
ELSE IF CLM-PROVIDER = SPACES
AND CLM-FACILITY = SPACES
MOVE "E05" TO CLM-ERROR
ELSE
MOVE SPACES TO CLM-ERROR
END-IF.
For each paragraph, state the complexity and recommend whether refactoring is needed.
Exercise 35.4: Dead Code Hunt (Intermediate)
Examine the following program and identify all dead code (unreachable paragraphs, unused data items, commented-out code, impossible branches). Document each instance and explain how you determined it was dead.
WORKING-STORAGE SECTION.
01 WS-COUNTER PIC 9(5) VALUE 0.
01 WS-OLD-COUNTER PIC 9(5) VALUE 0.
01 WS-STATUS PIC X VALUE SPACES.
88 STATUS-ACTIVE VALUE "A".
88 STATUS-INACTIVE VALUE "I".
01 WS-TEMP-FLAG PIC X VALUE SPACES.
01 WS-DEBUG-LINE PIC X(80) VALUE SPACES.
PROCEDURE DIVISION.
0000-MAIN.
PERFORM 1000-INIT
PERFORM 2000-PROCESS
PERFORM 8000-REPORT
STOP RUN.
1000-INIT.
MOVE 0 TO WS-COUNTER.
1500-OLD-INIT.
MOVE 0 TO WS-OLD-COUNTER
MOVE SPACES TO WS-TEMP-FLAG.
2000-PROCESS.
IF STATUS-ACTIVE
ADD 1 TO WS-COUNTER
END-IF
IF WS-STATUS = "X"
ADD 1 TO WS-COUNTER
END-IF.
* PERFORM 2500-EXTRA-PROCESS
* IF WS-TEMP-FLAG = "Y"
* ADD 10 TO WS-COUNTER
* END-IF
2500-EXTRA-PROCESS.
ADD 5 TO WS-COUNTER.
3000-UNUSED-REPORT.
DISPLAY WS-DEBUG-LINE.
8000-REPORT.
DISPLAY "Count: " WS-COUNTER.
Exercise 35.5: Code Review Simulation (Intermediate)
Pair up with a classmate (or review your own code after a 24-hour waiting period). Take a COBOL program from any previous chapter exercise and conduct a formal code review:
- The author prepares a review package (source code, test results, change description).
- The reviewer uses the checklist from Section 35.8 to examine the code.
- The reviewer documents findings as Critical, Major, or Minor.
- The author responds to each finding (accept, reject with justification, or request clarification).
- Both parties sign off on the resolution.
Document the entire process and submit the review record.
Exercise 35.6: Duplication Refactoring (Advanced)
The following program contains significant code duplication. Identify all duplicated blocks, then refactor to eliminate duplication using copybooks, shared paragraphs, or subprograms. Measure the reduction in total lines of code.
2000-PROCESS-CHECKING.
MOVE ACCT-BALANCE TO WS-BAL-DISPLAY
IF ACCT-BALANCE < 0
MOVE "OVERDRAWN" TO WS-STATUS
MOVE ACCT-NUMBER TO WS-ERR-ACCT
MOVE "NEGATIVE BAL" TO WS-ERR-MSG
PERFORM 9000-LOG-ERROR
END-IF
COMPUTE WS-INTEREST ROUNDED =
ACCT-BALANCE * WS-CHECK-RATE / 365
* WS-DAYS
ADD WS-INTEREST TO ACCT-BALANCE.
3000-PROCESS-SAVINGS.
MOVE ACCT-BALANCE TO WS-BAL-DISPLAY
IF ACCT-BALANCE < 0
MOVE "OVERDRAWN" TO WS-STATUS
MOVE ACCT-NUMBER TO WS-ERR-ACCT
MOVE "NEGATIVE BAL" TO WS-ERR-MSG
PERFORM 9000-LOG-ERROR
END-IF
COMPUTE WS-INTEREST ROUNDED =
ACCT-BALANCE * WS-SAVE-RATE / 365
* WS-DAYS
ADD WS-INTEREST TO ACCT-BALANCE.
4000-PROCESS-CD.
MOVE ACCT-BALANCE TO WS-BAL-DISPLAY
IF ACCT-BALANCE < 0
MOVE "OVERDRAWN" TO WS-STATUS
MOVE ACCT-NUMBER TO WS-ERR-ACCT
MOVE "NEGATIVE BAL" TO WS-ERR-MSG
PERFORM 9000-LOG-ERROR
END-IF
COMPUTE WS-INTEREST ROUNDED =
ACCT-BALANCE * WS-CD-RATE / 365
* WS-DAYS
ADD WS-INTEREST TO ACCT-BALANCE.
Exercise 35.7: Technical Debt Assessment (Advanced)
Choose a COBOL program of at least 500 lines (from this course or another source). Perform a comprehensive technical debt assessment:
- Calculate cyclomatic complexity for each paragraph.
- Identify all paragraphs exceeding 50 lines.
- Identify all nesting deeper than 3 levels.
- Find all instances of missing error handling (unchecked I/O, no SIZE ERROR).
- Identify dead code.
- Estimate duplication percentage.
- Assign a remediation time estimate to each issue category.
- Calculate a total technical debt score in person-hours.
- Prioritize the top 5 issues for remediation and justify your ranking.
Present your findings in a one-page summary suitable for a team lead or manager.
Exercise 35.8: Build a Mini Static Analyzer (Advanced)
Write a COBOL program (or a script in your language of choice) that reads a COBOL source file and reports:
- Total lines of code (excluding blank lines and comments)
- Number of paragraphs
- Paragraphs exceeding 50 lines
- Occurrences of GO TO
- IF statements without END-IF (approximate — look for period-terminated IFs)
- COMPUTE statements without ON SIZE ERROR
This is a simplified static analyzer. Test it against at least three different COBOL programs and compare its findings to your manual review.