Exploring COBOL: The Unseen Backbone of Financial Systems

The Common Business Oriented Language, more commonly known as COBOL, boasts a rich and significant history in the world of programming. Even today, it serves as a critical foundation in the financial sector, supporting banks, businesses, and financial institutions globally. Remarkably, COBOL operates largely behind the scenes, quietly powering essential transactions on mainframes and other high-reliability computing systems. Despite its importance, COBOL often fails to capture the excitement or attention that other more glamorous programming languages receive. Its reputation as a 'boring' programming language, primarily focused on business applications, seems to overshadow its capabilities.
Nonetheless, COBOL is a remarkably effective language for tasks such as data transactions and report generation. Its narrow focus on business applications allows developers to get started with minimal hassle. The language is highly self-documenting, which means that the code tends to be understandable and maintainable. Additionally, COBOL is particularly suited for handling decimal calculations and offers extensive input/output (I/O) access and support for various database types, even when dealing with simple files. Since the 2002 update, COBOL has undergone several modern enhancements, including free-form coding, object-oriented programming capabilities, and more.
In this article, we will explore how to set up an open-source COBOL toolchain and delve into a brief COBOL tutorial.
Spoiled For Choice
In the past, engaging with COBOL required either access to a mainframe system running OS/360 or attempting to set up a mainframe emulator, such as Hercules, which became an option starting in 1999. However, the landscape changed dramatically in 2002 with the introduction of GnuCOBOL, originally known as OpenCOBOL. This tool translates COBOL into C code, which is then compiled into a binary format. While GnuCOBOL is functional, it is not technically a compiler and does not claim strict adherence to standards, despite performing well against the NIST test suite.
Fortunately, the GNU Compiler Collection (GCC) has recently unveiled a new COBOL frontend (gcobol) in its 15.1 release. Presently, it is limited to Linux operating systems, but users of other systems can still access it if their preferred distributions include it in their repositories. Windows users can also run GnuCOBOL through the Windows Subsystem for Linux (WSL) or utilize it with MSYS2.
With either compiler installed, you are prepared to start writing COBOL programs. The best part is that we can avoid the complexities of Job Control Language (JCL), which is often deemed daunting, especially for those familiar with IBM OS/360 systems. Instead, we can utilize GCC or GnuCOBOL in various ways, including direct command-line calls, using a Makefile, or integrating the process into an Integrated Development Environment (IDE) if that suits your workflow.
Hello COBOL
To familiarize ourselves with COBOL, let's start with the classic 'Hello World' program, a common first step when learning a new programming language:
IDENTIFICATION DIVISION. PROGRAM-ID. hello-world. PROCEDURE DIVISION. DISPLAY 'Hello, world!'. STOP RUN.
By saving this code in a file named hello_world.cob
, we can compile it using GnuCOBOL like this: cobc -x -free hello_world.cob
. Here, the -x
flag indicates that we want to generate an executable binary, while -free
specifies that we are using free-format code, allowing us more flexibility in how we structure our program (no strict column usage or sequence numbers). Furthermore, we are permitted to write keywords in lowercase, although many developers prefer uppercase for clarity.
From this simple example, we can glean critical elements of COBOL programming. The Identification Division contains the program ID, alongside optional elements such as the author's name. The actual code resides in the Procedure Division, where we find a single display statement that outputs our string. It is also worth noting that each statement ends with a period (.) to signal termination.
Finally, 'STOP RUN' indicates the conclusion of the application, halting execution even when called from a subprogram.
Hello Data
While a 'Hello World' example is a fun introduction, it doesnt provide much insight into COBOLs extensive features. The true power of COBOL shines when we examine its unique aspects, particularly those that solidify its relevance in todays programming landscape.
For instance, few programming languages offer built-in support for decimal (fixed point) calculations. In a project focused on COBOL Basics, I documented various examples showcasing this feature and others. The main modification to our basic structure is the inclusion of the Data Division, which follows the Identification Division:
DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 99V99 VALUE 10.11. 01 B PIC 99V99 VALUE 20.22. 01 C PIC 99V99 VALUE 00.00. 01 D PIC $ZZZZV99 VALUE 00.00. 01 ST PIC $*(5).99 VALUE 00.00. 01 CMP PIC S9(5)V99 USAGE COMP VALUE 04199.04. 01 NOW PIC 99/99/9(4) VALUE 04102034.
As expected, the Data Division is where we define the variables that the program will utilize. All variables are declared within the Working-Storage Section. Although the format may appear overwhelming, it can be broken down easily. Each variable name begins with a two-digit data level indicator that structures the data hierarchy, with 01 representing the root levelup to 49 levels can be defined.
The next component is the variable name, which can contain up to 30 characters, followed by the PICTURE (or PIC) clause. This clause defines the data type and size of an elementary data item. For example, to designate a decimal value, we can use two numeric characters (represented by 9) for the whole portion, followed by an implied decimal point (V), and two digits for the fractional part (99). We can also utilize shorthand notations, such as S9(5) for signed values with five numeric characters. Special characters like an asterisk can replace leading zeroes, and Z can suppress zeroes.
The value clause effectively assigns a specified value to the variable indicated. However, one must be cautious, as demonstrated by the NOW variable, which, due to its PIC format, transforms into a formatted date (04/10/2034).
In the Procedure Division, these variables undergo various arithmetic operationsaddition (ADD A TO B GIVING C), subtraction (SUBTRACT A FROM B GIVING C ROUNDED), multiplication (MULTIPLY A BY CMP), and division (DIVIDE CMP BY 20 GIVING ST).
Additionally, COBOL supports different internal data formats defined by the USAGE clause, including computational (COMP) and display (the default). The COMP format stores data as binary, occupying a variable number of bytes, much like char, short, and int types in C. These internal formats are particularly advantageous for conserving space and enhancing calculation speed.
Hello Business
In a previous publication, I explored the reasons why a domain-specific language like COBOL cannot be effectively replaced by a general-purpose programming language. I also introduced the Hello Business project, which I developed in COBOL to gain hands-on experience with the language. This project could serve as a straightforward reference point based on the information presented thus far. Most new elements revolve around file I/O, loops, the use of the PERFORM statement, and the Report Writer. To best understand these features, it may be beneficial to consult the IBM Report Writer Programmers Manual (PDF).
Diving into the entirety of the code line by line would necessitate another article, so I will leave it as an exercise for interested readers unless there is significant demand for further COBOL tutorial articles.
In summary, COBOL offers a wealth of functionality that goes beyond these basics. Resources such as the IBM ILE COBOL reference (PDF), the IBM Mainframer COBOL tutorial, and the Wikipedia entry provide in-depth insights into many advanced features. These include object-oriented COBOL, database access, heap allocation, and interaction with other programming languages.