When COBOL is the right choice
The pages of this group measured a handful of things that a choice between COBOL and C++ actually turns on. This one puts the results in one place and then says where each language is the better tool, including the cases where the honest answer is neither of the two extremes: COBOL is not a museum piece and C++ is not a universal replacement.
What these pages measured
| Question | What the run showed | Page |
|---|---|---|
| Is money exact? | COBOL COMP-3 exact by default; C++ double wrong at the equality (678.33999999999992) and a tenth of a cent short after ten million additions; C++ cents or a decimal type exact by choice | Decimal money |
| Can they share a file? | Yes: a 37-byte record from a packed C++ struct read by COBOL and the other way round, both printing the same five lines | Records and files |
| Which is faster in batch? | Sort and aggregate of 200,000 records: C++ 332 ms minimum, 421 ms median; COBOL 803 ms minimum, 854 ms median, about a factor of two | Batch throughput |
| Which formats a report better? | COBOL pictures give currency, debit and suppression in the declaration; std::format gives widths and precision and this library lacks float grouping | Strings and edited output |
| Which survives a change? | Both compilers stop on a rename and both let a widened field through; the record length changed 13 to 15 and 12 to 16 bytes with no error | Maintainability |
| Which tooling is there? | The same three gates; COBOL has -debug and no sanitizer, C++ has sanitizers and no language-level field checks | Tooling |
Two of those results favour COBOL, two C++, and two are a draw that depends on what the program is for. That is what a fair comparison looks like, and it is why the conclusion below is a list of situations and not a winner.
When COBOL is the right choice
- The program already exists and works. A payroll or a billing run that has closed the month for twenty years is not a candidate for a rewrite on language grounds. It is a candidate for tests, documentation and a maintainer, which is what the testing group and the course are about.
- The data is decimal money at the centre. COBOL gets exact decimal arithmetic without a library and without a rule in a style guide. On the page that measured it, every C++ money program that was right had chosen to be right, and the default was wrong.
- The interface is fixed-width and the partners are banks. The record layout of the second page is the native data structure of COBOL; in C++ it is a struct with an attribute, a serializer and a test that the attribute is still there. Both work, and the COBOL one is cheaper to keep.
- The report is the product. Edited pictures put the currency sign, the debit marker and the zero suppression in the declaration, and every program that moves a number into the field prints the same text. That is a real advantage on a page full of money.
- The machine is a mainframe or the job is a batch step. The language, the files and the scheduler were designed together, and a COBOL program that reads a sorted file and writes a report is the shape the platform expects.
- The code has to compile in 2040. COBOL 85 source compiles today with a compiler that changes once a decade, and the language has no libraries to churn. That stability is the reason the estate exists.
When C++ is the right choice
- The program is new and small. A batch job of two hundred records, a converter, a tool for the team: C++ is faster to write, and the measured throughput advantage is on the side of the language with the better libraries for everything around the job.
- The work is not a report. Network services, binary protocols, parsers, anything with a graph or a plugin boundary: the C++ ecosystem has the libraries, and COBOL has none of them.
- The performance matters and was measured. On this page a factor of two, on a file that fits in memory, with a sort the C++ side wrote itself. A job that runs once a night does not care; a service with a latency budget does, and a rewritten C++ version of a hot COBOL step is a normal and sensible project.
- Memory safety has to be checked. The sanitizers are the only tool in this group that finds a buffer overflow at the line that wrote it. COBOL checks subscripts, which covers the common case and not the whole class.
- The team is C++ and will stay C++. A language nobody on the team can maintain is a liability whatever its arithmetic. The onboarding page is honest about the first hour being a cost in both directions, and the second year is what decides.
When the answer is both
The most common real answer is a mixed system, and the pages of this group show that it works at the byte level. A COBOL batch core behind a C++ or Python front end is the shape of most modernisation projects that succeed: the records are shared (page two), the money stays decimal where it is computed (page one), the heavy sort stays where it was written and measured (page three), and the new interface is written in the language the new team knows.
The C side of that shape is what the C interop lessons build: a COBOL module called from C, a shared object resolved at run time, a record passed by pointer. It is old technology and it still works, which is the point: a modernisation that keeps the working part and replaces the edges is cheaper than a rewrite, and it can be measured one step at a time.
A closing word on legacy
Legacy is not a property of a language. The C++ program of the maintainability page is twelve bytes of struct and four lines of output, and in twenty years it will be a file that nobody dares change for exactly the reasons the COBOL program is one today: no tests, no owner, and a format that other programs read. The COBOL estate is large because it was written when the work was large, not because COBOL is old.
What the language does decide is how expensive the next change is. On this group of pages, COBOL was better at money, at report text and at staying the same; C++ was better at speed, at tooling for memory and at everything that is not a batch file. A team that knows which of those it is buying is in a better position than one arguing about which language is modern, and the numbers on these pages are here so that the argument can start from measurements instead of taste.
Every number in this article comes from a run on Alpine Linux 3.24.3 (x86_64) in a Docker container with nproc 4: cobc (GnuCOBOL) 3.2.0 and g++ (Alpine 15.2.0) 15.2.0, both with -O2 unless a page says otherwise.
Article author: Arthur Isaev