Version 2 (modified by magnate, 9 years ago) (diff)

Update for 3.1.3

This 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 at, although the default build configuration no longer uses setgid.

Use the K&R style and indent using four spaces instead of tab. Avoid lines over 80 characters long, though this is not strict where there are multiple indents.

This means:

  • Opening braces should be on a separate line at the start of a function, but should otherwise follow the statement which requires them ('if', 'do', 'for' et al.)
  • Closing braces for should be on separate lines, except where followed by 'while' or 'else'
  • Spaces around the mathematical, comparison, and assignment operators ('+', '-', '/', '=', '!=', '==', '>', ...). No spaces around increment/decrement operators ('++', '--').
  • Spaces between C identifiers like 'if', 'while' and 'for' and the opening brackets ('if (foo)', 'while (bar)', ...).
  • do { } while (); loops should have a newline after "do {", and the "} while ();" bit should be on the same line.
  • No spaces between function names and brackets and between brackets and function arguments (function(1, 2) instead of function ( 1, 2 )).
  • 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.
  • return does not use brackets, sizeof does.


    if (fridge) {
      int i = 10;

      if (foo > sizeof(int))
        bar(1, 2);

      do {
        /* Only print even numbers */
        if (i % 2) continue;

        /* Be clever */
      } while (i--);

      x += 4;

      return 5;

Also, write modern C code. Include parenthesis wherever it increases readability, and nowhere else. Always declare functions 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.

Write code for humans first and execution second. Where code is unclear, comment, but there is no need to write such as the following:

    /* Delete the object */

It is unneccessarily verbose and hurts readability. Group code instead into ideas and comment each section of ideas. Also, use whitespace liberally to separate ideas visually.

Each 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.

Write 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.

If 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.

Don'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.

Don't use floating point calculations.

Code 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.) There are recent moves to allow mixed declarations and code, so you may see some of those warnings in recent versions.

Never 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.


Be careful when documenting functions to use the following design:

     * Provides an example of a documentation style.
     * The purpose of the function do_something() is explained here, mentioning
     * the name and use of every parameter (e.g. `example`).  It returns TRUE if
     * conditions X or Y are met, and FALSE otherwise.
     * BUG: Brief description of bug. (#12345)
     * TODO: Feature to implement. (#54321)
    bool do_something(void *example)

Additional notes about the format:

  • Having the brief description separated out from the remainder of the comment means that Doxygen can pull it out without needing @brief tags.
  • Variables should be referred to with surrounding backtick ('`') quotes.
  • Functions should be referred to as function_name() -- with the brackets.
  • 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...")
  • In long descriptions, use passive mood to refer to variables (i.e. "The variables are normalised." as opposed to "This normalises the variables.")
  • No UK/US spelling preference (i.e. the preference of the first commenter is adopted for that comment).
  • (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."