Documentation/CodingStyle

Coding conventions

Libraries

  • Source package names: lib<something>.
  • Binary packages: lib<something>N, lib<something>N-dbg, lib<something>-dev.
  • pkg-config files: lib<something>.pc
  • Headers: lib<something>.h, lib<something>-<foo>.h
  • Shared libraries/SONAMEs: lib<something>.so.X[.Y.Z]
  • Public functions: <something>_<name>. It is allowed to use shorter prefix if handy.

Code written for OpenInkpot

C

There is code written using different coding conventions. Please rendent it when you change it.

  • C99
  • No tabs, 4-space indentation
  • K&R:
    • Return type and modifiers of function are on separate line
    • Brackets around function are on separate lines
    • Open brackets are on the same line
    • else and while in do/while are on the same line as bracket
    • * gravitates towards variable, not type
    • Space between keyword and opening (
    • Function and macro call style: no space before the open paren, no spaces inside the parens, no spaces before commas, one space after each comma
    • vari_abl_es
    • MAC_ROS
    • Space around binary operators
    • No space after prefix operators
    • No space before postfix operators
  • Additionally:
    • No trailing whitespace
    • No parentheses around return value
    • bool, not int
    • typedef struct { } type_t;

Example:

#include <stdbool.h>

typedef struct {
    something *s;
} foobar_t;

typedef enum {
    AAA_B,
    AAA_C,
} enum_t;

static bool
foo(bool bar, char **baz);

int
main(int argc, char **argv)
{
    bool bar;
    char *baz;
    if (foo(bar, &baz)) {
        bar = argc + 1;
        bar++;
        --bar;
        baz = argv[argc - 1];
    } else {
        fu("Urgh?");
    }
}

Indent (for *.c):

indent -kr -nut -psl -ppi 2

Indent (for *.h):

indent -kr -nut -psl

Emacs settings (for .dir-locals.el):

TBD

VIM settings:

TBD

Shell

  • POSIX
  • No tabs
  • 4-space indentation
  • Local variables: lower_case
  • Global variables: UPPER_CASE
  • Do not use test, use [/]
  • Do not use backquotes, use $()

Example:

BAZ=1

foo() {
    bar=17
    BLAH=$(foooo "$bar" "$1")
    if [ "$BLAH" = 42 ]; then
        BLAH=24
    fi
}

foo "$BAZ"

Python

  • PEP-8

Other code

All patches for external projects are to use projects' code style.