Search code examples
coding-stylecountercobolcode-standards

Correct way to reset counter in COBOL


I want to know what is the better way to reset a counter. I have the following:

03 WS-SEQ-NO    PIC  9(04).

When WS-SEQ-NO is 9999, and i increment it, or if i move 10000 into it, it will get truncated, and become 0000. This is actually the desired result as i want it to tick over to 0000 after 9999. However, i am interested to know if this is acceptable by COBOL programming standards? Or should i be using an IF condition to reset it. E.g:

IF WS-SEQ-NO = 9999
    MOVE 0 TO WS-SEQ-NO
ELSE
    ADD 1 TO WS-SEQ-NO
END-IF.

Also, this code will only be executed once a month or so, and it is not in a loop, so i'm not desperate to avoid having the additional IF condition. I'm merely wondering if it is 'legal', so to speak, in a programming standards sense, to rely on this COBOL feature that truncates the number rather than coding for it. Thanks!


Solution

  • I would keep the condition and define the counter this way

    01 COUNTERS.
       03 WS-SEQ-NO    PIC  9(04).
          88 MAX-SEQ-NO VALUE 9999.
    ...
    IF MAX-SEQ-NO
        MOVE 0 TO WS-SEQ-NO
    ELSE
        ADD 1 TO WS-SEQ-NO
    END-IF.
    

    If you need to modify your counter to PIC 9(5), you won't forget to modify the condition because you won't have to.

    You'll only need to modify the level 88.