- Naming standards in a COBOL program. Programmers define names for data, paragraphs, and file names in COBOL. Let's start talking about data names. Data names are the elements on which instructions operate. For example, in this program, on line 27, I have a SALESPERSON-ID. That's a data name which represents the value of the salesperson ID that's on a file that is being read by this program.
Data names are used to represent fields of data where a field is a basic fact. For example, the salesperson's name, what region they're in, and yearly sales. All programmer-supplied names are chosen using the following rules: the name must contain letters, numbers, and optionally, a hypen. A programmer-supplied name may not begin with and end with a hyphen. A programmer-supplied name has a maximum of 30 characters.
We cannot use reserved words in our name. For example, SUM, ADD, or DATA. Data names must contain at least one letter, but paragraph names actually could be all numeric. In COBOL, all our code is enclosed in paragraphs and a paragraph name is a tag to which the program refers. It is a common COBOL programming practice to number the paragraphs for easier readability. Let's scroll down to the Procedure division, and we'll see the paragraphs in this program.
As you can see on line 78, the first paragraph has a number, 0100-PROCESS-RECORDS. Notice that I chose to start with 100. That leaves room for any additional paragraphs that I might need before or after this paragraph. For example, I might want to add 0050-OPEN-FILE, to split that processing up. Now I can move the two lines of code that open the files up into this paragraph.
I do want to point out that although I did put the 0050 paragraph before the 0100, COBOL really doesn't care about the numbering scheme, so I could have put them in the opposite order, but this makes the program much easier to read and maintain in the future. The paragraph name could contain numbers, letters, and a hyphen. Notice the only special character that can be used is the hyphen. And finally, file names are specified in several places throughout a COBOL program, but their initial appearance is in the Environment division.
Let's scroll back up to the Environment division. In this sample program, on line 15 is where I start my file control, and on lines 16 and 18 I have two file names. One's the SALESFILE, and one's the PRINT-FILE. The SALESFILE contains data that I will read into my program and the PRINT-FILE is for the report that I'm going to produce inside of this program. One more thing to consider. When you're providing names for your data, your paragraphs, and your file names, choose names that are meaningful.
Remember, you have up to 30 characters to choose a name that will be easy to understand and also provide easy maintenance in the future.
This course is designed to help new and experienced programmers alike add COBOL (or add COBOL back) to their skill set. Peggy Fisher shows how to get a COBOL development environment up and running and how to start programming. She reviews COBOL's data types and constants, control structures, file storage and processing methods, tables, and strings. Challenges issued along the way will help you practice what you've learned.
- Downloading and installing Cygwin and GNU COBOL
- Editing, compiling, linking, and running COBOL programs
- Describing data in COBOL
- Working with verbs and expressions
- Using branching
- Reading and writing sequential files
- Updating and deleting records
- Working with relative and indexed files
- Creating and searching tables
- Handling strings