MainframeMaster

COBOL Comments

Comments in COBOL are essential for documenting code, explaining business logic, and helping future maintainers understand the program's purpose and functionality. Well-written comments make COBOL programs more maintainable, reduce debugging time, and ensure that business rules are clearly documented for future reference.

Understanding COBOL Comments

Comments in COBOL serve as documentation that explains what the code does, why it was written that way, and how it fits into the larger business process. Good comments act as a bridge between the technical implementation and the business requirements, making the code accessible to both programmers and business analysts.

Comment Syntax and Types

1. Full-Line Comments

Full-line comments use an asterisk (*) in column 7 and continue for the entire line. These comments are ideal for section headers, program documentation, and explaining major code blocks.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
IDENTIFICATION DIVISION. PROGRAM-ID. COMMENT-EXAMPLES. AUTHOR. DEVELOPMENT-TEAM. DATE-WRITTEN. 2024-01-15. *> ================================================================ *> PROGRAM PURPOSE: *> This program processes customer account transactions including *> deposits, withdrawals, and transfers. It validates all transactions *> against business rules and maintains audit trails for compliance. *> *> BUSINESS RULES IMPLEMENTED: *> 1. All transactions must have valid account numbers *> 2. Withdrawals cannot exceed available balance *> 3. Savings accounts have minimum balance requirements *> 4. Daily transaction limits apply based on account type *> 5. All transactions are logged for audit purposes *> ================================================================ DATA DIVISION. WORKING-STORAGE SECTION. *> ================================================================ *> CUSTOMER ACCOUNT DATA STRUCTURE *> This structure holds all customer account information needed *> for transaction processing and validation *> ================================================================ 01 CUSTOMER-ACCOUNT-DATA. 05 CUSTOMER-ACCOUNT-NUMBER PIC 9(10). 05 CUSTOMER-ACCOUNT-BALANCE PIC 9(10)V99. 05 CUSTOMER-ACCOUNT-TYPE PIC X(1). *> Account type codes: C=Checking, S=Savings, B=Business 88 CHECKING-ACCOUNT VALUE 'C'. 88 SAVINGS-ACCOUNT VALUE 'S'. 88 BUSINESS-ACCOUNT VALUE 'B'. 05 CUSTOMER-DAILY-LIMIT PIC 9(8)V99. 05 CUSTOMER-MINIMUM-BALANCE PIC 9(8)V99. *> ================================================================ *> TRANSACTION PROCESSING CONTROLS *> These variables control the transaction processing flow and *> track validation results and error conditions *> ================================================================ 01 TRANSACTION-CONTROLS. 05 CURRENT-TRANSACTION-AMOUNT PIC 9(8)V99. 05 TRANSACTION-TYPE PIC X(1). *> Transaction types: D=Deposit, W=Withdrawal, T=Transfer 88 DEPOSIT-TRANSACTION VALUE 'D'. 88 WITHDRAWAL-TRANSACTION VALUE 'W'. 88 TRANSFER-TRANSACTION VALUE 'T'. 05 TRANSACTION-STATUS PIC X(1). 88 TRANSACTION-APPROVED VALUE 'A'. 88 TRANSACTION-REJECTED VALUE 'R'. 05 ERROR-MESSAGE PIC X(100). PROCEDURE DIVISION. *> ================================================================ *> MAIN PROCESSING LOGIC *> This is the primary entry point that orchestrates the entire *> transaction processing workflow *> ================================================================ MAIN-TRANSACTION-PROCESSING. DISPLAY 'Starting Transaction Processing System' *> Initialize all processing variables and validate system state PERFORM INITIALIZE-TRANSACTION-PROCESSING *> Process the transaction through validation and approval steps PERFORM PROCESS-CUSTOMER-TRANSACTION *> Update account balances and create audit records PERFORM UPDATE-ACCOUNT-AND-AUDIT-TRAIL *> Generate processing results and error reports PERFORM GENERATE-TRANSACTION-RESULTS DISPLAY 'Transaction Processing System Completed' STOP RUN.

This example demonstrates effective use of full-line comments for program documentation. The comments explain the program's purpose, business rules, data structures, and processing flow. The section headers with decorative lines make it easy to navigate through different parts of the program, while the detailed explanations provide context for future maintainers.

2. Inline Comments

Inline comments use the *> symbol and can appear anywhere on a line after the code. These comments are perfect for explaining specific variables, clarifying complex logic, and providing context for individual statements.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
INITIALIZE-TRANSACTION-PROCESSING. *> Set up all processing variables with default values MOVE ZERO TO CUSTOMER-ACCOUNT-NUMBER MOVE ZERO TO CUSTOMER-ACCOUNT-BALANCE MOVE 'C' TO CUSTOMER-ACCOUNT-TYPE *> Default to checking account MOVE ZERO TO CURRENT-TRANSACTION-AMOUNT MOVE 'D' TO TRANSACTION-TYPE *> Default to deposit transaction SET TRANSACTION-APPROVED TO TRUE *> Initialize as approved MOVE SPACES TO ERROR-MESSAGE *> Set account-specific limits based on account type *> This implements business rules for different account types PERFORM SET-ACCOUNT-TYPE-LIMITS DISPLAY 'Transaction processing initialized successfully'. SET-ACCOUNT-TYPE-LIMITS. *> Configure daily limits and minimum balances based on account type *> These limits are defined by business policy and regulatory requirements EVALUATE CUSTOMER-ACCOUNT-TYPE WHEN 'C' *> Checking account limits MOVE 5000.00 TO CUSTOMER-DAILY-LIMIT *> $5,000 daily limit MOVE 0.00 TO CUSTOMER-MINIMUM-BALANCE *> No minimum balance WHEN 'S' *> Savings account limits MOVE 10000.00 TO CUSTOMER-DAILY-LIMIT *> $10,000 daily limit MOVE 100.00 TO CUSTOMER-MINIMUM-BALANCE *> $100 minimum balance WHEN 'B' *> Business account limits MOVE 50000.00 TO CUSTOMER-DAILY-LIMIT *> $50,000 daily limit MOVE 1000.00 TO CUSTOMER-MINIMUM-BALANCE *> $1,000 minimum balance WHEN OTHER SET TRANSACTION-REJECTED TO TRUE *> Invalid account type END-EVALUATE. PROCESS-CUSTOMER-TRANSACTION. *> Main transaction processing logic that coordinates all validation *> and processing steps for the current transaction *> First, validate the transaction meets basic requirements PERFORM VALIDATE-TRANSACTION-BASIC-REQUIREMENTS *> If basic validation passes, perform account-specific validations IF TRANSACTION-APPROVED PERFORM VALIDATE-ACCOUNT-SPECIFIC-REQUIREMENTS END-IF *> If all validations pass, process the transaction IF TRANSACTION-APPROVED PERFORM EXECUTE-TRANSACTION-PROCESSING END-IF. VALIDATE-TRANSACTION-BASIC-REQUIREMENTS. *> Validate that the transaction meets fundamental requirements *> such as valid account number, positive amount, etc. IF CUSTOMER-ACCOUNT-NUMBER = 0 SET TRANSACTION-REJECTED TO TRUE MOVE 'Invalid account number' TO ERROR-MESSAGE DISPLAY 'ERROR: Invalid account number' ELSE IF CURRENT-TRANSACTION-AMOUNT <= 0 SET TRANSACTION-REJECTED TO TRUE MOVE 'Transaction amount must be positive' TO ERROR-MESSAGE DISPLAY 'ERROR: Transaction amount must be positive' ELSE SET TRANSACTION-APPROVED TO TRUE DISPLAY 'Basic transaction validation passed' END-IF END-IF. VALIDATE-ACCOUNT-SPECIFIC-REQUIREMENTS. *> Perform validations specific to the account type and transaction *> This includes balance checks, daily limits, and minimum balance *> requirements based on business rules EVALUATE TRANSACTION-TYPE WHEN 'W' *> Withdrawal validations PERFORM VALIDATE-WITHDRAWAL-REQUIREMENTS WHEN 'D' *> Deposit validations (usually minimal) PERFORM VALIDATE-DEPOSIT-REQUIREMENTS WHEN 'T' *> Transfer validations PERFORM VALIDATE-TRANSFER-REQUIREMENTS WHEN OTHER SET TRANSACTION-REJECTED TO TRUE MOVE 'Invalid transaction type' TO ERROR-MESSAGE DISPLAY 'ERROR: Invalid transaction type' END-EVALUATE. VALIDATE-WITHDRAWAL-REQUIREMENTS. *> Validate withdrawal-specific requirements including sufficient *> funds, daily limits, and minimum balance maintenance *> Check if sufficient funds are available IF CURRENT-TRANSACTION-AMOUNT > CUSTOMER-ACCOUNT-BALANCE SET TRANSACTION-REJECTED TO TRUE MOVE 'Insufficient funds for withdrawal' TO ERROR-MESSAGE DISPLAY 'ERROR: Insufficient funds for withdrawal' ELSE *> Check if withdrawal would violate minimum balance requirement IF (CUSTOMER-ACCOUNT-BALANCE - CURRENT-TRANSACTION-AMOUNT) < CUSTOMER-MINIMUM-BALANCE SET TRANSACTION-REJECTED TO TRUE MOVE 'Withdrawal would violate minimum balance' TO ERROR-MESSAGE DISPLAY 'ERROR: Withdrawal would violate minimum balance' ELSE *> Check if withdrawal exceeds daily limit IF CURRENT-TRANSACTION-AMOUNT > CUSTOMER-DAILY-LIMIT SET TRANSACTION-REJECTED TO TRUE MOVE 'Withdrawal exceeds daily limit' TO ERROR-MESSAGE DISPLAY 'ERROR: Withdrawal exceeds daily limit' ELSE SET TRANSACTION-APPROVED TO TRUE DISPLAY 'Withdrawal validation passed' END-IF END-IF END-IF.

This example shows effective use of inline comments that explain specific code elements. The comments clarify business rules, explain the purpose of variables, and provide context for complex validation logic. The inline comments make it easy to understand what each section of code does without having to read through the entire program to understand the business logic.

Comment Best Practices

1. Business Logic Documentation

Comments should explain the business logic behind the code, not just what the code does. This helps future maintainers understand why certain decisions were made and what business rules are being implemented.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
CALCULATE-INTEREST-ON-SAVINGS. *> ================================================================ *> BUSINESS RULE: Interest Calculation for Savings Accounts *> *> Interest is calculated monthly on the average daily balance *> for the previous month. The interest rate varies based on: *> 1. Account balance tier (higher balances get better rates) *> 2. Account age (longer-held accounts get loyalty bonuses) *> 3. Promotional rates (special offers for new customers) *> *> Regulatory Requirements: *> - Interest must be calculated to 4 decimal places *> - Minimum interest payment is $0.01 *> - Interest is compounded monthly and paid quarterly *> ================================================================ *> Get the average daily balance for interest calculation PERFORM GET-AVERAGE-DAILY-BALANCE *> Determine the applicable interest rate based on balance tier PERFORM DETERMINE-INTEREST-RATE *> Apply loyalty bonus for accounts held more than 2 years IF ACCOUNT-AGE-IN-MONTHS > 24 COMPUTE INTEREST-RATE = INTEREST-RATE * 1.10 *> 10% loyalty bonus END-IF *> Calculate monthly interest (balance * rate / 12) COMPUTE MONTHLY-INTEREST = (AVERAGE-DAILY-BALANCE * INTEREST-RATE) / 12 *> Round to 4 decimal places as required by regulation COMPUTE MONTHLY-INTEREST = FUNCTION ROUND(MONTHLY-INTEREST, 4) *> Apply minimum interest payment rule IF MONTHLY-INTEREST < 0.01 MOVE 0.01 TO MONTHLY-INTEREST *> Minimum $0.01 interest END-IF *> Add to quarterly interest accumulator ADD MONTHLY-INTEREST TO QUARTERLY-INTEREST-ACCUMULATOR. DETERMINE-INTEREST-RATE. *> Set interest rate based on account balance tier *> These rates are set by the bank's pricing committee monthly EVALUATE TRUE WHEN AVERAGE-DAILY-BALANCE >= 100000.00 MOVE 0.0250 TO INTEREST-RATE *> 2.50% for $100K+ balances WHEN AVERAGE-DAILY-BALANCE >= 50000.00 MOVE 0.0200 TO INTEREST-RATE *> 2.00% for $50K+ balances WHEN AVERAGE-DAILY-BALANCE >= 10000.00 MOVE 0.0150 TO INTEREST-RATE *> 1.50% for $10K+ balances WHEN AVERAGE-DAILY-BALANCE >= 1000.00 MOVE 0.0100 TO INTEREST-RATE *> 1.00% for $1K+ balances WHEN OTHER MOVE 0.0050 TO INTEREST-RATE *> 0.50% for balances under $1K END-EVALUATE.

This example demonstrates excellent business logic documentation. The comments explain not just what the code does, but why it does it, what business rules are being implemented, and what regulatory requirements must be met. This level of documentation is crucial for maintaining complex business logic and ensuring compliance with regulations.

2. Code Structure Documentation

Comments should also document the overall structure of the program, explaining how different sections work together and what the expected flow of execution is.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
*> ================================================================ *> PROGRAM STRUCTURE OVERVIEW *> *> This program follows a structured approach to transaction processing: *> *> 1. INITIALIZATION PHASE *> - Set up processing variables *> - Validate system environment *> - Open required files *> *> 2. PROCESSING PHASE *> - Read transaction records *> - Validate each transaction *> - Process approved transactions *> - Handle rejected transactions *> *> 3. CLEANUP PHASE *> - Close files *> - Generate reports *> - Clean up resources *> *> ERROR HANDLING STRATEGY: *> - All errors are logged with detailed messages *> - Processing continues for valid transactions *> - Invalid transactions are rejected but logged *> - System errors cause program termination *> ================================================================ PROCEDURE DIVISION. *> ================================================================ *> INITIALIZATION PHASE *> ================================================================ MAIN-PROCESSING. DISPLAY 'Starting Transaction Processing System' *> Initialize all processing variables and validate system state PERFORM INITIALIZE-PROCESSING-ENVIRONMENT *> ================================================================ *> PROCESSING PHASE *> ================================================================ *> Process all transactions in the input file PERFORM PROCESS-ALL-TRANSACTIONS *> ================================================================ *> CLEANUP PHASE *> ================================================================ *> Generate final reports and clean up resources PERFORM GENERATE-FINAL-REPORTS PERFORM CLEANUP-PROCESSING-RESOURCES DISPLAY 'Transaction Processing System Completed' STOP RUN. *> ================================================================ *> INITIALIZATION PROCEDURES *> ================================================================ INITIALIZE-PROCESSING-ENVIRONMENT. *> Set up the processing environment and validate system state PERFORM INITIALIZE-PROCESSING-VARIABLES PERFORM VALIDATE-SYSTEM-ENVIRONMENT PERFORM OPEN-REQUIRED-FILES PERFORM INITIALIZE-ERROR-HANDLING. *> ================================================================ *> PROCESSING PROCEDURES *> ================================================================ PROCESS-ALL-TRANSACTIONS. *> Main processing loop that handles all transactions PERFORM UNTIL END-OF-FILE PERFORM READ-NEXT-TRANSACTION IF NOT-END-OF-FILE PERFORM VALIDATE-TRANSACTION IF TRANSACTION-VALID PERFORM PROCESS-VALID-TRANSACTION ELSE PERFORM HANDLE-INVALID-TRANSACTION END-IF END-IF END-PERFORM. *> ================================================================ *> CLEANUP PROCEDURES *> ================================================================ GENERATE-FINAL-REPORTS. *> Generate summary reports and statistics PERFORM CREATE-PROCESSING-SUMMARY PERFORM CREATE-ERROR-REPORT PERFORM CREATE-AUDIT-TRAIL. CLEANUP-PROCESSING-RESOURCES. *> Clean up all resources and close files PERFORM CLOSE-ALL-FILES PERFORM RELEASE-MEMORY-RESOURCES PERFORM RESET-PROCESSING-FLAGS.

This example shows how to document program structure with clear section headers and flow explanations. The comments explain the overall approach, the phases of processing, and how different sections work together. This type of documentation makes it easy for new developers to understand the program's architecture and locate specific functionality.

Best Practices for COBOL Comments

Common Comment Patterns