To avoid ABEND, we can do something called defensive programming. It is a form of programming where we defensively design our code to ensure that it is still running under unforeseen circumstances. By doing defensive programming, we can reduce the number of bugs and make the program more predictable, regardless of the inputs.
Listed below are a few things we can do to avoid ABEND in COBOL:
INITIALIZE Fields at the Beginning of a Routine: By using the INITIALIZE statement, you can set all fields to their default initial values, ensuring they start with the correct data at the beginning of the program. However, when using this statement, it's essential to exercise caution and verify that any flags or accumulators dependent on specific initial values are appropriately initialized to avoid potential errors during program execution.
I/O Statement Checking: Implementing I/O statement checking is crucial to handle potential issues related to file operations. This can be achieved by using FILE STATUS variables, which provide information about the success or failure of file-related operations. Before proceeding with any further I/O operation, you should check these FILE STATUS variables to ensure the previous I/O operation was successful.
Numeric Field Checking: A general policy would be to not trust a numeric field we are doing math on. Assume that the input is invalid. It would be recommended to use the ON OVERFLOW and ON SIZE ERROR phrases to catch invalid or abnormal data. Special care should be taken when we need to do rounding, as truncation can occur in some cases.
Consistent Use of Scope Terminators: It would be best practice to explicitly terminate a scope using scope terminators such as END-IF, END-COMPUTE, or END-PERFORM.
Testing, Checking, and Peer-Review: Proper tests and peer review can be conducted to catch possible errors that may have slipped through your program. Additionally, we can also ensure that the business logic is correct.