In a previous post ‘The evil of COBOL: everything is in global variables’, I discussed the implementation of a toy complex number library in COBOL.
That example code was a single module, having one paragraph for each function. I used a naming convention to work around the fact that COBOL functions (paragraphs) are completely braindead and have no input nor output parameters, and all such functions in a given loadmodule have access to all the variables of the entire program.
Perhaps you try to call me on my claim that COBOL doesn’t have parameters, nor does it have return values. That’s true if you consider COBOL paragraphs to be the equivalent to functions. I’ve heard paragraphs described as not-really-functions, and there’s some truth to that, especially since you can do a PERFORM range that executes a set of paragraphs, and there can be non-intuitive control flow transfers between paragraphs of such a range of execution, that is entirely non-function like.
There is one circumstance where COBOL parameters can be found. It is actually possible to have both input and output parameters in COBOL, but it can only be done at a program level (i.e.: int main( int argc, char ** )). So, you can write a less braindead COBOL library, with a set of meaningful input and output parameters for each function, by using CALL instead of PERFORM, and a whole set of external programs, one for each of the operations that is desired. With that in mind, I’ve reworked my COBOL complex number toy library to use this program-level organization. This is still a toy library implementation, but serves to illustrate the ideas. The previous paragraph implementation can be found in the same repository, in the ../paragraphs-as-library/ directory.
Here are some examples of the functions in this little library, and examples of calls to them.
Multiply code:
And here’s a call to it:
Notice that I’ve opted to use dynamic calls to the COBOL functions, using a copybook that lists all the possible function names:
This frees me from the constraint of having to use inscrutable 8-character function names, which will get confusing as the library grows.
Like everything in COBOL, the verbosity makes it fairly unreadable, but refactoring all paragraphs into external programs, does make the calling code, and even the library functions themselves, much more readable. It still takes 49 lines of code, to initialize two complex numbers, multiply them and display them to stdout.
Compare to the same thing in C++, which is 18 lines for a grow-your-own complex implementation with multiply:
#include <iostream> struct complex{ double re_; double im_; }; complex mult(const complex & a, const complex & b) { // (a + b i)(c + d i) = a c - b d + i( b c + a d) return complex{ a.re_ * b.re_ - a.im_ * b.im_, a.im_ * b.re_ + a.re_ * b.im_ }; } int main() { complex a{1,2}; complex b{3,4}; complex c = mult(a, b); std::cout << "c = " << c.re_ << " +(" << c.im_ << ") I\n"; return 0; }
and only 11 lines if we use the standard library complex implementation:
#include <iostream> #include <complex> using complex = std::complex<double>; int main() { complex a{1,2}; complex b{3,4}; complex c = a * b; std::cout << "c = " << c << "\n"; return 0; }
Basically, we have one line for each operation: init, init, multiply, display, and all the rest is one-time fluff (the includes, main decl, return, …)
It turns out that the so-called OBJECT oriented COBOL extension to the language (circa Y2K), is basically a packaging of external-style-programs into collections that are class prefixed, just as I’ve done above. This provides the capability for information hiding, and allows functions to have parameters and return values. However, this doesn’t try to rectify the fundamental failure of the COBOL language: everything has to be in a global variable. This language extension appears to be a hack that may have been done primarily for Java integration, which is probably why nobody uses it. You just can’t take the dinosaur out of COBOL.
Sadly, it didn’t take people long to figure out that it’s incredibly dumb to require all variables to be global. Even PL/I, which is 59 years old at the time I write this (only five years younger than COBOL), got it right. They added parameters and return values to functions, and allow functions to have variables that are limited to that scope. PL/I probably went too far, and added lots of features that are also braindead (like the PL/I macro preprocessor), but the basic language is at least sane. It’s interesting that COBOL never evolved. A language like C++ may have evolved too much, and still is, but the most flagrant design flaw in the COBOL language has been there since inception, despite every other language in the world figuring out that sort of stupidity should not be propagated.
Note that I work on the development of a COBOL and PL/I compilation stack. I really like my work, which is challenging and great fun, and I work with awesome people. That doesn’t stop me from acknowledging that COBOL is a language spawned in hell by Satan. I can love my work, which provides tools for customers allowing them to develop, maintain and debug COBOL code, but also have great pity and remorse for those customers, having inherited ancient code built with an ancient language, and having no easy migration path away from that language.