Coding Style

The only sure way of finding out whether a program is correct is to read it. Therefore, programs must be easy to read. Naturally, easy to read means different things to different programmers. All we can do here is to give examples of different styles, and leave you to choose the style you prefer.

A very good way to get your program checked is to ask a fellow worker to read it. Be sure to choose a coding style that your fellow workers find easy to read.

Most people would find the following program fragment difficult to read.
/********************************************************/
/* SAMPLE #1:  A portion of CATMOUSE EXEC               */
/* not divided into segments and written with no        */
/* indentation, and no comments.  This style is not     */
/* recommended.                                         */
/********************************************************/

do forever
call display
pull move
if datatype(move,whole) & move >= 0 & move <=2
then select
when mouse+move > len then nop
when cat > mouse,
& mouse+move >= cat,
then mouse = cat
otherwise
mouse = mouse + move
end
if mouse = hole then leave
if mouse = cat  then leave
jump = random(1,spring)
if cat > mouse then do
if cat-jump < 1 then nop
else cat = cat-jump
end
else do
if cat+jump > len then nop
else cat = cat+jump
end
if cat = mouse then leave
end
call display
if cat = mouse then say "Cat wins"
else say "Mouse wins"
exit
This next example is easier to read. It is divided into segments, each with its own heading. The comments on the right are sometimes called remarks. They can help the reader get a general idea of what is going on.
/********************************************************/
/* SAMPLE #2:  A portion of CATMOUSE EXEC               */
/* divided into segments and written with 'some'        */
/* indentation and 'some' comments.                     */
/********************************************************/

/********************************************************/
/* Main program                                         */
/********************************************************/
do forever
   call display
   /*****************************************************/
   /* Mouse's turn                                      */
   /*****************************************************/
   pull move
   if datatype(move,whole) & move >= 0 & move <=2
   then select
      when mouse+move > len then nop       /* hits wall */
      when cat > mouse,
         & mouse + move >= cat,            /* hits cat  */
      then mouse = cat
      otherwise                            /* moves     */
      mouse = mouse + move
  end
  if mouse = hole then leave            /* reaches hole */
  if mouse = cat then leave             /* hits cat     */
  /******************************************************/
  /* Cat's turn                                         */
  /******************************************************/
  jump = random(1,spring)
  if cat > mouse then do      /* cat tries to jump left */
     if cat - jump < 1 then nop           /* hits wall  */
     else cat = cat - jump
  end
  else do                    /* cat tries to jump right */
     if cat + jump > len then nop         /* hits wall  */
     else cat = cat + jump
  end
  if cat = mouse then leave
end
/********************************************************/
/* Conclusion                                           */
/********************************************************/
call display
if cat = mouse then say "Cat wins"
else say "Mouse wins"
exit
This next example has additional features that are popular with some programmers. Keywords written in uppercase and a different indentation style highlight the structure of the code; the abundant comments recall the detail of the specification.
/********************************************************/
/* SAMPLE #3:  A portion of CATMOUSE EXEC               */
/* divided into segments and written with 'more'        */
/* indentation and 'more' comments.                     */
/* Note commands in uppercase (to highlight logic)      */
/********************************************************/

/********************************************************/
/* Main program                                         */
/********************************************************/
DO FOREVER
  CALL display
  /**********************************/
  /* Mouse's turn                   */
  /**********************************/
  PULL move
  IF datatype(move,whole) & move >= 0 & move <=2
    THEN SELECT
           WHEN mouse+move > len     /* mouse hits wall */
             THEN nop                /* and loses turn  */
           WHEN cat > mouse,
                & mouse+move >= cat, /* mouse hits cat  */
             THEN mouse = cat        /* and loses game  */
           OTHERWISE mouse = mouse + move /* mouse ...  */
         END                   /* moves to new location */
  IF mouse = hole THEN LEAVE   /* mouse is home safely  */
  IF mouse = cat  THEN LEAVE   /* mouse hits cat (ouch) */
  /**********************************/
  /* Cat's turn                     */
  /**********************************/
  jump = RANDOM(1,spring)      /* determine cat's move  */
  IF cat > mouse               /* cat must jump left    */
    THEN DO
           IF cat-jump < 1          /* cat hits wall    */
             THEN nop               /* misses turn      */
             ELSE cat = cat-jump    /* cat jumps left   */
         END
    ELSE DO                    /* cat must jump right   */
           IF cat+jump > len        /* cat hits wall    */
             THEN nop               /* misses turn      */
             ELSE cat = cat+jump    /* cat jumps right  */
         END
  IF cat = mouse THEN LEAVE    /* cat catches mouse     */
END
/********************************************************/
/* Conclusion                                           */
/********************************************************/
CALL display                        /* on final display */
  IF cat = mouse                    /* who won?         */
    THEN say "Cat wins"             /* ... the cat      */
    ELSE say "Mouse wins"           /* ... the mouse    */
EXIT
Congratulations! Now you know the best ways to program your REXX execs. Have fun writing programs that will make your life easier!

Reference Reference

Feedback


Timestamp icon Last updated: Tuesday, 7 January 2014


http://pic.dhe.ibm.com/infocenter/cicsts/v5r1/topic/com.ibm.cics.rexx.doc//dfhrx/codstyl.html