Maintainability over decades
A COBOL program that runs a payroll is older than most of the people who maintain it, and it is changed by one field at a time by people who did not write it. The question this page asks is not which language is nicer to write; it is which compiler stops you when a change reaches further than you meant. Two changes, made in both languages, and four builds.
The program before the change
A customer record with an id and a balance, and a program that prints the length of the record, the length of the balance and the value. The COBOL version first:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUST-REC.
05 CUST-ID PIC X(6).
05 CUST-BAL PIC 9(5)V99.
PROCEDURE DIVISION.
MOVE "C00001" TO CUST-ID
MOVE 1234.56 TO CUST-BAL
DISPLAY "record " LENGTH OF CUST-REC " bytes"
DISPLAY "balance " LENGTH OF CUST-BAL " bytes"
DISPLAY "value [" CUST-BAL "]"
STOP RUN.
cobc -x -free -Wall -O2 -o maint maint.cob
./maint
record 13 bytes
balance 7 bytes
value [01234.56]
And the C++ version, with a struct of a character array and an int:
#include <cstdio>
#include <cstring>
struct Customer {
char id[6];
int cents;
};
int main() {
Customer customer{};
std::memcpy(customer.id, "C00001", 6);
customer.cents = 123456;
std::printf("record %zu bytes\n", sizeof(Customer));
std::printf("balance %zu bytes\n", sizeof(customer.cents));
std::printf("value [%d]\n", customer.cents);
return 0;
}
g++ -std=c++23 -Wall -Wextra -O2 -o maint_cpp maint.cpp
./maint_cpp
record 12 bytes
balance 4 bytes
value [123456]
One field renamed
The first change is a rename: the balance becomes CUST-BALANCE in COBOL and amount in C++. The change reaches the declaration and one use, and the declaration is edited first, in both languages. Both compilers stop, and both say which line:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUST-REC.
05 CUST-ID PIC X(6).
05 CUST-BALANCE PIC 9(5)V99.
PROCEDURE DIVISION.
MOVE "C00001" TO CUST-ID
MOVE 1234.56 TO CUST-BAL
DISPLAY "record " LENGTH OF CUST-REC " bytes"
DISPLAY "balance " LENGTH OF CUST-BAL " bytes"
DISPLAY "value [" CUST-BAL "]"
STOP RUN.
cobc -x -free -Wall -O2 -o maint_bad maint_bad.cob
maint_bad.cob:12: error: 'CUST-BAL' is not defined
10 | PROCEDURE DIVISION.
11 | MOVE "C00001" TO CUST-ID
12 > MOVE 1234.56 TO CUST-BAL
13 | DISPLAY "record " LENGTH OF CUST-REC " bytes"
14 | DISPLAY "balance " LENGTH OF CUST-BAL " bytes"
The C++ edit is the same shape and gets the same kind of answer:
#include <cstdio>
#include <cstring>
struct Customer {
char id[6];
int amount;
};
int main() {
Customer customer{};
std::memcpy(customer.id, "C00001", 6);
customer.cents = 123456;
std::printf("record %zu bytes\n", sizeof(Customer));
std::printf("balance %zu bytes\n", sizeof(customer.cents));
std::printf("value [%d]\n", customer.cents);
return 0;
}
g++ -std=c++23 -Wall -Wextra -O2 -o maint_bad maint_bad.cpp
maint_bad.cpp: In function 'int main()':
maint_bad.cpp:12:14: error: 'struct Customer' has no member named 'cents'
12 | customer.cents = 123456;
| ^~~~~
One field widened
The second change is the dangerous one: the balance needs more room, so 9(5)V99 becomes 9(7)V99 in COBOL, and int becomes long long in C++. Both programs still compile, and the length they print tells the story:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUST-REC.
05 CUST-ID PIC X(6).
05 CUST-BAL PIC 9(7)V99.
PROCEDURE DIVISION.
MOVE "C00001" TO CUST-ID
MOVE 1234.56 TO CUST-BAL
DISPLAY "record " LENGTH OF CUST-REC " bytes"
DISPLAY "balance " LENGTH OF CUST-BAL " bytes"
DISPLAY "value [" CUST-BAL "]"
STOP RUN.
cobc -x -free -Wall -O2 -o maint_wide maint_wide.cob
./maint_wide
record 15 bytes
balance 9 bytes
value [0001234.56]
The record is two bytes longer, nothing in the program said so, and every file this program writes or reads with the old layout is now a different file. The COBOL compiler is not at fault: the program is correct, and the interface changed. The lesson that would have caught it is a record-length check in the job, like the one the layout capstone prints.
C++ has the same silence about the size, and this compiler says one thing more, because the program prints the value through printf and the format string is checked:
#include <cstdio>
#include <cstring>
struct Customer {
char id[6];
long long cents;
};
int main() {
Customer customer{};
std::memcpy(customer.id, "C00001", 6);
customer.cents = 123456;
std::printf("record %zu bytes\n", sizeof(Customer));
std::printf("balance %zu bytes\n", sizeof(customer.cents));
std::printf("value [%d]\n", customer.cents);
return 0;
}
g++ -std=c++23 -Wall -Wextra -O2 -o maint_wide maint_wide.cpp
./maint_wide
maint_wide.cpp:15:28: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
record 16 bytes
balance 8 bytes
value [123456]
Sixteen bytes, not twelve: the wider member changed the padding as well as the field. The warning is about the print, not the struct, and it is the kind of help a compiler gives when a type change travels further than the declaration. A COBOL program that displays a widened field gets no warning, because the picture is the display.
Four builds, two stops
| Change | COBOL | C++ |
|---|---|---|
| rename a field | compile error, line and name | compile error, line and member |
| widen a field | builds; record 13 to 15 bytes, value shape changes | builds; record 12 to 16 bytes, a format warning |
Both languages stop on the rename and both let the widening through. The difference this page can show is in the second row: a COBOL program that widens a field changes a file layout that other programs read, and the length is visible in LENGTH OF and in nothing else; a C++ program that widens a struct member changes the size and the padding, and a modern compiler may warn where the value is printed. Neither language has a mechanism that says "this number goes into a file that somebody else reads".
That mechanism is a test, and the one that would have caught both changes is the same in both languages: a golden output with the record length in it, compared on every build. The golden-file page of the testing group builds exactly that for a COBOL report; the C++ side of it is a redirect and diff.
What the two languages differ in, at this distance, is who reads the change. A COBOL shop has programs written in 1987, 1999 and 2015 in one folder, and the 1987 one is a fixed-width file layout with a comment that names its author; the compiler will read it the same way today. A C++ program of the same age is a different problem: the language has moved, the libraries have moved, and a build that worked in 2015 may need a flag, a header or a standard change before it compiles again. That is a real advantage of COBOL and it is not the whole story, because the 1987 COBOL program also has no tests and no owner. The next page is about the first hour a person spends with each language, which is where that difference starts.
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