DEV Community

loading...
Cover image for Write COBOL in 2020

Write COBOL in 2020

Kelvin
Software Engineer | DevOps Engineer | QA Automation
・5 min read

Motivation

Recently i've read article about Urgent need of Cobol Programmers in the midst of COVID-19. I've surfed the internet to know more about the language. Based on the answer from internet a lot of people said ancient language like COBOL, PASCAL is bad. I decide to found out what's writing COBOL program looks like from developer who usually wrote program using much modern language (Python or Javascript).

What is COBOL?

COBOL is abbrevation for 'Common Business Oriented Language'. It was introduced since 1959 (61 Years Ago). COBOL was designed by CODASYL. It was created as part of a US Department of Defense effort to create a portable programming language for data processing. In the late 1950s, computer users and manufacturers were becoming concerned about the rising cost of programming. A 1959 survey had found that in any data processing installation, the programming cost US$800,000 on average and that translating programs to run on new hardware would cost $600,000. At a time when new programming languages were proliferating at an ever-increasing rate, the same survey suggested that if a common business-oriented language were used, conversion would be far cheaper and faster.

Evolution of COBOL

  • In 1959, COBOL was developed by CODASYL (Conference on Data Systems Language).
  • COBOL-61 was released in 1961 with some revisions.
  • In 1968, COBOL was approved by ANSI as a standard language for commercial use (COBOL-68).
  • It was again revised in 1974 and 1985 to develop subsequent versions named COBOL-74 and COBOL-85 respectively.
  • In 2002, Object-Oriented COBOL was released, which could use encapsulated objects as a normal part of COBOL programming.

Where COBOL is used?

Most of the system in US like Financial System, Social Security Administration, Department of Defense, Internal Revenue Service, the Majority of State Financial / Unemployment Systems and Numerous other Critical Systems is developed using COBOL.

Your first program

I think that's enough introduction about COBOL background and history. Let's start the real code.

How to install

To run COBOL on your local machine. You need GNU-COBOL. In this article I'll be showing how to install it on Mac OS. First you need homebrew installed. After that you can just execute

brew install gnu-cobol
Enter fullscreen mode Exit fullscreen mode

To check whether the installation succeed or not, try run this command.

cobc --version
Enter fullscreen mode Exit fullscreen mode

it should shown something like this

cobc (GnuCOBOL) 2.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Keisuke Nishida, Roger While, Ron Norman, Simon Sobisch, Edward Hart
Built     Apr 29 2020 00:06:51
Packaged  Sep 06 2017 18:48:43 UTC
C version "4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.32.59)"
Enter fullscreen mode Exit fullscreen mode

Prepare your Text Editor

There's few different text-editor you can use to wrote COBOL Program like, HackEdit, OpenCobolIDE, etc. For the sake of simplicity, i'll just use Visual Studio Code.

Install COBOL Extension

Using Visual Studio Code you can just go to Extension Marketplace and install COBOL Plugin. The extension provide Syntax highlighting for COBOL, JCL, PL/I and MF directive files, this help us a lot understanding COBOL program.

Writing your first program in COBOL

Let's write some code below.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. hello.
       AUTHOR. KELVIN.
       PROCEDURE DIVISION.
           DISPLAY "Hello World".
        STOP RUN.
Enter fullscreen mode Exit fullscreen mode

and to run it

cobc -x hello.cob
./hello
Enter fullscreen mode Exit fullscreen mode

The output should looks like this.

Hello World
Enter fullscreen mode Exit fullscreen mode

Yes, this is your Hello World program using COBOL :).

Program Structure

Let's discuss about what's happening on our program. First take a look at this image.
cobol program structure

A brief introduction of these image is given below −

  • Sections are the logical subdivision of program logic. A section is a collection of paragraphs.
  • Paragraphs are the subdivision of a section or division. It is either a user-defined or a predefined name followed by a period, and consists of zero or more sentences/entries.
  • Sentences are the combination of one or more statements. Sentences appear only in the Procedure division. A sentence must end with a period.
  • Statements are meaningful COBOL statements that perform some processing.
  • Characters are the lowest in the hierarchy and cannot be divisible.

Division

Identification Division

First and only the mandatory division of every COBOL program. The programmer and the compiler use this division to identify the program. In this division, PROGRAM-ID is the only mandatory paragraph. PROGRAM-ID specifies the program name that can consist 1 to 30 characters.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. hello.
Enter fullscreen mode Exit fullscreen mode

Procedure Division

In short, procedure division is used to include the logic of the program. It consists of executable statements that written by the progammer.
There must be at least one statement in the procedure division. The last statement to end the execution in this division is either STOP RUN or EXIT PROGRAM.

      PROCEDURE DIVISION.
           DISPLAY "Hello World".
        STOP RUN.
Enter fullscreen mode Exit fullscreen mode

Conclussion

I've only tried wrote simple program in COBOL and saw other people code on internet. But here's few things i found compare to language that i used nowadays.

Bad Part:

  • A lot of reserved words, COBOL has over 300 keywords while Python only has 33 keywords. This lead to long list of reserved words to memorize.

  • Invent the wheel. Because there's no library concept in COBOL most of time you need to write your own libraries to do something you want. Also because of this everything is implemented as a keyword. for example when you want to support XML they introduce keywords like PARSE and RENDER.

  • Over-verbose syntax, when COBOL designed this kind of syntax was intended to make the code itself self documenting by using english like syntax so it readable for non-tecnical person but it ends up unmaintainable and uncomfortable for real programmer.

Good Part:

  • Less code to type, because a lot of reserved words, you only wrote data you're going to need and logic you're going to execute.

  • Performance. For many years COBOL compilers have been producing very high performance executables. Only native C and native Assembler beat COBOL on an IBM mainframe.

references:
https://softwareengineering.stackexchange.com/questions/112911/why-the-scorn-for-cobol
https://en.wikipedia.org/wiki/COBOL#Criticism_and_defense
https://devops.com/the-beauty-of-the-cobol-programming-language-v2/

Discussion (0)