Onboarding
A team that has to take over a COBOL system usually has one C++ or Java programmer and six months. The question this page asks is what that person meets in the first hour, because the first hour decides whether the rest of the year is spent reading or fighting. Two hello programs, the same six lines of output, and what each language makes you decide before it will run them.
Hello in both
The COBOL program has the four divisions, a name of twenty characters and a loop over a two-digit counter:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC X(20) VALUE "Ada".
01 WS-COUNT PIC 9(2) VALUE 3.
PROCEDURE DIVISION.
DISPLAY "hello " FUNCTION TRIM(WS-NAME)
PERFORM VARYING WS-COUNT FROM 1 BY 1 UNTIL WS-COUNT > 5
DISPLAY "line " WS-COUNT
END-PERFORM
STOP RUN.
cobc -x -free -Wall -O2 -o hello hello.cob
./hello
hello Ada
line 01
line 02
line 03
line 04
line 05
The C++ program does the same work with a std::string and a for loop:
#include <cstdio>
#include <string>
int main() {
std::string name = "Ada";
std::printf("hello %s\n", name.c_str());
for (int count = 1; count < 6; ++count) {
std::printf("line %d\n", count);
}
return 0;
}
g++ -std=c++23 -Wall -Wextra -O2 -o hello_cpp hello.cpp
./hello_cpp
hello Ada
line 1
line 2
line 3
line 4
line 5
What the first hour is spent on
The two programs are the same size and the C++ one has fewer lines that do nothing. The difference is in what the languages make you decide before either runs. A C++ programmer starting COBOL spends the first hour on three questions:
- Where is the data? COBOL has a data division before the procedure division, and the shape of every field is declared before it is used. That is a fixed cost in the first hour and the reason the fifth hour is easier: the program tells you what every number is.
- Where does the source start? Free format or fixed format is a property of the file, not of the language, and the two are different enough that a new reader has to be told which one the file is. The free and fixed lesson is the two forms side by side.
- What are the verbs? COBOL has about forty that matter, and they are English words with sub-clauses: PERFORM VARYING ... UNTIL ..., ADD ... GIVING ... ON SIZE ERROR. A C++ programmer reads them faster than the punctuation of C++ after a day, and writes them slower for a week.
A COBOL programmer starting C++ spends the first hour on the opposite questions: which header declares what, why the same line compiles on one machine and not another, and what a pointer owns. Neither list is longer; they are different lists.
The field has a shape
The clearest single difference between the two programs is the loop counter. In C++ an int prints as digits with no leading zero, and a report that needs 01 formats it at the print. In COBOL the counter is PIC 9(2), it holds exactly two digits, and DISPLAY prints both of them; the field is the format.
That property is the whole of the language in miniature. It makes the first week slower, because every value has a declared shape and a MOVE between two shapes is a rule to learn, and it makes maintenance faster for the same reason: a report field does not carry a format string that somebody can change in another file, and a file layout is the declaration, not a convention.
The toolchain each one needs
task COBOL C++
compiler cobc (GnuCOBOL) g++ or clang++
hello cobc -x -free -Wall hello.cob g++ -std=c++23 hello.cpp
syntax check cobc -fsyntax-only g++ -fsyntax-only
listing cobc -t (a .lst file) g++ -S (assembly)
debug build cobc -g -debug g++ -g
debugger gdb on the generated C gdb
runtime checks cobc -debug (subscript, CALL) the sanitizers
build file make + a .cob rule make + CMake
test framework none in the image none in the language
The two toolchains are the same shape: a compiler, a listing, a debug build, gdb and make. A C++ programmer does not have to learn a new kind of tool to work on COBOL, which is the part of the onboarding story that rarely gets said. The cobc options and gdb lessons are the long form of two lines of that table.
What COBOL does not have here is a test framework, and neither does this C++ image: the testing group builds the harness, the runner and the golden file by hand for COBOL, and a C++ project brings Catch2 or GoogleTest or writes the same small harness. On this ground the two languages are equally bare, which is worth knowing before a manager is told that one of them is modern.
What to read first
A C++ programmer with a COBOL system to maintain should read, in this order: the four divisions, the picture clause, the file section and the call. Those four are the whole of what a batch program is made of. The COBOL course puts them in the first ten lessons. A COBOL programmer with a C++ codebase to read should start with the build system, not the language: knowing which file is compiled with which flags answers more questions about a legacy program than any chapter on templates.
The next page compares the tooling itself, flag by flag, with the commands and the output each one gives on this machine.
The COBOL was built with cobc (GnuCOBOL) 3.2.0 and the C++ with g++ (Alpine 15.2.0) 15.2.0, both with -O2, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container with nproc 4.
Article author: Arthur Isaev