CRITICAL
Rule Definition
File access is generally expensive in terms of run time and especially the OPEN and CLOSE statements. It is highly recommended to avoid accessing to the same file multiple times into a program but rather to save the file into a buffer. Moreover, it is even more important to avoid opening and closing the same file several times. If a file is opened or closed multiple times, for instance into a loop, then this can decrease the application performance dramatically.
Remediation
Open and close files only one time. Put the OPEN and CLOSE statements outside loops.
Violation Code Sample
Loops with PERFORM - TIMES
* In-line (OPEN and CLOSE must not be inside loop)
PERFORM 10 TIMES
OPEN INPUT FILE-02
READ FILE-02
CLOSE FILE-02
END-PERFORM
* Out-of-line (OPEN and CLOSE must not be in the
* paragraph called inside loop)
PROC-TIMES-01.
PERFORM PROC-TIMES-01-A 10 TIMES.
...
PROC-TIMES-01-A.
OPEN INPUT FILE-03
READ FILE-03
CLOSE FILE-03
Loops with PERFORM - UNTIL
* In-line (OPEN and CLOSE must not be inside loop)
PERFORM UNTIL A = 10
OPEN INPUT FILE-05
READ FILE-05
CLOSE FILE-05
END-PERFORM
* Out-of-line (OPEN and CLOSE must not be in the
* paragraph called inside loop)
PROC-UNTIL-01.
MOVE 0 TO A.
PERFORM PROC-UNTIL-01-A UNTIL A = 10.
...
PROC-UNTIL-01-A.
ADD 1 TO A.
OPEN INPUT FILE-06
READ FILE-06
CLOSE FILE-06
Loops with PERFORM - VARYING
* In-line (OPEN and CLOSE must not be inside loop)
PERFORM VARYING A FROM 1 BY 1 UNTIL A = 10
OPEN INPUT FILE-08
READ FILE-08
CLOSE FILE-08
END-PERFORM
* Out-of-line (OPEN and CLOSE must not be in the
* paragraph called inside loop)
PROC-VARYING-01.
PERFORM PROC-VARYING-01-A
VARYING A FROM 1 BY 1 UNTIL A = 10.
...
PROC-VARYING-01-A.
OPEN INPUT FILE-09
READ FILE-09
CLOSE FILE-09
Fixed Code Sample
Loops with PERFORM - TIMES
* In-line
OPEN INPUT FILE-02
PERFORM 10 TIMES
READ FILE-02
END-PERFORM
CLOSE FILE-02
* Out-of-line
PROC-TIMES-01.
OPEN INPUT FILE-03
PERFORM PROC-TIMES-01-A 10 TIMES.
CLOSE FILE-03
...
PROC-TIMES-01-A.
READ FILE-03
Loops with PERFORM - UNTIL
* In-line
OPEN INPUT FILE-05
PERFORM UNTIL A = 10
READ FILE-05
END-PERFORM
CLOSE FILE-05
* Out-of-line
PROC-UNTIL-01.
MOVE 0 TO A.
OPEN INPUT FILE-06
PERFORM PROC-UNTIL-01-A UNTIL A = 10.
CLOSE FILE-06
...
PROC-UNTIL-01-A.
ADD 1 TO A.
READ FILE-06
Loops with PERFORM - VARYING
* In-line
OPEN INPUT FILE-08
PERFORM VARYING A FROM 1 BY 1 UNTIL A = 10
READ FILE-08
END-PERFORM
CLOSE FILE-08
* Out-of-line
PROC-VARYING-01.
OPEN INPUT FILE-09
PERFORM PROC-VARYING-01-A
VARYING A FROM 1 BY 1 UNTIL A = 10.
CLOSE FILE-09
...
PROC-VARYING-01-A.
READ FILE-09
Related Technologies
Cobol
Technical Criterion
CWE-1050 - Excessive Platform Resource Consumption within a Loop
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.