What Is COBOL Programming Language? 

COBOL, which stands for Common Business-Oriented Language, is one of the earliest high-level programming languages. Developed in the late 1950s and early 1960s, COBOL was created to meet the need for a language that could be used across various business, finance, and administrative systems. 

It was developed with readability and straightforward syntax to ensure code could be understood and maintained without expert programming skills. COBOL’s design focuses on business data processing, making it suitable for applications such as payroll, accounting, and inventory systems. 

Unlike other programming languages of its era, COBOL was intended to be both machine-independent and user-friendly, enabling its widespread adoption across different hardware platforms. 

This is part of a series of articles about legacy code.

Why Is COBOL Still Dominant? 

Despite being over six decades old, COBOL continues to hold a dominant position in enterprise environments due to its reliability and efficiency. Many legacy systems, particularly those in financial institutions, government agencies, and large corporations, still operate on COBOL-based systems. 

These systems handle vast amounts of critical transactions daily and have remained useful over time, leading organizations to favor maintaining their COBOL applications over transitioning to newer technologies.

Another reason for COBOL’s continued use is the immense cost and complexity associated with migrating existing systems to modern languages. Rewriting or replacing COBOL systems would require significant resources and carry risks of disrupting essential operations. 

Additionally, a large body of domain-specific knowledge and optimized COBOL code exists, making it easier for organizations to maintain. 

Related content: Read our guide to COBOL migration

Understanding the COBOL Syntax 

COBOL syntax is meant to be readable and simple, closely resembling natural English. This allows developers with varying levels of programming expertise to understand and maintain COBOL programs. COBOL’s syntax is characterized by its verbose nature, where clarity and explicitness are prioritized.

Basic elements of the COBOL syntax include:

  1. Divisions, sections, and paragraphs: COBOL programs are divided into hierarchical units known as divisions, sections, and paragraphs. Each division serves a different purpose and organizes the code logically.
  2. Statements and sentences: COBOL statements are instructions that perform actions, and multiple statements can form a sentence. Sentences are terminated with a period (.) and are contained within paragraphs.
  3. Verbs: COBOL uses English-like verbs such as MOVE, ADD, DISPLAY, and PERFORM. These verbs make the actions of the code clear and intuitive.
  4. Clauses and phrases: Clauses and phrases provide additional details and modify statements. For example, the TO clause in the MOVE statement specifies the target variable.

Key syntax rules include:

  • Columns and formatting: Traditional COBOL requires code to be written within specified columns (e.g., columns 8-72 for code). Modern COBOL relaxes these rules but maintaining consistent formatting enhances readability.
  • Reserved words: COBOL has a set of reserved words that should not be used as variable names. These include IF, THEN, ELSE, PERFORM, MOVE, etc.
  • Case insensitivity: COBOL syntax is case-insensitive, meaning MOVE and move are treated the same.
Tips from the expert→ Omer Rosenbaum, CTO & Co-founder at Swimm

In my experience, here are tips that can help you better master COBOL programming:

1. Use EVALUATE for control flow: Instead of relying heavily on nested IF statements, use the EVALUATE statement. It’s COBOL’s equivalent of a switch-case structure found in other languages, allowing for cleaner and more readable decision-making logic.

2. Use level numbers effectively for data structure clarity: COBOL’s level numbers (e.g., 01, 05, 10) allow you to create nested data structures. Use this feature to group related fields logically and represent hierarchical data, making the Data Division easier to understand and modify.

3. Adopt structured program design using paragraphs: Break down large COBOL programs into small, reusable paragraphs. This modular approach aligns with structured programming principles and makes your codebase easier to maintain and extend.

4. Implement version control for copybooks: Given the reusability of copybooks, ensure they are versioned and managed carefully. Implement a version control system that tracks changes to copybooks to maintain consistency and compatibility across programs.

5. Invest in understanding COBOL file handling: COBOL’s file handling capabilities are extensive and often underutilized. Spend time mastering indexed files, relative files, and sequential files to leverage COBOL’s full potential in handling large datasets efficiently. Understanding file I/O performance implications can significantly optimize your applications.

COBOL Program Structure 

COBOL’s structured programming approach emphasizes readability and modularity. The code structure consists of four main divisions, which help in organizing the program logically and systematically. 

Identification Division

The Identification division provides metadata about the program. This division contains program information such as the program name, author, date written, and other documentary attributes. It’s important for understanding the program’s purpose and origin.

Environment Division

The Environment division specifies the configuration and conditions under which the program will run. This division includes details about the hardware and software environments, such as the computer system being used and any necessary file assignments. It is crucial for ensuring that the program operates correctly across different systems.

This division is often divided into two sections: Configuration section and Input-Output section. The Configuration section mentions the hardware and operating system specifics, while the Input-Output section lists file controls and devices.

Data Division

In the Data division, all the variables and data structures used by the program are declared. This division is important for data handling as it ensures consistent and clear data usage throughout the program. 

It is divided into multiple sections, with the Working-Storage section being one of the most important, where general-purpose variables are defined. Another important section is the File section, where file structures are declared. 

Procedure Division

The Procedure division contains the code that defines the program’s operations. It includes a series of procedural statements and instructions that manipulate data and control the program flow. 

This division is where business logic is implemented, and it includes various sections and paragraphs, making the code modular and easier to manage. Each paragraph can contain one or more COBOL statements, following a logical sequence to perform the requisite tasks. 

Tutorial: Creating Your First COBOL Program 

Creating your first COBOL program involves setting up the necessary environment and writing a simple “Welcome, Friend” program. Follow the steps below to get started.

Step 1: Set Up Your Environment

First, download and install Visual Studio Code (VS Code). VS Code is a versatile code editor that supports many programming languages, including COBOL. 

After installing VS Code, add COBOL extensions. Install the bitlang.cobol extension for COBOL syntax highlighting and the Broadcom COBOL Language Support extension for additional COBOL language support.

Next, install GnuCOBOL, a free, open-source COBOL compiler. You can install it using Homebrew on macOS with the command:

brew install gnu-cobol

For Windows, you can use the MicroFocus trial for Visual Studio COBOL or explore other installation options.

Step 2: Write Your First COBOL Program

Open VS Code and create a new file named welcome.cob. Enter the following code into your welcome.cob file:

IDENTIFICATION DIVISION.

PROGRAM-ID. WELCOME.

PROCEDURE DIVISION.

`DISPLAY "Welcome, Friend".`

END PROGRAM WELCOME.

Step 3: Understanding the COBOL Program Structure

COBOL programs are divided into divisions, each serving a specific purpose:

IDENTIFICATION DIVISION: This division provides metadata about the program. For example:

IDENTIFICATION DIVISION.

PROGRAM-ID. WELCOME.

AUTHOR. YOUR-NAME.

DATE-WRITTEN. 2024-08-08.

PROCEDURE DIVISION: This division contains the executable code. For example:

PROCEDURE DIVISION.

`DISPLAY "Welcome, Friend".`

END PROGRAM WELCOME.

Step 4: Compile and Run the Program

Open a terminal and navigate to the directory containing your welcome.cob file. Run the following command to compile the program using GnuCOBOL:

cobc -x welcome.cob

Execute the compiled program with:

./welcome

You should see the output:

Welcome, Friend

Best Practices for COBOL Programming 

Adopting best practices in COBOL programming aids in writing efficient and maintainable code.

Use Verbose Syntax

Verbose syntax ensures the COBOL code is clear and self-explanatory. Using descriptive and explicit syntax aids developers in understanding the operations without ambiguity. It helps both current developers and future maintainers by making the code easily readable and understandable.

While verbosity might seem excessive compared to other languages, it aligns with COBOL’s design philosophy of being user-friendly and readable. Detailed syntax decreases the likelihood of errors and enhances documentation.

Use Copybooks

Copybooks in COBOL allow reusable code segments, supporting modularity and reducing redundancy. Similar to modern programming’s INCLUDE  files or libraries, copybooks speed up development by incorporating pre-tested code sections. This reuse ensures standardization and minimizes the likelihood of errors.

Using copybooks helps promote consistency across various programs. It saves time in development and simplifies maintenance. When updates or bug fixes are needed, changes can be made in a single copybook rather than in multiple program files.

Minimize the Use of GO TO Statements

Excessive use of GO TO statements leads to “spaghetti code,” where the program flow becomes hard to follow and maintain. Reducing the use of GO TO statements improves the clarity of the code, making it easier to debug and extend.

Structured programming techniques, like breaking the code into smaller, manageable paragraphs and using PERFORM statements, should be adopted. This ensures a clear, logical flow of control within the program.

Optimize Data Division

Optimizing the Data division involves careful planning and organization of data structures and variables. Properly defining data items reduces memory usage and improves processing efficiency. It also ensures that data is easily accessible and modifiable throughout the program.

Explicit naming conventions for variables and clear structuring within the Data division improve readability and maintainability. This superior data management helps simplify business processes, reducing execution time and resource consumption.

Documenting legacy code with Swimm

Legacy code represents a significant challenge as well as an opportunity for software organizations. Managing legacy code entails more than just dealing with untidy or outdated code; it involves transforming it into a reliable and efficient foundation that supports future development initiatives. Handling legacy code effectively can lead to considerable long-term savings and heightened productivity, marking it as a strategic priority for any R&D organization. 

Swimm is a devtool that will help you and your team document legacy code in an easy, manageable, and effective way. Utilize AI to create docs about legacy code and then discover those docs directly in your IDE. Those docs then stay up to date with ongoing code changes, boosting your team’s productivity and enhancing the quality of your codebase.