home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 January / enter-2004-01.iso / files / freeciv-1.14.0-win32-sound.exe / {app} / doc / CodingStyle.txt < prev    next >
Encoding:
Text File  |  2003-01-21  |  6.0 KB  |  197 lines

  1. ===========================================================================
  2.  Freeciv Style Guide
  3. ===========================================================================
  4.  
  5. If you want to hack Freeciv, and want your patches to be accepted, it
  6. helps to follow some simple style rules. Yes, some of these are a bit 
  7. nit-picky, but wars are fought over the silliest things...
  8.  
  9. - Freeciv is programmed in plain C (except the BEOS client). This
  10.   means that C++ features like:
  11.     - C++-style comments (i.e., // comments) and
  12.     - declaration variables in the middle of the function body
  13.   are forbidden.
  14.  
  15. - Use K&R indentation style with indentation 2 (if in doubt, use
  16.   "indent -kr -i2 -l77").  However, do not re-indent areas of code you
  17.   are not modifying or creating. Here are the most important properties:
  18.     - lines are at most 77 chars long
  19.     - spaces are inserted before and after operators ("int i, j, k;"
  20.       instead of "int a,b,c;" and "if (foo <= bar) c = a + b;" instead
  21.       of "if(foo<=bar) c=a+b;")
  22.     - function braces begin and end in the first column.
  23.         int foo()
  24.         {
  25.           return 0;
  26.         }
  27.       instead of
  28.         int foo() {
  29.           return 0;
  30.         }
  31.     - the tab width is 8
  32.     - the indentation is 2 columns per level
  33.  
  34. - An empty line should be placed between two separate blocks of code.
  35.  
  36. ================================
  37.  Comments
  38. ================================
  39.  
  40. - Every function should have a comment header. It should be above the 
  41.   function's implementation, not the prototype:
  42.  
  43. /*************************************************************************
  44.  the description of the function should be here
  45.  any information is helpful, such as who calls this function, etc.
  46.  do _not_ introduce a new function without some sort of comment.
  47. *************************************************************************/
  48. int the_function_starts_here(int value) 
  49. {
  50.   ...
  51. }
  52.  
  53. - One line comments. Comments should be indented correctly and 
  54.   placed above the code being commented upon:
  55.  
  56.   int x;
  57.  
  58.   /* I am a single line comment */
  59.   x = 3;
  60.  
  61. - Multiline comments. Asterisks should be placed in front of the 
  62.   comment line like so:
  63.  
  64.   /* I am a multiline
  65.    * comment, blah 
  66.    * blah blah */
  67.  
  68. - Comments in declarations. If you need to comment a declared variable,
  69.   it should be as such:
  70.  
  71.   struct foo {
  72.     int bar;                    /* bar is used for ....
  73.                                  * in ..... way */
  74.     int blah;                   /* blah is used for .... */
  75.   };
  76.  
  77. - Comments in conditionals. If you need a comment to show program flow,
  78.   it should be below the if or else:
  79.  
  80.   if(is_barbarian(pplayer)) {
  81.     x++;
  82.   } else {
  83.     /* If not barbarian, ... */
  84.     x--;
  85.   }
  86.  
  87. - Comments to translators are stored before the N_(), _() or Q_() marked
  88.   string, and are preceded by "TRANS:".  These comments are copied to the
  89.   translators file.  Use them whenever you think the translators may need
  90.   some more information:
  91.  
  92.     /* TRANS: Don't translate "commandname". */
  93.     printf(_("commandname <arg> [-o <optarg>]"));
  94.  
  95. ================================
  96.  Declaring variables
  97. ================================
  98.  
  99. - Variables can be initialized as soon as they're declared:
  100.  
  101. int foo(struct unit *punit)
  102. {
  103.   int x = punit->x;
  104.   int foo = x;
  105.   char *blah;
  106.  
  107.   ...
  108. }
  109.  
  110. - After variables are declared, there should be an empty line before the
  111.   rest of the function body.
  112.  
  113. - Merging declarations. Variables do not have to be declared one per line;
  114.   however, they should only be grouped by similar function.
  115.  
  116. int foo(struct city *pcity)
  117. {
  118.   int i, j, k;
  119.   int total, cost;
  120.   int build = pcity->shield_stock;
  121. }
  122.  
  123. ================================
  124.  Bracing
  125. ================================
  126.  
  127. - Extra braces on iterates. Note that the *_iterate_end; should be placed 
  128.   on the same line as the end brace:
  129.  
  130.   unit_list_iterate(pcity->units_supported, punit) {
  131.     kill(punit);
  132.   } unit_list_iterate_end;
  133.  
  134. - In switch statements, braces should only be placed where needed, i.e. to
  135.   protect local variables.
  136.  
  137. - Braces shall be used after conditionals:
  138.  
  139.   if (x == 3) {
  140.     return;
  141.   }
  142.  
  143.  and 
  144.  
  145.   if (x == 3) {
  146.     return 1;
  147.   } else {
  148.     return 0;
  149.   }
  150.  
  151.  not
  152.  
  153.   if (x == 3)
  154.     return 1;  /* BAD! */
  155.  
  156. ================================
  157.  Other stuff
  158. ================================
  159.  
  160. - If an empty block is needed you should put an explanatory comment in
  161.   an empty block (i.e. {}):
  162.  
  163.   while(*i++) {
  164.     /* nothing */
  165.   }
  166.  
  167. - Order include files consistently:  First state all system include
  168.   files with <> in alphabetic order, then all Freeciv include files
  169.   with "", sorted by directory (common, server, ...) and then by
  170.   alphabetic order, with a blank line between the sections.  This helps
  171.   to avoid adding unnecessary or duplicated include files.
  172.  
  173. - If you use a system specific feature, don't add #ifdef __CRAY__ or
  174.   something like that.  Rather write a check for that feature for
  175.   both configure.in and configure.ac, and use a meaningful macro name 
  176.   in the source.
  177.  
  178. - Always prototype global functions in the appropriate header file.
  179.   Local functions should always be declared as static. To catch and
  180.   some other problems please use the following warning options "-Wall
  181.   -Wpointer-arith -Wcast-align -Wmissing-prototypes -Wmissing-declarations 
  182.   -Wstrict-prototypes -Wnested-externs" if you use gcc.
  183.  
  184. - If you send patches, use "diff -u" (or "diff -r -u", or "cvs diff -u").
  185.   For further information, see <http://www.freeciv.org/contribute.html>.
  186.   Also, name patch files descriptively (e.g. "fix-foo-bug-0.diff" is good,
  187.   but "freeciv.diff" is not).
  188.  
  189. - When doing a "diff" for a patch, be sure to exclude unnecessary files
  190.   by using the "-X" argument to "diff".  E.g.:
  191.  
  192.     % diff -ruN -Xdiff_ignore freeciv_cvs freeciv >/tmp/fix-foo-bug-0.diff
  193.  
  194.   A suggested "diff_ignore" file is included in the Freeciv distribution.
  195.  
  196. ===========================================================================
  197.