home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / standards.text < prev    next >
Text File  |  1991-03-04  |  21KB  |  530 lines

  1. GNU Coding Standards                last updated 1 May 90
  2.  
  3. Reference standards:
  4.  
  5. Don't in any circumstances refer to Unix source code for or during
  6. your work on GNU!  (Or to any other proprietary programs.)
  7.  
  8. If you have a vague recollection of the internals of a Unix program,
  9. this does not absolutely mean you can't write an imitation of it, but
  10. do try to organize the imitation internally along different lines,
  11. because this is likely to make the details of the Unix version
  12. irrelevant and dissimilar to your results.
  13.  
  14. For example, Unix utilities were generally optimized to minimize
  15. memory use; if you go for speed instead, your program will be very
  16. different.  You could keep the entire input file in core and scan it
  17. there instead of using stdio.  Use a smarter algorithm discovered more
  18. recently than the Unix program.  Eliminate use of temporary files.  Do
  19. it in one pass instead of two (we did this in the assembler).
  20.  
  21. Or, on the contrary, emphasize simplicity instead of speed.  For some
  22. applications, the speed of today's computers makes simpler algorithms
  23. adequate.
  24.  
  25. Or go for generality.  For example, Unix programs often have static
  26. tables or fixed-size strings, which make for arbitrary limits; use
  27. dynamic allocation instead.  Make sure your program handles nulls and
  28. other funny characters in the input files.  Add a programming language
  29. for extensibility and write part of the program in that language.
  30.  
  31. Or turn some parts of the program into independently usable libraries.
  32. Or use a simple garbage collector instead of tracking precisely when
  33. to free memory, or use a new GNU facility such as obstacks.
  34.  
  35. Other Contributors:
  36.  
  37. If someone else sends you a piece of code to add to the program you
  38. are working on, we need legal papers to use it--the same sort of legal
  39. papers we will need to get from you.  EACH significant contributor to
  40. a program must sign some sort of legal papers in order for us to have
  41. clear title to the program.  The main author alone is not enough.
  42.  
  43. So, before adding in any contributions from other people, tell us
  44. so we can arrange to get the papers.  Then wait until we tell you
  45. that we have received the signed papers, before you actually use the
  46. contribution.
  47.  
  48. This applies both before you release the program and afterward.  If
  49. you receive diffs to fix a bug, and they make significant change, we
  50. need legal papers for it.
  51.  
  52. You don't need papers for changes of a few lines here or there, since
  53. they are not significant for copyright purposes.  Also, you don't need
  54. papers if all you get from the suggestion is some ideas, not actual code
  55. which you use.  For example, if you write a different solution to the
  56. problem, you don't need to get papers.
  57.  
  58. I know this is frustrating; it's frustrating for us as well.  But if
  59. you don't wait, you are going out on a limb--for example, what if the
  60. contributor's employer won't sign a disclaimer?  You might have to take
  61. that code out again!
  62.  
  63. The very worst thing is if you forget to tell us about the other
  64. contributor.  We could be very embarrassed in court some day as a
  65. result.
  66.  
  67. Compatibility standards:
  68.  
  69. With certain exceptions, utility programs and libraries for GNU should
  70. be upward compatible with those in Berkeley Unix, and upward
  71. compatible with ANSI C if ANSI C specifies them, and upward compatible
  72. with POSIX if POSIX specifies them.
  73.  
  74. When these standards conflict, it is useful to offer compatibility
  75. modes for each of them.
  76.  
  77. ANSI C and POSIX prohibit many kinds of extensions.  Feel free to
  78. make the extensions anyway, and include a -ansi or -compatible option
  79. to turn them off.  However, if the extension has a significant chance
  80. of breaking any real programs or scripts, then it is not really upward
  81. compatible.  Try to redesign its interface.
  82.  
  83. When a feature is used only by users (not by programs or command
  84. files), and it is done poorly in Unix, feel free to replace it
  85. completely with something totally different and better.  (For example,
  86. vi is replaced with Emacs.)  But it is nice to offer a compatible
  87. feature as well.  (There is a free vi-clone, so we will offer it.)
  88.  
  89. Additional useful features not in Berkeley Unix are welcome.
  90. Additional programs with no counterpart in Unix may be useful,
  91. but our first priority is usually to duplicate what Unix already
  92. has.
  93.  
  94. Formatting standards:
  95.  
  96. It is important put the open-brace that starts the body of a C function
  97. in column zero, and avoid putting any other open-brace or open-parenthesis
  98. or open-bracket in column zero.  Several tools look for
  99. open-braces in column zero to find the beginnings of C functions.
  100. These tools will not work on code not formatted that way.
  101.  
  102. It is also important for function definitions to start the name
  103. of the function in column zero.  `ctags' or `etags' cannot recognize
  104. them otherwise.  Thus, the proper format is this:
  105.  
  106. static char *
  107. concat (s1, s2)        /* Name starts in column zero here */
  108.      char *s1, *s2;
  109. {               /* Open brace in column zero here */
  110.   ...
  111. }
  112.  
  113. or, if you want to use ANSI C, format the definition like this:
  114.  
  115. static char *
  116. concat (char *s1, char *s2)
  117. {
  118.   ...
  119. }
  120.  
  121. In ANSI C, if the arguments don't fit nicely on one line,
  122. split it like this:
  123.  
  124. int
  125. lots_of_args (int an_integer, long a_long, short a_short,
  126.               double a_double, float a_float)
  127. ...
  128.  
  129. For the body of the function, we prefer code formatted like this:
  130.  
  131.   if (x < foo (y, z))
  132.     haha = bar[4] + 5;
  133.   else
  134.     {
  135.       while (z)
  136.         {
  137.       haha += foo (z, z);
  138.       z--;
  139.         }
  140.       return ++x + bar ();
  141.     }
  142.  
  143. We find it easier to read a program when it has spaces before the
  144. open-parentheses and after the commas.  Especially after the commas.
  145.  
  146. When you split an expression into multiple lines, split it
  147. before an operator, not after one.  Here is the right way:
  148.  
  149.     if (foo_this_is_long && bar > win (x, y, z)
  150.     && remaining_condition)
  151.  
  152. Try to avoid having two operators of different precedence at the same
  153. level of indentation.  For example, don't write this:
  154.  
  155.       mode = (inmode[j] == VOIDmode
  156.           || GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j])
  157.           ? outmode[j] : inmode[j]);
  158.  
  159. Instead, use extra parentheses so that the indentation shows the nesting:
  160.  
  161.       mode = ((inmode[j] == VOIDmode
  162.            || (GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j])))
  163.           ? outmode[j] : inmode[j]);
  164.  
  165. Insert extra parentheses so that Emacs will indent the code properly.
  166. For example, the following indentation looks nice if you do it by hand,
  167. but Emacs would mess it up:
  168.  
  169.     v = rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000
  170.     + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000;
  171.  
  172. But adding a set of parentheses solves the problem:
  173.  
  174.     v = (rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000
  175.      + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000);
  176.  
  177. Format do-while statements like this:
  178.  
  179.    do
  180.      {
  181.        a = foo (a);
  182.      }
  183.    while (a > 0);
  184.  
  185. Please use formfeed characters (^L) to divide the program into pages
  186. at logical places (but not within a function).  It does not matter
  187. just how long the pages are, since they do not have to fit on a
  188. printed page.  The formfeeds should appear alone on their lines,
  189. just as they do in this file.
  190.  
  191. Commenting Standards:
  192.  
  193. Every program should start with a comment saying briefly
  194. what it is for.  Example:  "fmt -- filter for simple filling of text".
  195.  
  196. Please put a comment on each function saying what the function does,
  197. what sorts of arguments it gets, and what the possible values of
  198. arguments mean and are used for.  It is not necessary to duplicate in
  199. words the meaning of the C argument declarations, if a C type is being
  200. used in its customary fashion.  If there is anything nonstandard about
  201. its use (such as an argument of type `char *' which is really the
  202. address of the second character of a string, not the first), or any
  203. possible values that would not work the way one would expect (such as,
  204. that strings containing newlines are not guaranteed to work), be sure
  205. to say so.
  206.  
  207. Also explain the significance of the return value, if there is one.
  208.  
  209. Please put two spaces after the end of a sentence in your comments, so
  210. that the Emacs sentence commands will work.  Also, please write
  211. complete sentences and capitalize the first word.  If a lower-case
  212. identifer comes at the beginning of a sentence, don't capitalize it!
  213. Changing the spelling makes it a different identifier.  If you don't
  214. like starting a sentence with a lower case letter, write the sentence
  215. differently (e.g. "The identifier lower-case is ...").
  216.  
  217. The comment on a function is much clearer if you use the argument
  218. names to speak about the argument values.  The variable name itself
  219. should be lower case, but write it in upper case when you are speaking
  220. about the value rather than the variable itself.  Thus, "the inode
  221. number NODE_NUM" rather than "an inode".
  222.  
  223. There is usually no purpose in restating the name of the function in
  224. the comment before it, because the reader can see that for himself.
  225. There might be an exception when the comment is so long that the function
  226. itself would be off the bottom of the screen.
  227.  
  228. There should be a comment on each static variable as well, like this:
  229.  
  230.     /* Nonzero means truncate lines in the display;
  231.        zero means continue them.  */
  232.  
  233.     int truncate_lines;
  234.  
  235. Every #endif should have a comment, except in the case of short conditionals
  236. (just a few lines) that are not nested.  The comment should state the condition
  237. of the conditional that is ending, *including its sense*.  #else should have
  238. a comment describing the condition *and sense* of the code that follows.
  239. For example:
  240.  
  241.     #ifdef foo
  242.       ...
  243.     #else /* not foo */
  244.       ...
  245.     #endif /* not foo */
  246.  
  247. but
  248.  
  249.     #ifndef foo
  250.       ...
  251.     #else /* foo */
  252.       ...
  253.     #endif /* foo */
  254.  
  255. Syntactic Standards:
  256.  
  257. Please explicitly declare all arguments to functions.
  258. Don't omit them just because they are ints.
  259.  
  260. Declarations of external functions and functions to appear later
  261. in the source file should all go in one place near the beginning of
  262. the file (somewhere before the first function definition in the file),
  263. or else should go in a header file.  Don't put extern declarations
  264. inside functions.
  265.  
  266. Don't declare multiple variables in one declaration that spans lines.
  267. Start a new declaration on each line, instead.  For example, instead
  268. of this:
  269.  
  270.    int    foo,
  271.           bar;
  272.  
  273. write either this:
  274.  
  275.    int foo, bar;
  276.  
  277. or this:
  278.  
  279.    int foo;
  280.    int bar;
  281.  
  282. (If they are global variables, each should have a comment
  283. preceding it anyway.)
  284.  
  285. When you have an if-else statement nested in another if statement,
  286. always put braces around the if-else.  Thus, never write like this:
  287.  
  288.     if (foo)
  289.       if (bar)
  290.     win ();
  291.       else
  292.     lose ();
  293.  
  294. always like this:
  295.  
  296.     if (foo)
  297.       {
  298.     if (bar)
  299.       win ();
  300.     else
  301.       lose ();
  302.       }
  303.  
  304. Don't declare both a structure tag and variables or typedefs in the
  305. same declaration.  Instead, declare the structure tag separately
  306. and then use it to declare the variables or typedefs.
  307.  
  308. Try to avoid assignments inside if-conditions.  For example, don't
  309. write this:
  310.  
  311.   if ((foo = (char *) malloc (sizeof *foo)) == 0)
  312.     fatal ("virtual memory exhausted");
  313.  
  314. instead, write this:
  315.  
  316.   foo = (char *) malloc (sizeof *foo);
  317.   if (foo == 0)
  318.     fatal ("virtual memory exhausted");
  319.  
  320. Don't make the program ugly to placate lint.  Please don't insert any
  321. casts to void.  Zero without a cast is perfectly fine as a null
  322. pointer constant.
  323.  
  324. Naming Standards:
  325.  
  326. Please use underscores to separate words in a name,
  327. so that the Emacs word commands can be useful within them.
  328. Stick to lower case; reserve upper case for macros and enum
  329. constants, and for name-prefixes that follow a uniform convention.
  330.  
  331. For example, use names like `ignore_space_change_flag';
  332. don't use names like `iCantReadThis'.
  333.  
  334. Variables that indicate whether command-line options have been
  335. specified should be named after the meaning of the option, not after
  336. the option-letter.  A comment should state both the exact meaning of
  337. the option and its letter.  For example,
  338.  
  339.     /* Ignore changes in horizontal whitespace (-b).  */
  340.     int ignore_space_change_flag;
  341.  
  342. When you want to define names with constant integer values, use `enum'
  343. rather than `#define'.  GDB knows about enumeration constants.
  344.  
  345. Use file names of 14 characters or less, to avoid creating gratuitous
  346. problems on System V.
  347.  
  348. Semantic Standards:
  349.  
  350. Avoid arbitrary limits on the length or number of *any* data structure,
  351. including filenames, lines, files, and symbols, by allocating all
  352. data structures dynamically.  In most Unix utilities, "long lines
  353. are silently truncated".  This is not acceptable in a GNU utility.
  354.  
  355. Utilities reading files should not drop null characters, or any other
  356. nonprinting characters including those with codes above 0177, except
  357. perhaps utilities specifically intended for interface to printers.
  358.  
  359. Check every system call for an error return, unless you know you
  360. wish to ignore errors.  Include the system error text (from perror
  361. or equivalent) in *every* error message resulting from a failing
  362. system call, as well as the name of the file if any and the
  363. name of the utility.  Just "cannot open foo.c" or "stat failed"
  364. is not sufficient.
  365.  
  366. Check every call to `malloc' or `realloc' to see if it returned zero.
  367. Check `realloc' even if you are making the block smaller; in a system
  368. that rounds block sizes to a power of 2, `realloc' may get a different
  369. block if you ask for less space.
  370.  
  371. In Unix, `realloc' can destroy the storage block if it returns zero.
  372. GNU `realloc' does not have this bug: if it fails, the original block
  373. is unchanged.  Feel free to assume the bug is fixed.  If you wish to
  374. run your program on Unix, and wish to avoid lossage in this case, you
  375. can use the GNU `malloc'.
  376.  
  377. You must expect `free' to alter the contents of the block that was
  378. freed.  Anything you want to fetch from the block, you must fetch
  379. before calling `free'.
  380.  
  381. Use `getopt' to decode arguments, unless the argument syntax makes this
  382. unreasonable.
  383.  
  384. When static storage is to be written in during program execution,
  385. use explicit C code to initialize it.  Reserve C initialized
  386. declarations for data that will not be changed.
  387.  
  388. Try to avoid low-level interfaces to obscure Unix data structures
  389. (such as file directories, utmp, or the layout of kernel memory),
  390. since these are less likely to work compatibly.  If you need to
  391. find all the files in a directory, use `readdir' or some other
  392. high-level interface.  These will be supported compatibly by GNU.
  393.  
  394. GNU signal handling will probably be like that in BSD, rather than
  395. that in system V, because BSD's is more powerful and easier to use.
  396.  
  397. In error checks that detect "impossible" conditions, just abort.
  398. There is usually no point in printing any message.  These checks
  399. indicate the existence of bugs.  Whoever wants to fix the bugs will
  400. have to read the source code and run a debugger.  So explain the
  401. problem with comments in the source.  The relevant data will be in
  402. variables, which are easy to examine with the debugger, so there is no
  403. point moving them elsewhere.
  404.  
  405. Library standards:
  406.  
  407. Try to make library functions reentrant.  If they need to do dynamic
  408. storage allocation, at least try to avoid any nonreentrancy aside from
  409. that of malloc itself.
  410.  
  411. Here are certain name conventions for libraries, to avoid name
  412. conflicts.
  413.  
  414. Choose a name prefix for the library, more than two characters long.
  415. All external function and variable names should start with this
  416. prefix.  In addition, there should only be one of these in any given
  417. library member.  This usually means putting each one in a separate
  418. source file.
  419.  
  420. An exception can be made when two external symbols are always used
  421. together, so that no reasonable program could use one without the
  422. other; then they can both go in the same file.
  423.  
  424. External symbols that are not documented entry points for the user
  425. should have names beginning with `_'.  They should also contain
  426. the chosen name prefix for the library, to prevent collisions with
  427. other libraries.  These can go in the same files with user entry 
  428. points if you like.
  429.  
  430. Static functions and variables can be used as you like and need not
  431. fit any naming convention.
  432.  
  433. Portability standards:
  434.  
  435. Much of what is called "portability" in the Unix world refers to
  436. porting to different Unix versions.  This is not relevant to GNU
  437. software, because its purpose is to run on top of one and only
  438. one kernel, the GNU kernel, compiled with one and only one C
  439. compiler, the GNU C compiler.  The amount and kinds of variation
  440. among GNU systems on different cpu's will be like the variation
  441. among Berkeley 4.3 systems on different cpu's.
  442.  
  443. It is difficult to be sure exactly what facilities the GNU kernel will
  444. provide, since it isn't finished yet.  Therefore, assume you can use
  445. anything in 4.3; just avoid using the format of semi-internal data
  446. bases (e.g., directories) when there is a higher-level alternative
  447. (readdir).
  448.  
  449. You can freely assume any reasonably standard facilities in the C
  450. language, libraries or kernel, because we will find it necessary to
  451. support these facilities in the full GNU system, whether or not we
  452. have already done so.  The fact that there may exist kernels or C
  453. compilers that lack these facilities is irrelevant as long as the GNU
  454. kernel and C compiler support them.
  455.  
  456. It remains necessary to worry about differences among cpu types, such
  457. as the difference in byte ordering and alignment restrictions.  It's
  458. unlikely that 16-bit machines will ever be supported by GNU, so there
  459. is no point in spending any time to consider the possibility that an
  460. int will be less than 32 bits.
  461.  
  462. You can assume that all pointers have the same format, regardless
  463. of the type they point to, and that this is really an integer.
  464. There are some weird machines where this isn't true, but they aren't
  465. important; don't waste time catering to them.  Besides, eventually
  466. we will put function prototypes into all GNU programs, and that will
  467. probably make your program work even on weird machines.
  468.  
  469. Since some important machines (including the 68000) are big-endian,
  470. it is important not to assume that the addres of an int object
  471. is also the address of its least-significant byte.  Thus, don't
  472. make the following mistake:
  473.  
  474.                 int c;
  475.                 ...
  476.                 while ((c = getchar()) != EOF)
  477.                         write(file_descriptor, &c, 1);
  478.  
  479. You can assume that it is reasonable to use a meg of memory.  Don't
  480. strain to reduce memory usage unless it can get to that level.  If
  481. your program creates complicated data structures, just make them in
  482. core and give a fatal error if malloc returns zero.
  483.  
  484. If a program works by lines and could be applied to arbitrary user-
  485. supplied input files, it should keep only a line in memory, because
  486. this is not very hard and users will want to be able to operate
  487. on input files that are bigger than will fit in core all at once.
  488.  
  489. Utility Interface Standards:
  490.  
  491. Please don't make the behavior of a utility depend on the name used
  492. to invoke it.  It is useful sometimes to make a link to a utility
  493. with a different name, and that should not change what it does.
  494.  
  495. Instead, use a run time option or a compilation switch or both
  496. to select among the alternate behaviors.
  497.  
  498. It is a good idea to follow the Posix guidelines for the command-line
  499. options of a program.  The easiest way to do this is to use getopt to
  500. parse them.  Note that the GNU version of getopt will normally permit
  501. options anywhere among the arguments unless the special argument `--' 
  502. is used.  This is not what Posix specifies; it is a GNU extension.
  503.  
  504. Please define long-named options that are equivalent to the
  505. single-letter Unix-style options.  We hope to make GNU more user
  506. friendly this way.  This is easy to do with the GNU version of
  507. getopt.
  508.  
  509. It is usually a good idea for file names given as ordinary arguments
  510. to be input files only; any output files would be specified using
  511. options (preferably -o).  Even if you allow an output file name as an
  512. ordinary argument for compatibility, try to provide a suitable option
  513. as well.  This will lead to more consistency among GNU utilities, so
  514. that there are fewer idiosyncracies for users to remember.
  515.  
  516. Documentation Standards:
  517.  
  518. Please use Texinfo for documenting GNU programs.  See the Texinfo
  519. manual, either the hardcopy or the version in the GNU Emacs Info
  520. sub-system (C-h i).
  521.  
  522. See existing GNU texinfo files (e.g. those under the man/ directory in
  523. the GNU Emacs Distribution) for examples.
  524.  
  525. Write well.  Document all -switches and their interactions.  Document
  526. all commands.  Give examples of their use.  Tell people how and when
  527. to use the features for what they typically want to accomplish, not
  528. just what each feature does.
  529.  
  530.