Changes between Initial Version and Version 1 of CodingGuidelines


Ignore:
Timestamp:
10/31/10 17:28:09 (9 years ago)
Author:
magnate
Comment:

Initial paste from rephial.org wiki, no changes

Legend:

Unmodified
Added
Removed
Modified
  • CodingGuidelines

    v1 v1  
     1This document describes what Angband code and its documentation should look like.  It is borrowed in part from the Angband Coding Guidelines by Robert Ruehlmann, Ben Harrison, and Gwidon S. Naskrent.  You may also want to read the [Angband security guide] (http://thangorodrim.net/development/security.txt). 
     2 
     3Use the [BSD/Allman style] (http://en.wikipedia.org/wiki/Indent_style#BSD.2FAllman_style), and indent using tabs.  Avoid lines over 80 character long (though this is not strict). 
     4 
     5This means: 
     6 
     7 * Curly braces for code blocks should always be on seperate lines, with the same indentation level as the previous line. 
     8 * Spaces around the mathematical, comparison, and assignment operators ('+', '-', '/', '=', '!=', '==', '>', ...).  No spaces around increment/decrement operators ('++', '--'). 
     9 * Spaces between C identifiers like 'if', 'while' and 'for' and the opening brackets ('if (foo)', 'while (bar)', ...). 
     10 * `do { } while ();` loops should have a newline after "do", and the "} while ();" bit should be on the same line. 
     11 * No spaces between function names and brackets and between brackets and function arguments (function(1, 2) instead of function ( 1, 2 )). 
     12 * If you have an if statement whose conditionally executed code is only one statement, do not write both on the same line, except in the case of "break" or "continue" in loops. 
     13 * `return` does not use brackets, `sizeof` does. 
     14 
     15Example: 
     16    if (fridge) 
     17    { 
     18      int i = 10; 
     19 
     20      if (foo > sizeof(int)) 
     21        bar(1, 2); 
     22 
     23      do 
     24      { 
     25        /* Only print even numbers */ 
     26        if (i % 2) continue; 
     27 
     28        /* Be clever */ 
     29        printf("Aha!"); 
     30      } while (i--); 
     31 
     32      p++; 
     33      x += 4; 
     34 
     35      return 5; 
     36    } 
     37 
     38 
     39Also, write modern C code.  Include parenthesis wherever it increases readability, and nowhere else.  Always declare functions in as <type> <name>(<params>).  If you don't pass any parameters, make sure you write 'void' in the declaration.  Use the 'const' wherever you don't modify a variable. 
     40 
     41Write code for humans first and execution second.  Where code is unclear, comment, but there is no need to write such as the following: 
     42 
     43    /* Delete the object */ 
     44    object_delete(idx); 
     45 
     46It is unneccessarily verbose and hurts readability.  Group code instead into ideas and comment each section of ideas.  Also, use whitespace liberally to visually seperate ideas. 
     47 
     48Each function should have a comment at its head, describing the function and including any notes on its usage, including what its various return values (if any) mean.  Avoid multi-line comments in functions; instead, split the code that requires the comments into its own function or move the comment into the comment section at the top of the function. 
     49 
     50Write code as modularly as possible.  Name functions and global variables inside a module with the same prefix, like "macro_".  Avoid global variables where possible, and don't expose them to the rest of the code unnecessarily. 
     51 
     52If you make use of memory allocation in your module, try and make such code clean up after itself, or include it in "init" and "free" functions.  Always initialise your module so it is in a known state!  It is possible that the game will eventually not quit when you lose a character, but rather allow another character to be loaded or created.  Unless you get things in a known state, this will cause crashes. 
     53 
     54Don't use magic numbers.  Use defines or enums (and enums where possible).  Where a constant is local, include it at the top of a file/section.  Where it is not, place it in the module's corresponding header file.  Avoid putting more things into defines.h. 
     55 
     56Don't use floating point calculations. 
     57 
     58Code should compile cleanly as strict C89 on release and should not rely on undefined behaviour.  (Angband has, for years, been a well-defined piece of software, and no-one implements C99 fully yet.) 
     59 
     60Never use `strcpy()`, `strcat()`, or `sprintf()`.  Instead, use my_strcpy, my_strcat and strnfmt, which are safer and will avoid accidental trashing of memory.  Similarly, avoid strncat/strncpy unless absolutely necessary, as using them is error-prone. 
     61 
     62Documentation 
     63============= 
     64 
     65Be careful when documenting functions to use the following design: 
     66 
     67    /** 
     68     * Provides an example of a documentation style. 
     69     * 
     70     * The purpose of the function do_something() is explained here, mentioning 
     71     * the name and use of every parameter (e.g. `example`).  It returns TRUE if 
     72     * conditions X or Y are met, and FALSE otherwise. 
     73     * 
     74     * BUG: Brief description of bug. (#12345) 
     75     * TODO: Feature to implement. (#54321) 
     76     */ 
     77    bool do_something(void *example) 
     78 
     79Additional notes about the format: 
     80 - Having the brief description separated out from the remainder of the comment means that Doxygen can pull it out without needing @brief tags. 
     81 - Variables should be referred to with surrounding backtick ('`') quotes. 
     82 - Functions should be referred to as function_name() -- /with/ the brackets. 
     83 - In brief descriptions of classes and functions, use present tense (i.e. answer the question "What does this do?" with "It constructs / edits / calculates / returns...") 
     84 - In long descriptions, use passive mood to refer to variables (i.e. "The variables are normalised." as opposed to "This normalises the variables.") 
     85 - No UK/US spelling preference (i.e. the preference of the first commenter is adopted for that comment). 
     86 - (from "The Elements of Style") "A sentence should contain no unnecessary words, a paragraph no unnecessary sentences, for the same reason that a drawing should have no unnecessary lines and a machine no unnecessary parts. This requires not that the writer make all his sentences short, or that he avoid all detail and treat his subjects only in outline, but that every word tell."