Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language COBOL

(Short Version with Descriptive Varnames.)

Date:07/14/08
Author:Joseph James Frantz
URL:http://www.aoirthoir.com
Comments:3
Info:http://www.opencobol.org
Score: (3.76 in 21 votes)
       IDENTIFICATION DIVISION.
       PROGRAM-ID. 99-Bottles-of-Beer-On-The-Wall.
       AUTHOR. Joseph James Frantz.
      *COMMENTS.
      ******************************************************************
      * PURPOSE:
      *   This is a sample COBOL program to display the lyrics of the
      *   song "99 Bottles of Beer on the Wall."
      *   This version of the COBOL 99 beers program demonstrates a few
      *   features of COBOL:
      *
      *   1. PERFORM VARYING, Cobol's version of a Loop.
      *   2. ADD/SUBTRACT with GIVING for math calculations.
      *   3. EVALUATE/WHEN, Cobol's version of Case.
      *   4. INSPECT/TALLYING, which finds the number of specified
      *      characters in a variable.
      *   5. Reference Modification:
      *      Var-name(Start character:Number of characters)
      *      which is essentially Cobol's version of text subscripting.
      *   6. Long descriptive variable names.
      *   7. Use of SPACES and ZEROES for field/display values.
      *   8. Highlight the self documenting nature of COBOL.
      ******************************************************************
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 Keeping-Track-Variables.
          05 Bottles                      PIC S99   VALUE 0.
          05 Remaining-Bottles            PIC S99   VALUE 0.
          05 Counting                     PIC 99    VALUE 0.
          05 Start-Position               PIC 99    VALUE 0.
          05 Positions                    PIC 99    VALUE 0.
       PROCEDURE DIVISION.
       PASS-AROUND-THOSE-BEERS.
        PERFORM VARYING Bottles FROM 99 BY -1 UNTIL Bottles = -1
          DISPLAY SPACES
          SUBTRACT 1 FROM Bottles GIVING Remaining-Bottles
          EVALUATE Bottles
            WHEN 0
              DISPLAY "No more bottles of beer on the wall, "
                      "no more bottles of beer."
              DISPLAY "Go to the store and buy some more, "
                      "99 bottles of beer on the wall."
            WHEN 1
              DISPLAY "1 bottle of beer on the wall, "
                      "1 bottle of beer."
              DISPLAY "Take one down and pass it around, "
                      "no more bottles of beer on the wall."
            WHEN 2 Thru 99
              MOVE ZEROES TO Counting
              INSPECT Bottles,
                TALLYING Counting FOR LEADING ZEROES
              ADD 1 TO Counting GIVING Start-Position
              SUBTRACT Counting FROM 2 GIVING Positions
              DISPLAY Bottles(Start-Position:Positions)
                      " bottles of beer on the wall, "
                      Bottles(Start-Position:Positions)
                      " bottles of beer."
              MOVE ZEROES TO Counting
              INSPECT Remaining-Bottles TALLYING
                Counting FOR LEADING ZEROES
              ADD 1 TO Counting GIVING Start-Position
              SUBTRACT Counting FROM 2 GIVING Positions
              DISPLAY "Take one down and pass it around, "
                      Remaining-Bottles(Start-Position:Positions)
                      " bottles of beer on the wall."
          END-EVALUATE
        END-PERFORM
       STOP RUN.

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
"Pretty" versionSumanta Mukhopadhyay10/06/051
Typical of mainframe COBOL programsBill Bass04/24/081
3Donald Fraser04/20/057

Comments

>>  jim said on 07/30/08 19:53:48

jim Tight code and nice demo of some COBOL strengths. Pretty short program too for a language that's often accused of being "verbose".

>>  Joseph James Frantz said on 08/09/08 00:42:16

Joseph James Frantz Thanks Jim.

I am always surprised when people object to COBOL on the basis of its being verbose. Generally I find out that their vast experience with COBOL extends to having taken one college course on the subject, if even that. COBOL can be as verbose or as slim as you want.

There are many reserved words of course, but none of them are terribly large. For instance:

MOVE "Hello World" TO myvar

Is not much different than

SET myvar = "Hello World"
or
$myvar = "Hello World";

The same is true with the vast majority of COBOL reserved words. EVALUATE...WHEN is not much different from SWITCH...CASE or CASE...WHEN in other languages. PERFORM VARYING myvar FROM 1 by 1 UNTIL myvar = 100 is one of the rarer cases that is more wordy than in SOME other languages, but not all languages. If though, you were doing anything but a For i=1 to 100 loop, then the PERFORM is not more wordy than a LOOP WHILE or a LOOP UNTIL.

Now it is true that there are a few lines required at the top of a COBOL program that most other languages do not have. To borrow from my code above:

IDENTIFICATION DIVISION.
PROGRAM-ID. SOMENAME.
DATA DIVISION.
WORKING-STORAGE SECTION.
PROCEDURE DIVISION.

This is the code that most folks complain about, the required stuff that makes COBOL verbose and wordy. That's 142 characters. If all you wanted to do was write a hello world program, then I can see that the 142 characters is something to complain about, since the stuff you are really interested in is:

DISPLAY "HELLO WORLD"
or
echo "HELLO WORLD";
or
something similar in the language of your choice. So in other languages, sure the hello world would probably be smaller, but only by about 142 characters. But in any real world programs, these minor 142 characters make your code much cleaner, easier to debug, much more readable and vastly more maintainable. From a business standpoint it also ensures that real world people can verify some of the business logic you have in your code. Languages like PHP, Java, C, and Ruby cannot make that claim. I am not disrespecting these or other languages, they are fine languages and each have their strong points. COBOL's strong point is, from the ground up it is Data Centric. Most other languages, Data is an afterthought. With COBOL it is central to the entire language.

Another complaint is the size of variable names. The thing to understand that variables can be named pretty much as the coder decides. Another version of the 99 Beers done in COBOL, the coder used the variable names i, j, k & l. Depending on your compiler, COBOL variable names can be as large as 64 characters, and I believe in some compilers 128 characters. The emphasis here is they *can* be that long. You are not required to make them that long. For instance I forgo in my code the common practice of prefacing variables from the Working-Storage section with WS-.

Finally, comparing the size of my version here, with PHP versions, C or other languages, you'll find that most of the complaints about the verbosity of COBOL programs are unwarranted. Now, I'm not saying that you can't make even smaller versions in those languages, or in COBOL. What I am saying rather is, that the verbosity of a particular program often depends more on the coding style of the coder, than on the language itself.
=====

I appreciate all of the votes also. If anyone has comments about the code, or ways it could be improved, perhaps how you would do something different, I would greatly appreciate it if you would comment here. Or you can contact me directly.

>>  Mycroft said on 09/20/08 18:38:18

Mycroft The major change I would make is to use COMPUTE instead of the other verbs. The reason is that COMPUTE will do type conversion in ALL versions of COBOL, while MOVE, ADD, SUBTRACT, etc will not. That includes things like: COMPUTE var-name = 1.

Shortest Production program I ever did in COBOL was 14 lines. Longest was over 5700 lines. Most fun was a COBOL program that wrote COBOL FDs to be compiled and run without human intervention.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: