STEPS TO WRITING A COBOL PROGRAM
The final and most authoritative source for programming help is the
textbook; yet, many people like to have a concise set of steps to make them feel more
comfortable writing their first program. For those that are experienced programmers
their may be little that is new here but for a first time programmer you should be able to
follow the steps and complete the assignment. Keep in mind that a dozen programmers might
write a dozen different guides but these steps seem most logical to me in developing a
program using the Micro Focus package.
1. Read the program requirements carefully and determine what input and
output files are required. For instance if an input file is required and it is not
supplied then you will probably have to make one. If an output file is required then
you must decide on a name such as assgn3.out for instance.
2. If you are required to make a data file see the question on this site
that discusses creating data files.
3. Look at a previous data file to get an idea of what one looks
like. Make sure your file is readable in Micro Focus and your fields start on the
correct columns. If you haven't looked at one of the previous data files you can
load one, as well as the one you create just as you would load a program. If your
data file is assgn3.out then that is the file you would load into the Micro Focus editor.
Don't worry about spending too much time creating the data file and verifying it's
integrity. Double and triple check it with the assignment specifications.
If the data file is wrong and even one item is in the incorrect column then the program
will never work properly.
4. Put your data file on your floppy disk as assgn3.dat. If you have all
of the files for a particular program in a folder then make the appropriate changes to the
select statement or the line in working storage where you specify your file
location. If all of your assignment 3 files are in a folder on your floppy called
assgn3 then your path would be a:\assgn3\assgn3.dat, etc.
5. Copy assgn2.cbl to the location you would like the assignment3 source
code (your program) to be located and rename it as assgn3.cbl. In this way you are
starting with a skeleton of a program that you already know works 100%.
6. Begin modifying the program. You will want to change the program
name for instance to assgn3 and also the end program statement. Change the input and
output files to reflect the name of your data file and new output file. Type in new
report headings, etc.
7. Stub out (place *'s in column 7) all of the procedure division except
for the statements to open and close your files. "Stubbing out"portions of code
is a powerful debugging technique. You can debug small portions of your code and
remove the *'s on another small portion and debug that section. Soon all of the comment
*'s will be removed and your entire program should be functioning.
8. Check and animate your program and correct errors until you receive a
error code of all zeros in the bottom of the animate screen.
9. When you have created the data file correctly and checked(compiled) and
animated (ran) the program with no errors it is time to begin processing the input data
file.
10. When you reach logical stopping points you may want to backup your source
code. You might want to call it assgn3a.cbl,assgn3b.cbl, etc. In this way if
you introduce an error that you can't correct you can go back to a known good
version. Many times at three in the morning you are almost finished with a program
and that one final change causes an error you spend hours trying to find. By making
the backup versions as you proceed you can recover and move forward again quickly.
When the final version is done you can erase the previous versions. Micro Focus
doesn't care which program you are loading and running and different programs will create
different intermediate files (compiled object code) so there is no problem with different
versions interacting and using the same input and output files. .
11. Next define the detail line in your working storage area preferably
using a spacing chart. If a spacing chart is not available you can use graph
paper. It is rare that I see anyone using a spacing chart to do their assignments
because the perception is that they take a long time when you could be writing code.
The perception is completely wrong. A spacing chart will save you hours of work on
the latter assignments in the class by allowing you to layout all of your reports.
12. Unstub the perform 100-TOP-OF-PAGE statement in the procedure division
and recheck and animate your program. Check the output and you should see a top of
page in your output file.
13. Draw a simple flowchart of the procedure division processing.
Generally this step is one of the first ones you should do after you read the assignment
instructions but many people have great resistance to creating flowcharts thinking they
are a waste of time. As the programs get more complex a flowchart will save you
hours of frustration and they are simple to draw. The flowchart for assignment 3 for
instance can be six or eight boxes that help you understand the program flow. A
flowchart makes you think about what you are about to code before you code it so if you
are unclear about an assignment requirement you can ask at this point. It is easier
to understand the problem before you begin than to realize you didn't understand 3/4
through and have to modify your logic. Consult your text for illustrations of flowcharts..
14. Now that the program is running, the files are opening properly, and
the report title has been generated you need to generate your detail lines. The detail
lines result from processing the records on your input file. Use as many simple
performs such as PERFORM 100-TOP-OF-PAGE as you can. Simple performs execute code in
a paragraph and return to the point after the PERFORM and continue. The numbers in
the paragraph name help you easily find them. For instance I might want a
35-PRIME-READ. In that paragraph I could do my first read and display the contents and
move ' NO' to ARE-THERE-MORE-RECORDS if no more records exist. The prime read and other
housekeeping such as initializing numeric totals should occur before you begin the
processing loop. Okay, you have read the first record but how do you process all the
records? You should use a processing loop beginning with a "PERFORM UNTIL"
statement. Between PERFORM UNTIL and the scope delimiter for PERFORM
UNTIL(END-PERFORM) you code the statements to be in your processing loop. An example
of a processing loop is as follows:
PERFORM UNTIL ARE-THERE-MORE-RECORDS = ' NO'
MOVE NAME-IN TO NAME-OUT
WRITE PRINT-REC AFTER ADVANCING 2 LINES
READ YOUR INPUTFILE AT END MOVE ' NO TO ARE-THERE-
MORE-RECORDS
END-READ
END-PERFORM
PERFORM 100-TOP-OF-PAGE
In the example each record would be read and the
name moved to the NAME-OUT field. The write statement would print the detail line with
double spacing. When the next record is read and no more records exist then ' NO' is
moved to the ARE-THERE-MORE-RECORDS switch and the processing loop stops. Processing
goes to the line after the scope delimiter (END-PERFORM)and the next statement executed
will be the PERFORM 100-TOP-OF-PAGE. Your next statement after your processing loop
probably will be different. This is merely an illustration.
16. Don't worry about summary lines at first. Get all of the detail
lines printing and then work on the summary lines which will need buckets in working
storage to accumulate totals. Once buckets are coded in the numeric totals of
working storage then you will want to include code in the processing loop to add values
from each record processed to those buckets. Once again read the instructions.
If a summary page is needed generate a summary top of page.If only a summary line is
needed then write code similar to the detail line in working storage and align your
summary total amounts up with the columns they are summarizing. Look at summary lines in
the book and create your summary line on a printer spacing chart so you can line
everything up.
17. Finally, make use of the block copy to save yourself time. If
you need a summary top of page for instance you can block the 100-TOP-OF-PAGE and copy and
reinsert it and then begin modifying it In this way you start with known good
code and make few modifications rather than typing in a whole new block of code from the
beginning and introducing the potential for more errors
By taking a structured approach rather than attempting to
write the whole program at once you can correct errors at each step making the resolution
of any particular problem much easier. If this guide helped you please send me
feedback and let me know if something is unclear I would also like to know so that I can
modify it. .Happy COBOLING!
HOME