Showing posts with label COBOL. Show all posts
Showing posts with label COBOL. Show all posts

Friday, November 25, 2011

Steps to write a cobol program


  1. create a PDS
  2. create member.
  3. edit member code cobol pegàsour.cobol.pg
            a) use 88 level no of multiple conditions
b)use evaluate to avoid nested if
c) use 77 for independent datanames
d) decide the pge into manageable paragraphs
e) use the para nos in asc order
f) define user defined variables with more meaningful names
g) user arrays for effective peg process.
h)avoid goto
i)use separate exit statement for all performs.
     4. prepare compilation jcl use existing jcls.
     5. submit a peg for compilation.
     6. enter into spool check for the compilation job statements
     7. if there are any errors rc will be non zero
     8. open a compiled peg and find for errors
     9. when compilation and link step /activities/ 00 prepare run jcl with data file and
         object jcl.
    10. submit jcl to execute cobol.
    11. identify output
    12. if the data is stored in the file open the file by using ispf panel
    13. if o/p is matching with the requirements that peg
    14. then inform to project       leader by mail or phone.

Monday, November 21, 2011

COBOL Rules


Cobol Coding

Good COBOL Coding practices for better Modularity and Performance.
1.
Do not use 66 level. Planning of data definition should make this redundant
2.
Do not use 77 level. Collect all these together and group under a 01 level.
3.
Use 88 level appropriately. For conditional names.
4.
All level numbers increment by an odd number in order to allow for the insertion of extra levels during modifications; this reduces the need for wholesale renumbering of the levels with all the problems that this entails.
 
01
LEVEL-1.



05
LEVEL-2.



10 LEVEL-3
PIC 9(02) COMP-3 VALUE ZEROES.
5.
PARAGRAPHs or SECTIONs should be used; they should not be mixed except when the PARAGRAPH names are used to subdivide the SECTION. If this is used then only the SECTIONs should be performed.
6.
It corresponds to the sequence/logical position in the structure of the code. This enables the location to be easily ascertained in a listing and also allows the user to see where the program has progressed to in the event of any problems.
7.
The SORT and MERGE statements should be avoided if at all possible, it is more efficient to use an external sort, e.g. SYNCSORT/DFSORT, rather than calling the internal sort which also uses one of these sorts. This is because with an internal sort, control is passed back to the program after each record reducing the speed of the sort. It is also easier to debug a COBOL program than it is to debug the dump from an internal sort abend. If SORT routines are used then the maximum key length is 4092 bytes with no maximum number of keys. To increase the efficiency of the sort, keep the key fields at the start of the record and as contiguous as possible.
8.
GOTO should never be used; a structured program should not need to use this command. If however because of coding or processing constraints, then the only possible use is to transfer control to the end of the SECTION or PARAGRAPH being processed. It MUST NOT be used in any other case.
9.
Nested IF statements should be avoided and consideration should be given to the use of EVALUATE as an alternative. The other option is to restructure the logic of the program so that the if statement is either eliminated or simplified. The WHEN OTHER must always be coded; if this is not present then if none of the prior conditions are satisfied then the program will fall through with no processing occurring. The WHEN OTHER statement should always be the last option; if it is not then all conditions following are irrelevant as they will not be tested.
10.
Care should be taken when using STRING and UNSTRING as this may hide data structures. When using these operations then the ON OVERFLOW clause should always be used; if this is not used then you are not notified of the incomplete operation and control passes to the next sequential statement.
11.
All structures are terminating with END-XXX.
12.
The READ and WRITE PARAGRAPH/SECTIONS should be grouped together
13.
For each file there should be a separate READ PARAGRAPH/SECTION with an ‘AT END’ clause; this should be performed in all cases and the AT-END condition tested for sequential files or the return code. For each file there should be a separate READ section. This is to reduce the size of the load module as the machine instructions generated for the execution of a perform are less than that for a READ; it also means that the file is only read in the one place rather than it scattered throughout the program, easing maintenance.
14.
An initial value of spaces or zero can be given to an item either in the DATA DIVISION or the PROCEDURE DIVISION. In the latter division then there are two options for doing this, to move an initial value to the individual items or to use in COBOL II onwards, the INITIALIZE statement. The use of the VALUE field of the data division should be used to give an initial value to constants.
15.
For variables that are used in working storage the INITIALIZE statement should be used. The INITIALIZE statement is limited in that it will only move spaces (to alphanumeric) or zeros (to numeric) depending on the item definition. All numeric types are supported for this statement, zoned, packed, binary. If any other value is required as an initial value then this must be moved separately, it is still the case that this should be done in the initialization PARAGRAPH or SECTION that is performed prior to processing and at any other time as required.
16.
How the table has been defined in the DATA DIVISION controls the access method used to retrieve the data. If an index has been defined then the method used is ‘SEARCH ALL’ if the data is in key sequence (and BATCH), else use SEARCH; if there are more than 10 entries, and the table will be heavily accessed consider sorting the data; if no index is present then use PERFORM VARYING to retrieve the data.
17.
An index is more efficient than a subscript; this is because a factor of the element size for the table is built into the index, ensuring that the displacement from the start of the table is known for the element and does not have to be calculated at run time as it does with subscripts. This increases the speed at which the elements can be accessed when an index is used. If a table is to be searched sequentially, then if possible put the occurrences that are more likely to satisfy the requirements when a table is checked at the start. If the table is to be searched using a binary search, then the items must be stored in ASCENDING/DESCENDING key sequence.
18.
Static call should be used when size is not an issue but speed is, or the program is called often. Or else use Dynamic call.
19.
The following keywords must not be used:
NEXT SENTENCE
ADD CORR, MOVE CORR
MULTIPLY
GO TO

TSO Commands from COBOL program



 CBL APOST,NODECK,OBJECT,BUF(10000),DYNAM         => Compiler option override
      ******************************************************************
      *   FUNCTION = This sample program demonstrates how to invoke    *
      *              TSO commands from a COBOL program using           *
      *              standard TSO services as documented in the        *
      *              TSO/E Programming Services manual.                *
      ******************************************************************
       Identification Division.
       Program-ID. SMSTSOEV.

       Data Division.
        Working-Storage Section.
         01 Filler.
           05 ws-dummy        Pic s9(8) Comp.
           05 ws-return-code  Pic s9(8) Comp.
           05 ws-reason-code  Pic s9(8) Comp.
           05 ws-info-code    Pic s9(8) Comp.
           05 ws-cppl-address Pic s9(8) Comp.
           05 ws-flags        Pic X(4) Value X'00010001'.
           05 ws-buffer       Pic X(256).
           05 ws-length       Pic s9(8) Comp Value 256.

       Procedure Division.
      *----------------------------------------------------------------*
      *          Call IKJTSOEV to create the TSO/E environment         *
      *----------------------------------------------------------------*
           CALL 'IKJTSOEV' Using ws-dummy,ws-return-code,ws-reason-code,
                                 ws-info-code,ws-cppl-address.
           IF ws-return-code > zero
             DISPLAY 'IKJTSOEV Failed, Return-code=' ws-return-code
                                     ' Reason-code=' ws-reason-code
                                     'Info-code='    ws-info-code
             MOVE ws-return-code to Return-code
             STOP RUN.
      *----------------------------------------------------------------*
      *          Build the TSO/E command in ws-buffer                  *
      *----------------------------------------------------------------*

           MOVE 'ALLOCATE DD(SYSPUNCH) SYSOUT HOLD' to ws-buffer.

      *----------------------------------------------------------------*
      *   Call the TSO/E Service Routine to execute the TSO/E command  *
      *----------------------------------------------------------------*
           CALL 'IKJEFTSR' Using ws-flags,ws-buffer,ws-length
                                 ws-return-code,ws-reason-code,ws-dummy.
           IF ws-return-code > zero
             DISPLAY 'IKJEFTSR Failed, Return-code=' ws-return-code
                                     ' Reason-code=' ws-reason-code
             MOVE ws-return-code to Return-code
             STOP RUN.

      *----------------------------------------------------------------*
      *          Check that the ALLOCATE command worked                *
      *----------------------------------------------------------------*
           DISPLAY 'ALLOCATE Worked ! ' Upon Syspunch.

           STOP RUN.

Compiler Options


The default options that were set up when your compiler was installed are in effect for your program unless you override them with other options. To check the default compiler options of your installation, do a compile and check in the compilation listing.

Ways of overriding the default options


1.Compiler options can be passed to COBOL Compiler Program (IGYCRCTL) through the PARM in JCL.
2.PROCESS or CBL statement with compiler options, can be placed before the identification division.
3.If the organization uses any third party product or its own utility then these options can be coded in the pre-defined line of the utility panel.

Precedence of Compiler Options

  1. (Highest precedence). Installation defaults, fixed by the installation.
  2. Options coded on PROCESS /CBL statement
  3. Options coded on JCL PARM parameters
  4. (Lowest Precedence). Installation defaults, but not fixed.

The complete list of compiler option is in the table:

Aspect
Compiler Option
Source Language
APOST, CMPR2, CURRENCY, DBCS, LIB, NUMBER,
QUOTE, SEQUENCE, WORD
Date Processing
DATEPROC, INTDATE, YEARWINDOW
Maps and Listing
LANGUAGE, LINECOUNT, LIST, MAP, OFFSET, SOURCE, SPACE, TERMINAL, VBREF, XREF
Object Deck generation
COMPILE, DECK, NAME, OBJECT, PGMNAME
Object Code Control
ADV, AWO, DLL, EXPORTALL, FASTSRT, OPTIMIZE, NUMPROC, OUTDD, TRUNC, ZWB
Debugging
DUMP, FLAG, FLAGMIG, FLAGSTD, SSRANGE, TYPECHK
Other
ADATA, ANALYZE, EXIT, IDLGEN


ADV: It is meaningful if your program has any printer files with WRITE..ADVANCING keyword. The compiler adds one byte prefix to the original LRECL of printer files for printing control purpose. If you are manually populating printing control character in the program, then you can compile your program with NOADV.

DYNAM: Use DYNAM to cause separately compiled programs invoked through the CALL literal statement to be loaded dynamically at run time. DYNAM causes dynamic loads (for CALL) and deletes (for CANCEL) of separately compiled programs at object time. Any CALL identifier statements that cannot be resolved in your program are also treated as dynamic calls. When you specify DYNAM, RESIDENT is also put into effect.




LIST/OFFSET: LIST and OFFSET are mutually exclusive.  If you use both, LIST will be ignored. LIST is used to produce listing a listing of the assembler language expansion of your code. OFFSET is used to produce a condensed Procedure Division listing. 
With OFFSET, the procedure portion of the listing will contain line numbers, statement references, and the location of the first instruction generated for each statement.  These options are useful for solving system ABENDS. Refer JCL session for more details.

MAP:   Use MAP to produce a listing of the items you defined in the Data Division.

SSRANGE:   If the program is compiled with SSRANGE option, then any attempt to refer an area outside the region of the table will abnormally terminate with protection exception, usually S0C4.It also avoids any meaningless operation on reference modification like negative number in the starting position of reference modification expression. If the program is compiled with NOSSRANGE, then the program may proceed further with junk or irrelevant data. So usually the programs are compiled with SSRANGE during development and testing.

RENT: A program compiled as RENT is generated as a reentrant object module. CICS programs should be compiled with RENT option to share the same copy of the program by multiple transactions (Multithreading)

RESIDENT:   Use the RESIDENT option to request the COBOL Library Management Feature. (The COBOL Library Management Feature causes most COBOL library routines to be located dynamically at run time, instead of being link-edited with the COBOL program.).CICS Programs should be compiled with RESIENT option.

XREF: Use XREF to get a sorted cross-reference listing.  EBCDIC data-names and procedure-names will be listed in alphanumeric order.   It also includes listing, where all the data-names that are referenced within your program and the line number where they are defined. This is useful for identifying the fields that are defined but not used anywhere after the development of new program.

COMPILATION of COBOL Program


COBOL COMPILATION

                                                                                      SYSPRINT
PARM                                                                               (Compiler listing)
(Compiler
  Options)


              IGYCRCTL
  (COBOL COMPILER)
 



SYSIN                                                                           SYSLIN(Object Module)
(Source)





SYSLIB                                                                            PARM
(Copybook Library)                                                            (Link
                                                                              edit Options)

            IEWL
       (Link Editor)
 
                                                                                 
                            
SYSLMOD
(Load Module)



SYSPRINT                                                                                   SYSLIB
(Link edit messages)                                                     (Subroutine Library)

COMPILATION JCL:
//SMSXL86B JOB ,'COMPILATION JCL', MSGCLASS=Q,MSGLEVEL=(1,1),CLASS=C  
//COMPILE1  EXEC  PGM=IGYCRCTL, PARM=’XREF,APO,ADV,MAP,LIST),REGION=0M
//STEPLIB      DD  DSN=SYS1.COB2LIB,DISP=SHR
//SYSIN         DD  DSN=SMSXL86.TEST.COBOL(SAMPGM01),DISP=SHR
//SYSLIB       DD  DSN=SMSXL86.COPYLIB,DISP=SHR
//SYSPRINT    DD  SYSOUT=*
//SYSLIN       DD  DSN=&&LOADSET, DCB=(RECFM=FB,LRECL=80,BLKSIZE=3200),
//                 DISP=(NEW,PASS),UNIT=SYSDA,SPACE=(CYL,(5,10),RLSE),
//SYSUT1       DD UNIT=&SYSDA,SPACE=(CYL,(1,10))  => Code SYSUT2 to UT7
//LINKEDT1    EXEC  PGM=IEWL,COND=(4,LT)
//SYSLIN       DD  DSN=&&LOADSET, DISP=(OLD,DELETE)
//SYSLMOD    DD  DSN=&&GOSET(SAMPGM01),DISP=(NEW,PASS),UNIT=SYSDA
//                 SPACE=(CYL,1,1,1))
//SYSLIB       DD  DSN=SMSXL86.LOADLIB,DISP=SHR
//SYSUT1       DD  UNIT=SYSDA,SPACE=(CYL,(1,10))
//SYSPRINT    DD SYSOUT=*

//*** EXECUTE THE PROGRAM ***
//EXECUTE1  EXEC  PGM=*.LINKEDT1.SYSLMOD,COND=(4,LT),REGION=0M
//STEPLIB      DD  DSN=SMSXL86.LOADLIB,DISP=SHR
//                 DD  DSN=SYS1.SCEERUN,DISP=SHR
//SYSOUT      DD  SYSOUT=*
//SYSPRINT    DD  SYSOUT=*

FILE STATUS CODES


FILE STATUS CODES


          It is a two-byte working storage item. The first byte denotes the general category whereas second byte denotes the particular type of error message under that category.

0

Successful OPEN/READ/WRITE Operation

0
Successful completion

2
Duplicate key was detected which is allowed as per definition of AIX.

4
Length of record just READ didn’t conform  to the fixed length attributes for the file.

5
Referenced Optional file is not present during OPEN. If open mode is I-O or EXTEND, then file will be created.

7
Open or Close statement is executed with a phrase that implies a tape file (ex NO REWIND) whereas the file is not in TAPE.
1

When AT END condition fails

0
Sequential READ is attempted on
1.after the end of file is reached
2.optional file that is not present.

4
Sequential READ was attempted for a relative file and RRN is larger than the maximum that can be stored in the relative key data item.
0

When INDEX Key fails

1
Sequence error exists for sequentially accessed index file.

2
Attempt was made to write a record that would create a duplicate key.

3
Record not found.(for keyed random access)

4
Space not found for WRITE
3

Permanent Open error

5
Opening a non-optional file that was not present.

7
Open mode is not permitted.

8
Open issued for a file that was closed previously with lock

9
File attribute mismatch-Open failed.
4

Logic error in opening/closing/deleting

1
OPEN a opened file.

2
CLOSE attempted for not opened file.

3
IO statement before the current REWRITE/DELETE is not successful.

4
REWRITE attempt with invalid length

7
READ file which is not opened in INPUT or I-O mode

8
WRITE file which is not opened in I-O OUPUT or EXTEND mode

9
DELETE or REWRITE file which is not opened in I-O mode.
9

Implementation defined

1
VSAM password error

2
Logic error

3
VSAM resource unavailable

6
No DD statement specified for VSAM file.

7
File integrity verified for VSAM file.