MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

Modern COBOL

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.

What Makes COBOL Modern?

Modern COBOL includes features that enable contemporary application development:

  • Object-oriented programming: Classes, methods, inheritance, and polymorphism (COBOL 2002+)
  • JSON and XML support: Built-in parsing and generation (COBOL 2014)
  • Enhanced string functions: Improved string manipulation capabilities
  • Better exception handling: Try-catch blocks and improved error management
  • Web service integration: Capabilities for REST APIs and SOAP services
  • Unicode support: International character handling
  • Dynamic tables: Flexible array operations
  • Modern data formats: Support for contemporary data exchange

These features enable COBOL to work effectively in modern computing environments while preserving its traditional strengths.

Object-Oriented Programming in COBOL

COBOL 2002 introduced object-oriented programming capabilities:

Defining Classes

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
CLASS-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.

Using Objects

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
WORKING-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.

Inheritance

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
CLASS-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.

JSON Support in Modern COBOL

COBOL 2014 includes built-in JSON support for parsing and generating JSON data:

Parsing JSON

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
WORKING-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.

Generating JSON

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
WORKING-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.

XML Support in Modern COBOL

COBOL 2014 also supports XML parsing and generation:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
WORKING-STORAGE SECTION. 01 xml-string PIC X(300) VALUE '12345John Smith'. 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.

XML support enables COBOL programs to work with XML-based web services and data exchange formats.

Enhanced String Functions

Modern COBOL provides improved string manipulation functions:

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
WORKING-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.

Improved Exception Handling

Modern COBOL provides better exception handling capabilities:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PROCEDURE 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.

Web Service Integration

Modern COBOL can integrate with web services through various mechanisms:

REST API Integration

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
*> 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.

Unicode Support

Modern COBOL includes improved Unicode support for international characters:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
*> 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.

Dynamic Tables

Modern COBOL provides enhanced table (array) handling:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
WORKING-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 Programming Practices

Modern COBOL encourages contemporary programming practices:

  • Modular design: Use classes and methods for better organization
  • Separation of concerns: Separate business logic from data access
  • Error handling: Use structured exception handling
  • Data validation: Validate input and handle edge cases
  • Documentation: Document code, especially modern features
  • Testing: Test modern features thoroughly
  • Integration: Design for integration with modern systems

Backward Compatibility

Modern COBOL maintains full backward compatibility:

  • Traditional code works: COBOL 85 code compiles and runs in modern COBOL
  • Gradual adoption: You can adopt modern features incrementally
  • Mixed styles: Mix object-oriented and procedural code
  • No forced migration: Existing code doesn't need to be rewritten

This allows organizations to modernize at their own pace while preserving existing investments.

When to Use Modern COBOL Features

Consider modern COBOL features when:

  • Integrating with APIs: JSON/XML support simplifies API integration
  • Building new applications: Modern features provide better capabilities
  • Refactoring code: OOP can improve code organization
  • Working with web services: Modern features support web integration
  • Handling modern data formats: JSON/XML support is essential
  • International applications: Unicode support for multiple languages

Best Practices for Modern COBOL

Follow these best practices:

  • Understand compiler support: Verify which modern features your compiler supports
  • Adopt gradually: Introduce modern features incrementally
  • Maintain compatibility: Ensure new code works with existing systems
  • Document modern features: Document use of OOP, JSON/XML, etc.
  • Test thoroughly: Test modern features, especially JSON/XML parsing
  • Train developers: Ensure team understands modern COBOL features
  • Consider performance: Be aware of performance implications of modern features
  • Use appropriate features: Choose modern features that add value
  • Maintain code quality: Apply good programming practices regardless of features used
  • Plan migration: Plan how to adopt modern features in existing codebases

Explain Like I'm 5: Modern COBOL

Think of modern COBOL like upgrading an old car:

  • Traditional COBOL is like an old reliable car - it works great but has basic features
  • Modern COBOL is like adding new features to that car - GPS (JSON/XML), Bluetooth (web services), better engine (OOP)
  • Backward compatibility means the old car still works - you don't have to throw it away
  • New features let you do new things - like connecting to the internet (APIs) or using modern tools
  • Gradual adoption means you can add features one at a time - you don't have to upgrade everything at once

So modern COBOL is like giving an old reliable car new modern features while keeping everything that made it great in the first place!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: JSON Parsing

Create a program that parses a JSON string containing customer information (ID, name, balance) and displays the parsed data.

Exercise 2: JSON Generation

Create a program that generates a JSON string from COBOL data structures representing a product (ID, name, price, quantity).

Exercise 3: Simple Class

Design a simple COBOL class with methods to set and get values. Create an object instance and demonstrate method calls.

Exercise 4: Enhanced String Functions

Write a program that demonstrates various enhanced string functions: UPPER-CASE, LOWER-CASE, REVERSE, LENGTH, and TRIM.

Exercise 5: Modern Integration

Design a program that uses JSON to prepare data for a REST API call, demonstrating how modern COBOL can integrate with web services.

Test Your Knowledge

1. Which COBOL standard introduced object-oriented programming?

  • COBOL 85
  • COBOL 2002
  • COBOL 2014
  • COBOL 74

2. Which COBOL standard added JSON and XML support?

  • COBOL 85
  • COBOL 2002
  • COBOL 2014
  • None of the above

3. Is modern COBOL backward compatible with traditional COBOL?

  • No, you must rewrite all code
  • Yes, traditional COBOL code works in modern COBOL
  • Only partially compatible
  • Depends on the compiler

4. What is a key advantage of modern COBOL?

  • Faster execution
  • Integration with modern systems and data formats
  • Smaller program size
  • Easier syntax

5. Can you mix object-oriented and procedural programming in modern COBOL?

  • No, you must choose one style
  • Yes, you can mix both styles
  • Only in COBOL 2014
  • Only with special compilers

6. What makes COBOL "modern"?

  • New syntax
  • Features for contemporary application development like OOP, JSON/XML, web services
  • Faster compilers
  • Smaller file sizes

Related Concepts

Related Pages