Software/PulseAudio/Documentation/Developer/CodingStyle

Logo

HomeAboutCommunityDownloadDocumentationPlanet


Coding Style

Please follow the following rough rules when submitting code for inclusion in PA or committing code into Git. Although this list might appear massive I am still quite liberal in accepting patches that do not follow these rules. OTOH I am not your reindenting monkey, so please follow them, especially if your submissions are non-trivial!

  1. No tabs please! Please indent exclusively with spaces.
  2. Indentation is 4 characters.
  3. Please keep the opening bracket of a block on the same line as the expression opening it. i.e. This is correct:

void good_function(void) {

    if (a) {
        printf("Hello World!\n");
        a = 0;
    }
}

void bad_function(void) 
{
    if (a) 
    {
        printf("Hello World!\n");
        a = 0;
    }
}
  1. Avoid unnecessary curly braces. Good code:

if (!braces_needed)
    printf("This is compact and neat.\n");

if (!braces_needed) {
    printf("This is superfluous and noisy.\n");
}
  1. Don't put the return type of a function on a separate line. This is good:

int good_function(void) {
}

int
bad_function(void) {
}
  1. On function calls and definitions, don't put an extra space between the function name and the opening parenthesis of the argument list. This good:

double sin(double x);

double sin (double x);
  1. Exported function names should be prefixed with pa_. Static definitions should not be prefixed. Exported structs should be typedef'ed so they can be used without typing "struct" each time. Structs that are used only inside a single source file should not be typedef'ed that way.

  2. Keep private functions private! Make them static!
  3. Data types that are usually passed as call-by-value arguments should be suffixed with _t with a typedef. I.e. all enums should be suffixed like this:

typedef enum pa_resample_method {
  /* ... */
} pa_resample_method_t;
  1. No C++ comments please! i.e. this is good:

/* This is a good comment */

// This is a bad comment

void good_code(int a, int b) {
    pa_assert(a > 47);
    pa_assert(b != 0);

    /* ... */
}

void bad_code(int a, int b) {
    pa_assert(a > 47 && b != 0);

}
  1. Errors are returned from functions as negative values. Success is returned as 0 or positive value.
  2. Check for error codes on every system call, and every external library call. If you you are sure that calls like that cannot return an error, make that clear by wrapping it in pa_assert_se(). i.e.:

pa_assert_se(close(fd) == 0);

Yes, not all code that is part of PA right now follows these rules closely. This is mostly due to the fact that the formalization of these rules is newer then most of the code itself.

Indentation in Emacs

If you use Emacs, consider using this Elisp function for proper indentation:

(defun my-c-mode()
  "Lennart Poettering's C indentation"
  (message "Setting Lennart Poettering's C indentation...")
  (c-set-offset 'substatement-open 0)
  (c-set-offset 'statement-case-open 0)
  (c-set-offset 'case-label '+)
  (c-set-offset 'arglist-intro '++)
  (c-set-offset 'arglist-close 0)
  (define-key c-mode-base-map "\C-m" 'c-context-line-break)
  (setq tab-width 8)
  (setq c-basic-offset 4)
  (c-toggle-hungry-state 1)
  (setq indent-tabs-mode nil))