home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5487 < prev    next >
Encoding:
Text File  |  1992-08-25  |  8.6 KB  |  303 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!convex!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Perl language formatting conventions?
  5. Originator: tchrist@pixel.convex.com
  6. Sender: usenet@news.eng.convex.com (news access account)
  7. Message-ID: <1992Aug25.224912.1775@news.eng.convex.com>
  8. Date: Tue, 25 Aug 1992 22:49:12 GMT
  9. Distribution: comp
  10. Reply-To: tchrist@convex.COM (Tom Christiansen)
  11. References: <DAVE.92Aug25141544@pipi.iis.u-tokyo.ac.jp> <1992Aug25.152315.20630@news.eng.convex.com> <BtJux7.CEp@news.cso.uiuc.edu>
  12. Nntp-Posting-Host: pixel.convex.com
  13. Organization: Convex Computer Corporation, Colorado Springs, CO
  14. X-Disclaimer: This message was written by a user at CONVEX Computer
  15.               Corp. The opinions expressed are those of the user and
  16.               not necessarily those of CONVEX.
  17. Lines: 284
  18.  
  19. From the keyboard of ejk@ux2.cso.uiuc.edu (Ed Kubaitis - CCSO):
  20. :For the widest acceptance the style guide probably shouldn't rush in 
  21. :where K&R feared to tread;-)
  22.  
  23. There's some merit in having everyone working on one piece of
  24. source code use the same conventions, whatever they may be.
  25.  
  26. I'm actually thinking more of things like conventions regarding 
  27. comments and variable declarations than I am about braces.  
  28.  
  29. However, a special hell I reserve for those who write C this way:
  30.  
  31. char *func(s)
  32. char *s
  33. {
  34. int i;
  35. if (condition)
  36.     {
  37.     statement1;
  38.     statement2;
  39.     statement3;
  40.     }
  41. return i;
  42. }
  43.  
  44.  
  45. Yes, that's right, I have to read code like that, and no, I didn't strip
  46. any indentation.
  47.  
  48. Here's the Berkeley style guide for comparison.  I don't personally
  49. use all of them (i.e. I use 4-space indents and test pointers with 
  50. a raw "if (p)') but surprisgnly, I find that many I do use even 
  51. though I had never read this guide.
  52.  
  53. --tom
  54.  
  55.  
  56. /*
  57.  * Style guide for BSD's KNF (Kernel Normal Form).
  58.  *
  59.  *    @(#)style    1.9 (Berkeley) 12/10/91
  60.  */
  61.  
  62. /*
  63.  * VERY important single-line comments look like this.
  64.  */
  65.  
  66. /* Most single-line comments look like this. */
  67.  
  68. /*
  69.  * Multi-line comments look like this.  Make them real sentences.  Fill
  70.  * them so they look like real paragraphs.
  71.  */
  72.  
  73. /* Include files go at the top of the source module. */
  74. #include <stdio.h>        /* Non-local includes in brackets. */
  75.  
  76. /*
  77.  * Global pathnames are defined in /usr/include/paths.h.  Pathnames local
  78.  * to the program go in pathnames.h in the local directory.
  79.  */
  80. #include <paths.h>        /* Non-local includes in brackets. */
  81. #include "pathnames.h"        /* Local includes in quotes. */        
  82.  
  83. /*
  84.  * All ANSI function decls go at the top of the source module.  Use the
  85.  * __P macro from include file <sys/cdefs.h>.  Only the kernel has a name
  86.  * associated with the types, i.e. in the kernel use:
  87.  *
  88.  *    void function __P((int a));
  89.  *
  90.  * in user land use:
  91.  *
  92.  *    void function __P((int));
  93.  */
  94. void function __P((int, const char *));
  95.  
  96. /*
  97.  * Macros are capitalized, parenthesized, and should avoid side-effects.
  98.  * If they are an inline expansion of a function, the function is defined
  99.  * all in lowercase, the macro has the same name all in uppercase. If the
  100.  * macro needs more than a single line, use braces.  Put a space before
  101.  * the backslashes.
  102.  */
  103. #define    MACRO(x, y) { \
  104.     variable = (x) + (y); \
  105.     line two; \
  106. }
  107.  
  108. /* Enum types are capitalized. */
  109. enum enumtype { ONE, TWO } et;
  110.  
  111. /*
  112.  * When declaring variables in structures, declare them sorted by use, then
  113.  * by size, and then by alphabetical order.  The first category normally
  114.  * doesn't apply, but there are exceptions.  Each one gets its own line.
  115.  * Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;".
  116.  *
  117.  * Major structures should be declared at the top of the file they are
  118.  * used in, or in separate header files, if they are used in multiple
  119.  * source files. Use of the structures should be by separate declarations
  120.  * and should be "extern" if they are declared in a header file.
  121.  */
  122. struct foo {
  123.     struct    foo *next;    /* List of active foo */
  124.     struct    mumble amumble;    /* Comment for mumble */
  125.     int    bar;
  126. };
  127. struct foo *foohead;        /* Head of global foo list */
  128.     
  129. /*
  130.  * All major routines should have a comment briefly describing what
  131.  * they do. The comment before the "main" routine should describe
  132.  * what the program does.
  133.  */
  134. main(argc, argv)
  135.     int argc;
  136.     char *argv[];
  137. {
  138.     extern char *optarg;
  139.     extern int optind;
  140.     long num;
  141.     int ch;
  142.     char *ep;
  143.  
  144.     /*
  145.      * For consistency, getopt should be used to parse options.
  146.      * Options should be sorted in the getopt call and the switch
  147.      * statement, unless they fall through.  Elements in a switch
  148.      * statement that fall through should have a FALLTHROUGH comment.
  149.      * Numerical arguments should be checked for accuracy.
  150.      */
  151.     while ((ch = getopt(argc, argv, "abn")) != EOF)
  152.         switch (ch) {        /* Indent the switch. */
  153.         case 'a':        /* Don't indent the case. */
  154.             aflag = 1;
  155.             /* FALLTHROUGH */
  156.         case 'b':
  157.             bflag = 1;
  158.             break;
  159.         case 'n':
  160.             num = strtol(optarg, &ep, 10);
  161.                         if (num <= 0 || *ep)
  162.                                 err("illegal number -- %s", optarg);
  163.             break;
  164.         case '?':
  165.         default:
  166.             usage();
  167.         }
  168.     argc -= optind;
  169.     argv += optind;
  170.  
  171.     /*
  172.      * Space after keywords (while, for, return, switch).  No braces are
  173.      * used for single statement block.
  174.      *
  175.      * Forever loops are done with for's, not while's.
  176.      */
  177.     for (;;)
  178.         stmt;
  179.     
  180.     /*
  181.      * Parts of a for loop may be left empty.  Avoid declarations in
  182.      * blocks unless the routine is unusually complicated.
  183.      */
  184.     for (; cnt < 15; cnt++) {
  185.         stmt1;
  186.         stmt2;
  187.     }
  188.  
  189.     while (cnt < 20) {
  190.         stmt1;        /* Second level indents are four spaces. */
  191.         z = a + really + long + statment + that + needs + two lines +
  192.             gets + indented + four + spaces + on + the + second +
  193.             and + subsequent + lines.
  194.     }
  195.  
  196.     /*
  197.      * Try to put shorter part first.  The closing and opening braces
  198.      * go on the same line as the else.
  199.      */
  200.     if (test)
  201.         stmt;
  202.     else if (bar) {
  203.         stmt;
  204.         stmt;
  205.     } else
  206.         stmt;
  207.         
  208.     /* No space after function names. */
  209.     if (error = function(a1, a2))
  210.         exit(error);
  211.  
  212.     /*
  213.      * Unary operators do not require spaces, binary operators do.
  214.      * Try not to use too many parenthesis unless the statement is
  215.      * really confusing without them.
  216.      */
  217.     a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
  218.     k = l & FLAGS;
  219.  
  220.     /*
  221.      * Exits should be 0 on success, and 1 on failure.  Don't denote
  222.      * all the possible exit points, using the integers 1 through 300.
  223.      */
  224.     exit(0);    /* Avoid obvious comments such as "Exit 0 on success." */
  225. }
  226.  
  227. /*
  228.  * If a function type is declared, it should be on a line
  229.  * by itself preceeding the function.
  230.  */
  231. static char *
  232. function(a1, a2, a3, a4)
  233.     int a1, a2, a4;    /* Declare ints too. */
  234.     float a3;    /* List in order declared, as much as possible. */
  235. {
  236.     /*
  237.      * When declaring variables in functions declare them sorted by size,
  238.      * then in alphabetical order; multiple ones per line are okay.  Old
  239.      * style function declarations can go on the same line.  ANSI style
  240.      * function declarations should go in the include file "externs.h".
  241.      * If a line overflows reuse the type keyword.
  242.      *
  243.      * In general, don't initialize variables in the declarations.
  244.      */
  245.     extern u_char one;
  246.     extern char two;
  247.     struct foo three, *four;
  248.     double five;
  249.     int *six, seven, eight();
  250.     char *nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen;
  251.     char *overflow();
  252.     void *malloc();
  253.  
  254.     /*
  255.      * Casts and sizeof's are not followed by a space.  NULL is any
  256.      * pointer type, and doesn't need to be cast, so use NULL instead
  257.      * of (struct foo *)0 or (struct foo *)NULL.  Also, test pointers
  258.      * against NULL, i.e. use:
  259.      *
  260.      *     (p = f()) == NULL
  261.      * not:
  262.      *    !(p = f())
  263.       *
  264.      * Routines returning void * should not have their return values cast
  265.      * to any pointer type.
  266.      */
  267.     if ((four = malloc(sizeof(struct foo))) == NULL)
  268.         return (NULL);
  269.     if ((six = (int *)overflow()) == NULL)
  270.         return (NULL);
  271.     return (eight);
  272. }
  273.  
  274. static void
  275. usage()
  276. {    /* Insert an empty line if the function has no variables. */
  277.  
  278.     /*
  279.      * Use printf(3), not fputs/puts/putchar/whatever.
  280.      *
  281.      * Usage statements should look like the manual pages.  Options w/o
  282.      * operands come first, in alphabetical order inside a single set of
  283.      * braces.  Followed by options with operands, in alphabetical order,
  284.      * each in braces.  Followed by required arguments in the order they
  285.      * are specified, followed by optional arguments in the order they
  286.      * are specified.  A bar ('|') separates either/or options/arguments,
  287.      * and multiple options/arguments which are specified together are
  288.      * placed in a single set of braces.
  289.      *
  290.      * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
  291.      * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
  292.      */
  293.     (void)fprintf(stderr, "usage: f [-ab]\n");
  294.     exit(1);
  295. }
  296.  
  297.  
  298. -- 
  299.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  300.  
  301.  
  302.               A truly wise man never plays leapfrog with a unicorn.
  303.