Style

This coding style is (or should be) used for all the core Telepathy components, notably telepathy-glib, libtelepathy, telepathy-gabble and telepathy-stream-engine.

These are guidelines only; there are always exceptional circumstances. Consistency and readability are usually the most important considerations.

C code using GLib/GObject

Filenames

#includes

If you're going to #include "config.h", do that first, in case it defines things like "inline".

Next, #include the header in which this .c file's API is declared. This guarantees that all public headers are self-contained.

Next, #define any libc feature-test macros you need (_GNU_SOURCE etc.) and #include any C/POSIX standard library headers you need, in alphabetical order.

Next, #include any headers you need from non-standard libraries (GLib, Gtk, telepathy-glib, Avahi, ...) in alphabetical order.

Finally, #include any private (non-installed) headers from the library or program you're writing.

For example:

/* Example 1: foo.c
 * ... insert copyright etc. here ... 
 */
#include "config.h"

#include "foo.h"

#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L /* for posix_memalign */
#endif
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include <avahi-common/address.h>
#include <glib-object.h>
#include <telepathy-glib/bar.h>

#define DEBUG_FLAG DEBUG_FOO
#include "bar.h"
#include "debug.h"
#include "foobar.h"

(Try to avoid using non-standard functions that need feature test macros - I've just used posix_memalign here for the sake of an example.)

Layout

/* Example 2 */
while (foo)
  {
    if (badger != NULL)
      bar ();

    if (mushroom != NULL)
      {
        do_stuff ();
      }
    else
      {
        do_more_stuff ();
        do_yet_more_stuff ();
      }
  }

/* Example 3 */
static void badger_mushroom (Mushroom *self,
    const gchar *reason,
    gint another_param,
    ...,
    gpointer omg_so_many_params);

/* Example 4 */
static void
badger_mushroom (Mushroom *self,
    const gchar *reason,
    gint another_param,
    ...,
    gpointer omg_so_many_params)
{
  ...
}

(Rationale: that way you can grep for ^function_name and you'll only find the definition, without any clutter caused by declarations).

Naming

Build system

List libs and cflags in stack order, with lowest in the stack first (so one can link against highest in the stack somewhere else without picking up everything from the somewhere else). (This guideline was borrowed from gstreamer)

# Example 6
AM_CFLAGS = $(DBUS_CFLAGS) $(GLIB_CFLAGS) $(DBUS_GLIB_CFLAGS) $(TP_GLIB_CFLAGS)
something_LDADD = $(DBUS_LIBS) $(GLIB_LIBS) $(DBUS_GLIB_LIBS) $(TP_GLIB_LIBS)

Misc

Fungus *fungus;
Mushroom *mushroom;

mushroom = MUSHROOM (fungus);
/* or if there's no suitable asserting macro or if the cast is from
 * a subclass to a superclass, */
mushroom = (Mushroom *) fungus;

Python

See PEP 8.

Indentation should always be 4 spaces.

C++ code using Qt

telepathy-qt4 is meant to be in the kdelibs coding style.

The same build system, #include and filename guidelines as for C/GLib/GObject (above) apply to C++.

Emacs C Mode

Adding the following to your .emacsrc creates a Telepathy style for you:

(defun telepathy-c-initialization-hook ()
  (c-add-style "telepathy" 
  '("gnu"
    (indent-tabs-mode . nil)
    (c-offsets-alist
     (arglist-intro . 4)
     (arglist-cont-nonempty . 4)))))

(add-hook 'c-initialization-hook 'telepathy-c-initialization-hook)

You can also set telepathy style as default:

(setq c-default-style "telepathy")

Vim Configuration

Adding the following to your .vimrc should help with adherence to these guidelines:

set cino=>2s,{1s,n-s,^-s
autocmd FileType python setl tw=78 ts=4 sts=4 sw=4 et
autocmd FileType c      setl tw=78 ts=2 sts=2 sw=2 et