Coding Style
Not logged in

Fossil source code should following the style guidelines below.

General points:

  1. No line of code exceeds 80 characters in length. (Occasional exceptions are made for HTML text on @-lines.)
  1. There are no tab characters.
  1. Line terminators are \n only. Do not use a \r\n line terminator.
  1. 2-space indentation is used. Remember: No tabs.
  1. Comments contain no spelling or grammatical errors. (Abbreviations and sentence fragments are acceptable when trying to fit a comment on a single line as long as the meaning is clear.)
  1. The tone of comments is professional and courteous. Comments contain no profanity, obscenity, or innuendo.
  1. All C-code conforms to ANSI C-89.
  1. All comments and identifiers are in English.
  1. The program is single-threaded. Do not use threads. (One exception to this is the HTTP server implementation for Windows, which we do not know how to implement without the use of threads.)

C preprocessor macros:

  1. The purpose of every preprocessor macros is clearly explained in a comment associated with its definition.
  1. Every preprocessor macro is used at least once.
  1. The names of preprocessor macros clearly reflect their use.
  1. Assumptions about the relative values of related macros are verified by asserts. Example: assert(READ_LOCK+1==WRITE_LOCK);

Function header comments:

  1. Every function has a header comment describing the purpose and use of the function.
  1. Function header comment defines the behavior of the function in sufficient detail to allow the function to be re-implemented from scratch without reference to the original code.
  1. Functions that perform dynamic memory allocation (either directly or indirectly via subfunctions) say so in their header comments.

Function bodies:

  1. The name of a function clearly reflects its purpose.
  2. Automatic variables are small, not large objects or arrays. Avoid excessive stack usage.
  3. The check-list items for functions also apply to major subsections within a function.
  4. All code subblocks are enclosed in {...}.
  5. assert() macros are used as follows:
    1. Function preconditions are clearly stated and verified by asserts.
    2. Invariants are identified by asserts.

Class (struct) declarations:

  1. The purpose and use of every class (a.k.a. structure) is clearly defined in the header comment of its declaration.
  1. The purpose and use of every class member is clearly defined either in the header comment of the class declaration or when the member is declared or both.
  1. The names of class members clearly reflect their use.
  1. Invariants for classes are clearly defined.

Variables and class instances:

  1. The purpose and use of every variable is defined by a comment at the variable definition.
  1. The names of variables clearly reflect their use.
  1. Related variables have related names. (ex: aSavepoint and nSavepoint.)
  1. Variables have minimum practical scope.
  1. Automatic variables are small, not large objects or arrays.
  1. Constants are "const".
  1. Invariants on variables or groups of variables are defined and tested by asserts.
  1. When a variable that refers to the same value is used within multiple scopes, the same name is used in all cases.
  1. When variables refer to different values, different names are used even when the names are in different scopes.
  1. Variable names with wide scope are sufficiently distinctive to allow searching for them using grep.