From the course: COBOL Essential Training

COBOL naming standards - COBOL Tutorial

From the course: COBOL Essential Training

Start my 1-month free trial

COBOL naming standards

- [Instructor] Here's an updated version of the BMI calculator. This version reads input from a file, calculates the BMI and then prints a report of the results. Now, I will review the requirements for reading and writing to files later in this course. But for now, let's take a closer look at the data names used in this enhanced version starting with the input-output section. Here, under the FILE_CONTROL, I have created names for the two files that are going to be used in this program. These file names are used in the program and they are created using all letters and a hyphen. Hyphen is the only special character allowed in our user-defined names. The file name, BMI dash File is used throughout the COBOL program. And it is assigned to a file name that exists on my computer, BMI dash INPUT dash DAT. Next on line 11, we have another file, PRINT dash FILE. This is the output file for this program, and it is assigned to BMI dash REPORT dash DAT which also exists on my computer. In addition to file names, we use data names to represent fields of data, where a field is a piece of information. In the file description section on line 15 where it says FD BMI dash FILE. This is where I've created data names for each piece of data on the input file that will be read by my program. Let me scroll down a little bit. Here we see on line 19, PERSON dash NAME. The PERSON dash NAME, is made up of two fields. On line 20 we have LASTNAME, then on line 21, we have FIRSTNAME. Below that we have HEIGHT dash INCHES, and below that we have WEIGHT. When I scroll down a little further we'll see WORKING-STORAGE. WORKING-STORAGE is where I have my variable that will hold the calculated BMI. Now notice I'm using a prefix of WS This is on line 31. So WS dash BMI, I am using this prefix to indicate that this variable is from WORKING_STORAGE and not a field on my input file. If we look down a little further, you'll see on line 41, I'm having my DETAIL_LINE. The DETAIL-LINE, which is used to write to my report file also has data names that are prefixed with DET, to indicate they are part of the detail for the report. This is a common practice in COBOL Naming Standards. It is important to choose meaningful names, but just like other programming languages, COBOL has certain rules we need to follow when selecting these names. User-defined names can contain letters, numbers and a hyphen. But a programmer-supplied name may not begin or end with a hyphen. The data names may not exceed a max of 30 characters, and reserved words may not be used as programmer-supplied names. Data names must contain at least one letter and paragraph names can contain numbers, letters and a hyphen, but they can also be all numeric. Let's go back to our BMI program. I'm going to scroll down a little further so we can see the PROCEDURE DIVISION. As you know, the PROCEDURE DIVISION is where the bulk of our programming will occur. And this is where we have our user-defined paragraphs. It is helpful to number the paragraphs for easier readability. Such as you'll notice on line 60 I have 0100 dash PROCESS dash RECORDS. I chose to start with 100 cause that left room for any additional paragraphs that I might need before or after this paragraph. And it turns out I did need to add 0050 dash OPEN dash FILE. As I stated the paragraph name can contain numbers, letters and a hyphen. The paragraph names can even be all numbers. Just like the data names, you want to make sure you choose meaningful paragraph names. So for example, here I have 50 is OPEN-FILE I have on line 60 is PROCESS_RECORDS. Line 69 is the paragraph that will actually calculate the BMI. We have paragraph 300 which is write the HEADING_LINE 320 writes the DETAIL_LINE. Paragraph 400 is the last paragraph and it performs the activities that are needed to stop the execution of the program by closing the two files, STOP-RUN and END PROGRAM.

Contents