Mainframes 360
The one stop destination for System Z professionals

Thursday, August 6, 2009

Common COBOL Errors and Coding Standards

Q. What are some of the common COBOL Errors, a beginner could make?
Here are the top 10 Mistakes in COBOL, you should watch out for, when you start-out to write COBOL-Programs.

1. Period was Required
Often, you forget to put periods after divisions, sections or para's, and that’s when you get a "A Period was Required" Message. See what happens when you forget to put a period after IDENTIFICATION DIVISION.

Image280 
2. Forgetting a Dash
You will get strange error-Messages when you forget to put a dash in paragraph names that have multiple words. For example, if you forget to code a dash in the OPEN-FILE-ROUTINE, you get the following error Message.

Image281 
3. XXX should not begin in Area-A
I have coded PERFORM Instruction by error in Col 11. Coding a COBOL-statement in Area-A, when it should begin in Area-B, will issue an error like ... should not begin in Area-A, it was processed as if in Area-B.

Image282 
4. YYY should begin in Area-A.
I have coded the OPEN-FILE-ROUTINE by mistake in Area B. Coding a para, section or division entry in Area-B, when it should begin in Area-A, will issue an error like ...should begin in Area-A, it was processed as if in
Area-A.

Image283 
5. XXX was not defined as a data-name.
I have declared the Variable WS-EMPLOYEE-SALARY in the Working Storage Section. But in the PROCEDURE DIVISION, while using it, I did a Typo-error, and spelled it as WS-EMPLOYEE-SALRY(forgot the 'A'). COBOL Compiler does not recognize the Data-name WS-EMPLOYEE-SALRY(the one without the 'A'), and complains about it.

Image284 
6. The XXX Statement was Invalid. Expected ..., but found something else.
I must have committed a Syntax-error. Out here, I’ve forgotten to put a closing single-quote on the Literal String 'QUASAR'. Here’s what the Compiler complains.

Image285 
7. Putting PICTURE Clause on a Group Data-Item.
Many beginners write the PICTURE Clause on the Group Level Data-Item. In COBOL, you break down Group Data-items further in smaller Storage Areas. So, you always specify the PICTURE Clause for the Lowest-Level Data-Items. By error, I have coded PICTURE Clause on WS-EMPLOYEE-RECORD, when it should be specified only for the EMPLOYEE-NAME, EMPLOYEE-SALARY and EMPLOYEE-JDATE Fields.

Image286 
8. Neither an FD nor SD was found in the Program for the File XXX.
I have done a typo-error in the FD Para, so the COBOL Internal file-name specified in the SELECT Clause does not match the FD Descriptor.

Image287

9. No Matching Scope-Terminator Found.
When you code IFs, EVALUATEs, loops using Inline PERFORMs be sure to supply the Scope Terminator END-IF, END-EVALUATE,
END-PERFORM correctly. The Compiler Software recognizes the statements inside an PERFORM..END-PERFORM as one block. I've forgotten to code an END-PERFORM, and the compiler complains about it– it doesn't know where the PERFORM Block ends. 

Image290
Q. What are some COBOL Coding Standards one should follow?
Program Header : Every COBOL Module should begin with a Program-Header, that specifies the Program Name, system, description, File Access, DB2 Tables Access, Sub-programs called etc.

Here's how I have coded the Program-Header for my COBOL-Program PROG05.

Image291

IDENTIFICATION DIVISION : The PROGRAM-ID should be 7 OR 8 chars and must be the same as Member-name. Most projects or Production Environments specify a naming-convention for the PROGRAM-ID. At my work-place  it goes like this -

PROGRAM-ID. XXXYZAnn.

The first 3 characters XXX should be the name of the Sub-system. Say for example,  our Mainframe sub-system is GEMINI, so we use the three letter prefix GEM for all our programs. It is followed by B, if it’s a Batch Program, C if its an Online Program. Our programs are mostly Batch, so we'd put a B. We also use a single letter code - I for Inserts, U for Updates, D for Deletes, S for Selects. Say, that my Program does Inserts, so I use the code I. We also code D for DB2, or I for IMS. My Program uses DB2 for storing Data. Hence, my program-id would be GEMBIDnn. So, in my project, all our Programs are like GEMBID01, GEMBID27, GEMBID58, GEMBID59, GEMBID60, GEMBID2A so on and so forth.

ENVIRONMENT DIVISION : For readability, you should write each SELECT Statement on a new line, and the ASSIGN TO Clause, ORGANIZATION IS, ACCESS MODE IS, FILE STATUS IS, and the RECORD KEY IS Clauses should be indented thereafter on successive lines.

image

DATA DIVISION : It's a good practice to organize the Working-Storage Variables in the COBOL-Program, so its very easy to FIND Variables. For example, all the Storage Areas used for Input-Output go together. All the Print-Formats such as Header-Lines, Separator Lines should be grouped. Then follows the DB2-I/O Areas which contain 'EXEC SQL – END-EXEC' Blocks. These are Storage Areas in COBOL that will receive the data fetched from DB2 Tables, or send data-output to be stored in DB2 Tables. Likewise, Flags-and-Switches which help detect special conditions, Literals and Constants(Values that don’t change) and Counters which help keep track how many times you do a thing;are stored together under the Working Storage Section.

image

Level-Numbers and Picture Clause – Get into the habit of inserting Blank
Empty-Lines, lavishly. Don't hesitate to use them.

Level-Numbers indicate Break-up or Structure. User Non-contiguous Level-Numbers like 01, 05, 10, 15 and so on. Indent each higher Level-Number 3 columns to the right. See how I have indented 01, 05 and 88-Levels.

image

Code the Picture Clause preferably on Position 45. For Numeric-Edited variables like WS-CONTRACT-FACE-AMT, write the entire PIC Clause 
–ZZZ,ZZZ,ZZZ,ZZZ,ZZZ.99 instead of writing –Z(3),Z(3),Z(3),Z(3),Z(3).9(2).

image
Q. I want a comprehensive list of all the possible COBOL Errors, that the COBOL Compiler complains about? 
To generate a complete Listing of all possible errors in COBOL, along with the Errors-Codes and their Description, I'd call for ERRMSG. Write a COBOL Program, with the PROGRAM-ID ERRMSG. and that does the trick. I've coded the ERRMSG COBOL Program.

Image288

This magic trick works, COBOL-Compiler Software recognizes the special-program ERRMSG, and it produces an entire list of Error-codes and Descriptions in the Compiler-Listing. Here's an extract of what you'll get as output in the Compile-Listing. It’s a huge List. Get the Complete Error-Listing here.

Image289

To many people who are thrown to work at a mainframe computer on their first job, they feel lost. Mainframe people seem to speak a completely different language and that doesn't make life easy. What's more, the books and manuals are incredibly hard to comprehend.

"What on earth is a Mainframe?" is an absolute beginner's guide to mainframe computers. We'll introduce you to the hardware and peripherals. We'll talk about the operating system, the software installed on a mainframe. We'll also talk about the different people who work on a mainframe. In a nutshell, we'll de-mystify the mainframe.

Readers based in India, can buy the e-book for Rs. 50 only or the print book. International readers based in the US and other countries can click here to purchase the e-book.