A main loop and a refactor
The last lesson of the part on paragraphs is the change most old COBOL needs before anything else can be done to it: one paragraph that reads, validates, totals and prints, split into paragraphs that each do one thing. The outputs are compared at the end, because a refactor that changes a report is not a refactor.
The paragraph that does everything
Five lines of input in a table, one rejected because its amount holds a letter, four region totals and a grand total, all in one loop:
IDENTIFICATION DIVISION.
PROGRAM-ID. REFOLD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LINES-T.
05 LINE-E OCCURS 5 TIMES.
10 L-REGION PIC X(2).
10 L-AMT PIC X(5).
01 I-F PIC 9 VALUE 0.
01 AMT-N PIC 9(5).
01 N-F PIC 9(5).
01 S-F PIC 9(5).
01 E-F PIC 9(5).
01 W-F PIC 9(5).
01 TOTAL-F PIC 9(6) VALUE 0.
01 GOOD-N PIC 9 VALUE 0.
01 BAD-N PIC 9 VALUE 0.
PROCEDURE DIVISION.
MOVE "N" TO L-REGION(1).
MOVE "01250" TO L-AMT(1).
MOVE "S" TO L-REGION(2).
MOVE "00400" TO L-AMT(2).
MOVE "N" TO L-REGION(3).
MOVE "00X50" TO L-AMT(3).
MOVE "E" TO L-REGION(4).
MOVE "00900" TO L-AMT(4).
MOVE "N" TO L-REGION(5).
MOVE "00300" TO L-AMT(5).
PERFORM VARYING I-F FROM 1 BY 1 UNTIL I-F > 5
IF L-AMT(I-F) IS NUMERIC
MOVE L-AMT(I-F) TO AMT-N
ADD 1 TO GOOD-N
ADD AMT-N TO TOTAL-F
EVALUATE L-REGION(I-F)
WHEN "N" ADD AMT-N TO N-F
WHEN "S" ADD AMT-N TO S-F
WHEN "E" ADD AMT-N TO E-F
WHEN "W" ADD AMT-N TO W-F
WHEN OTHER DISPLAY "line " I-F " unknown region"
END-EVALUATE
DISPLAY "line " I-F " region " L-REGION(I-F)
" amount " AMT-N
ELSE
ADD 1 TO BAD-N
DISPLAY "line " I-F " rejected amount [" L-AMT(I-F) "]"
END-IF
END-PERFORM.
DISPLAY "north " N-F " south " S-F " east " E-F " west " W-F.
DISPLAY "good " GOOD-N " bad " BAD-N " total " TOTAL-F.
STOP RUN.
cobc -x -free -Wall -o refold old.cob
./refold
line 1 region N amount 01250
line 2 region S amount 00400
line 3 rejected amount [00X50]
line 4 region E amount 00900
line 5 region N amount 00300
north 01550 south 00400 east 00900 west 00000
good 4 bad 1 total 002850
Four paragraphs with one job each
The same program with the input in a paragraph of its own, the loop body in another, the rejection in a third and the totals in a fourth:
IDENTIFICATION DIVISION.
PROGRAM-ID. REFNEW.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LINES-T.
05 LINE-E OCCURS 5 TIMES.
10 L-REGION PIC X(2).
10 L-AMT PIC X(5).
01 I-F PIC 9 VALUE 0.
01 AMT-N PIC 9(5).
01 N-F PIC 9(5).
01 S-F PIC 9(5).
01 E-F PIC 9(5).
01 W-F PIC 9(5).
01 TOTAL-F PIC 9(6) VALUE 0.
01 GOOD-N PIC 9 VALUE 0.
01 BAD-N PIC 9 VALUE 0.
PROCEDURE DIVISION.
PERFORM LOAD-LINES.
PERFORM PROCESS-LINE VARYING I-F FROM 1 BY 1 UNTIL I-F > 5.
PERFORM PRINT-TOTALS.
STOP RUN.
LOAD-LINES.
MOVE "N" TO L-REGION(1).
MOVE "01250" TO L-AMT(1).
MOVE "S" TO L-REGION(2).
MOVE "00400" TO L-AMT(2).
MOVE "N" TO L-REGION(3).
MOVE "00X50" TO L-AMT(3).
MOVE "E" TO L-REGION(4).
MOVE "00900" TO L-AMT(4).
MOVE "N" TO L-REGION(5).
MOVE "00300" TO L-AMT(5).
PROCESS-LINE.
IF L-AMT(I-F) IS NOT NUMERIC
PERFORM REJECT-LINE
ELSE
PERFORM POST-AMOUNT
PERFORM PRINT-LINE
END-IF.
REJECT-LINE.
ADD 1 TO BAD-N.
DISPLAY "line " I-F " rejected amount [" L-AMT(I-F) "]".
POST-AMOUNT.
MOVE L-AMT(I-F) TO AMT-N.
ADD 1 TO GOOD-N.
ADD AMT-N TO TOTAL-F.
EVALUATE L-REGION(I-F)
WHEN "N" ADD AMT-N TO N-F
WHEN "S" ADD AMT-N TO S-F
WHEN "E" ADD AMT-N TO E-F
WHEN "W" ADD AMT-N TO W-F
WHEN OTHER DISPLAY "line " I-F " unknown region"
END-EVALUATE.
PRINT-LINE.
DISPLAY "line " I-F " region " L-REGION(I-F)
" amount " AMT-N.
PRINT-TOTALS.
DISPLAY "north " N-F " south " S-F " east " E-F " west " W-F.
DISPLAY "good " GOOD-N " bad " BAD-N " total " TOTAL-F.
cobc -x -free -Wall -o refnew refnew.cob
./refnew
line 1 region N amount 01250
line 2 region S amount 00400
line 3 rejected amount [00X50]
line 4 region E amount 00900
line 5 region N amount 00300
north 01550 south 00400 east 00900 west 00000
good 4 bad 1 total 002850
The output is the proof
Two runs that look the same in a browser are not a proof: one space too many or a line in the wrong place is invisible in a page and obvious to a client. The way to be sure is to write both outputs to files and compare them with diff:
./refold > old.out
./refnew > new.out
diff old.out new.out && echo identical
identical
diff prints nothing when the files are equal, and the && runs the echo only when it succeeds. A refactor of a program that has no golden file is a guess, which is the subject of the group on testing COBOL; the command here is the smallest version of it.
What the refactor keeps
The new program is longer and its main flow is four lines. What it gained is that each name says what a block does, so a change has a place to go: a new region is one WHEN in POST-AMOUNT, a different rejection message is one paragraph, and a reader who wants the totals does not read the validation.
What it kept is the working storage, the loop bound and the order of the lines, because a refactor moves statements and does not change behaviour. The part on paragraphs ends here; the next part is files, where a program stops holding its input in a table and starts reading it from a SELECT and an FD.
Exercises
- Move the input of the new program into a file and read it in LOAD-LINES. Which paragraphs change?
- Add a region "C" to the old program and to the new one, then diff the outputs. Which file had fewer places to change?
- Split POST-AMOUNT into the totals and the region dispatch, and diff again. What does the diff prove?
- Write the comparison as a shell script that builds both, runs both and exits non-zero on a difference.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the safest way to show that a refactor changed nothing?
Answer
Write both outputs to files and compare them with diff
diff compares the bytes, which is the check a reader cannot do by eye for spacing or order.
-
What should a paragraph of a refactored program do?
Answer
One job, named for it
The split gives each block a name that says what it does, so a change has one place to go.
-
What does the new program keep from the old one?
Answer
The working storage, the loop bound and the output order
A refactor moves statements; the data and the behaviour stay, which is why the diff is empty.
-
Why is a browser look not enough to compare two reports?
Answer
Spacing and line order can differ without being visible
The page collapses spaces, and diff compares the exact bytes the client receives.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. PERFORM ONE-P. PERFORM TWO-P. DISPLAY N-F. STOP RUN. ONE-P. ADD 1 TO N-F. TWO-P. ADD 10 TO N-F.Answer
11
The two paragraphs add one and ten to the same field.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 T-T. 05 E-E OCCURS 3 TIMES. 10 E-A PIC 9(2). 01 I-F PIC 9 VALUE 0. 01 S-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. MOVE 5 TO E-A(1). MOVE 6 TO E-A(2). MOVE 7 TO E-A(3). PERFORM TOTAL-UP VARYING I-F FROM 1 BY 1 UNTIL I-F IS GREATER THAN 3. DISPLAY S-F. STOP RUN. TOTAL-UP. ADD E-A(I-F) TO S-F.Answer
018
The performed paragraph sees the loop counter and adds the three entries.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC X(5) VALUE "00120". 01 N-F PIC 9(5) VALUE 0. PROCEDURE DIVISION. IF A-F IS NUMERIC MOVE A-F TO N-F END-IF. DISPLAY N-F. STOP RUN.Answer
00120
The field holds only digits, so the move takes the number and the receiving picture prints it in five digits.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 R-F PIC X VALUE "E". 01 N-F PIC 9(3) VALUE 100. PROCEDURE DIVISION. EVALUATE R-F WHEN "N" DISPLAY "north" WHEN "S" DISPLAY "south" WHEN OTHER DISPLAY "other " N-F END-EVALUATE. STOP RUN.Answer
other 100
The code E is not one of the cases, so WHEN OTHER prints it with the amount.
-
A refactor moves a DISPLAY from the loop into a paragraph. What must be checked?
Answer
Where the paragraph is performed from, so the line still prints once per record
A moved statement keeps its position in the flow only if the call site is the old position.
-
Why is the split worth the extra lines?
Answer
Each block has a name, a single job and a place for the next change
The names are the gain: they tell a reader what a block does without reading its statements.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev