Modern COBOL refers to contemporary COBOL programming using features from COBOL 2002 and COBOL 2014 standards. These modern features include object-oriented programming, JSON and XML support, enhanced string functions, improved exception handling, web service capabilities, Unicode support, and modern programming practices. Modern COBOL maintains full backward compatibility with traditional COBOL while adding capabilities for contemporary application development, integration with modern systems, and support for current data formats and communication protocols.
Modern COBOL includes features that enable contemporary application development:
These features enable COBOL to work effectively in modern computing environments while preserving its traditional strengths.
COBOL 2002 introduced object-oriented programming capabilities:
12345678910111213141516171819202122232425262728293031CLASS-ID. CustomerClass. ENVIRONMENT DIVISION. CONFIGURATION SECTION. REPOSITORY. CLASS CustomerClass. DATA DIVISION. WORKING-STORAGE SECTION. 01 customer-name PIC X(30). 01 customer-id PIC 9(5). PROCEDURE DIVISION. METHOD-ID. set-customer-name. DATA DIVISION. LINKAGE SECTION. 01 name-param PIC X(30). PROCEDURE DIVISION USING name-param. MOVE name-param TO customer-name END METHOD set-customer-name. METHOD-ID. get-customer-name. DATA DIVISION. LINKAGE SECTION. 01 name-result PIC X(30). PROCEDURE DIVISION RETURNING name-result. MOVE customer-name TO name-result END METHOD get-customer-name. END CLASS CustomerClass.
This example defines a CustomerClass with methods to set and get customer names, demonstrating encapsulation in COBOL.
1234567891011121314151617181920WORKING-STORAGE SECTION. 01 customer-obj OBJECT REFERENCE CustomerClass. 01 customer-name PIC X(30). PROCEDURE DIVISION. MAIN-PARA. *> Create object instance INVOKE CustomerClass "new" RETURNING customer-obj *> Call method to set name INVOKE customer-obj "set-customer-name" USING BY CONTENT "JOHN SMITH" *> Call method to get name INVOKE customer-obj "get-customer-name" RETURNING customer-name DISPLAY "Customer: " customer-name STOP RUN.
1234567891011121314151617181920212223CLASS-ID. PremiumCustomerClass INHERITS CustomerClass. *> PremiumCustomerClass inherits from CustomerClass *> and can add additional methods and data DATA DIVISION. WORKING-STORAGE SECTION. 01 discount-rate PIC V999 VALUE 0.10. PROCEDURE DIVISION. METHOD-ID. calculate-discount. DATA DIVISION. LINKAGE SECTION. 01 amount-param PIC 9(8)V99. 01 discount-result PIC 9(8)V99. PROCEDURE DIVISION USING amount-param RETURNING discount-result. COMPUTE discount-result = amount-param * discount-rate END METHOD calculate-discount. END CLASS PremiumCustomerClass.
Inheritance allows creating specialized classes that extend base classes, promoting code reuse and organization.
COBOL 2014 includes built-in JSON support for parsing and generating JSON data:
12345678910111213141516171819WORKING-STORAGE SECTION. 01 json-string PIC X(200) VALUE '{"customerId":12345,"name":"John Smith","balance":1000.50}'. 01 customer-data. 05 customer-id PIC 9(5). 05 customer-name PIC X(30). 05 customer-balance PIC 9(8)V99. PROCEDURE DIVISION. MAIN-PARA. *> Parse JSON into COBOL data structure JSON PARSE json-string INTO customer-data DISPLAY "Customer ID: " customer-id DISPLAY "Name: " customer-name DISPLAY "Balance: " customer-balance STOP RUN.
JSON PARSE converts JSON strings into COBOL data structures, making it easy to work with JSON data from APIs.
1234567891011121314151617WORKING-STORAGE SECTION. 01 customer-data. 05 customer-id PIC 9(5) VALUE 12345. 05 customer-name PIC X(30) VALUE 'JOHN SMITH'. 05 customer-balance PIC 9(8)V99 VALUE 1000.50. 01 json-output PIC X(200). PROCEDURE DIVISION. MAIN-PARA. *> Generate JSON from COBOL data JSON GENERATE json-output FROM customer-data DISPLAY "JSON: " json-output *> Output: {"customerId":12345,"name":"JOHN SMITH","balance":1000.50} STOP RUN.
JSON GENERATE creates JSON strings from COBOL data structures, enabling COBOL programs to produce JSON for APIs and web services.
COBOL 2014 also supports XML parsing and generation:
1234567891011121314151617WORKING-STORAGE SECTION. 01 xml-string PIC X(300) VALUE ''. 01 customer-xml. 05 customer-id PIC 9(5). 05 customer-name PIC X(30). PROCEDURE DIVISION. MAIN-PARA. *> Parse XML into COBOL data XML PARSE xml-string INTO customer-xml DISPLAY "Parsed XML - ID: " customer-id DISPLAY "Name: " customer-name STOP RUN. 12345 John Smith
XML support enables COBOL programs to work with XML-based web services and data exchange formats.
Modern COBOL provides improved string manipulation functions:
12345678910111213141516171819202122232425262728WORKING-STORAGE SECTION. 01 text-field PIC X(50) VALUE 'Hello World'. 01 result-field PIC X(50). 01 text-length PIC 9(4). PROCEDURE DIVISION. MAIN-PARA. *> Convert to uppercase MOVE FUNCTION UPPER-CASE(text-field) TO result-field DISPLAY "Uppercase: " result-field *> Convert to lowercase MOVE FUNCTION LOWER-CASE(text-field) TO result-field DISPLAY "Lowercase: " result-field *> Reverse string MOVE FUNCTION REVERSE(text-field) TO result-field DISPLAY "Reversed: " result-field *> Get length COMPUTE text-length = FUNCTION LENGTH(text-field) DISPLAY "Length: " text-length *> Trim spaces MOVE FUNCTION TRIM(text-field) TO result-field DISPLAY "Trimmed: " result-field STOP RUN.
Enhanced string functions simplify common text manipulation tasks and reduce the need for custom code.
Modern COBOL provides better exception handling capabilities:
1234567891011121314151617181920PROCEDURE DIVISION. MAIN-PARA. *> Try-catch style exception handling (conceptual) PERFORM PROCESS-DATA ON EXCEPTION DISPLAY "Error occurred during processing" PERFORM HANDLE-ERROR END-PERFORM STOP RUN. PROCESS-DATA. *> Code that might raise exceptions IF ERROR-CONDITION RAISE EXCEPTION END-IF. HANDLE-ERROR. *> Error handling code DISPLAY "Error handled".
Improved exception handling makes error management more structured and maintainable.
Modern COBOL can integrate with web services through various mechanisms:
1234567891011121314151617181920212223*> Using JSON for REST API communication WORKING-STORAGE SECTION. 01 api-request. 05 request-data PIC X(200). 01 api-response PIC X(500). 01 customer-json PIC X(200). PROCEDURE DIVISION. MAIN-PARA. *> Prepare JSON request JSON GENERATE customer-json FROM customer-data *> Call REST API (implementation varies) CALL "HTTP-POST" USING BY REFERENCE api-url BY REFERENCE customer-json BY REFERENCE api-response END-CALL *> Parse JSON response JSON PARSE api-response INTO response-data STOP RUN.
JSON support makes REST API integration straightforward in modern COBOL.
Modern COBOL includes improved Unicode support for international characters:
1234567891011121314*> Unicode string handling (conceptual) WORKING-STORAGE SECTION. 01 unicode-text PIC N(50). 01 ascii-text PIC X(50). PROCEDURE DIVISION. MAIN-PARA. *> Work with Unicode characters MOVE "Hello 世界" TO unicode-text *> Convert between Unicode and ASCII *> (Implementation details vary by compiler) STOP RUN.
Unicode support enables COBOL programs to handle international characters and text in multiple languages.
Modern COBOL provides enhanced table (array) handling:
1234567891011121314151617181920WORKING-STORAGE SECTION. 01 table-size PIC 9(4) VALUE 100. 01 customer-table. 05 customer-entry OCCURS 1 TO 1000 TIMES DEPENDING ON table-size INDEXED BY customer-index. 10 customer-id PIC 9(5). 10 customer-name PIC X(30). PROCEDURE DIVISION. MAIN-PARA. *> Dynamic table with variable size SET customer-index TO 1 MOVE 12345 TO customer-id(customer-index) MOVE 'JOHN SMITH' TO customer-name(customer-index) *> Resize table dynamically ADD 50 TO table-size STOP RUN.
Dynamic tables provide flexibility for handling variable amounts of data.
Modern COBOL encourages contemporary programming practices:
Modern COBOL maintains full backward compatibility:
This allows organizations to modernize at their own pace while preserving existing investments.
Consider modern COBOL features when:
Follow these best practices:
Think of modern COBOL like upgrading an old car:
So modern COBOL is like giving an old reliable car new modern features while keeping everything that made it great in the first place!
Complete these exercises to reinforce your understanding:
Create a program that parses a JSON string containing customer information (ID, name, balance) and displays the parsed data.
Create a program that generates a JSON string from COBOL data structures representing a product (ID, name, price, quantity).
Design a simple COBOL class with methods to set and get values. Create an object instance and demonstrate method calls.
Write a program that demonstrates various enhanced string functions: UPPER-CASE, LOWER-CASE, REVERSE, LENGTH, and TRIM.
Design a program that uses JSON to prepare data for a REST API call, demonstrating how modern COBOL can integrate with web services.
1. Which COBOL standard introduced object-oriented programming?
2. Which COBOL standard added JSON and XML support?
3. Is modern COBOL backward compatible with traditional COBOL?
4. What is a key advantage of modern COBOL?
5. Can you mix object-oriented and procedural programming in modern COBOL?
6. What makes COBOL "modern"?