C Style Guide

From Mugshot Developer Wiki

Jump to: navigation, search

This page covers both C and C++ for the Windows Client and Linux Client.

In tend to use Java-similar C/C++ code formatting, because otherwise we get really confused as we go back and forth between languages.

The Windows frontend to the client is in C++ (well, with Javascript... don't ask). The cross-platform backend library (in the "common" directory) is in C, as is the Linux frontend.

Contents

In common between C and C++

  • 4 space indents with { on the previous line. Note however, that the Visual Studio editor really doesn't like
if (foo &&
    bar) {
   doSomething();
}

(you'll find it almost impossible to indent things that way.) So write:

if (foo &&
    bar) 
{
   doSomething();
}

Putting the { on the next line is also permissible any time that it makes things clearer.

We also put { on the next line for function bodies and out-of-line method bodies, but not for inline method bodies.

Specific to C++

  • javaCaps rather than StudlyCaps for method names and local variables. A nice consequence of this is that it distinguishes member functions from functions exported from one system DLL or another. (Windows doesn't really namespace anything, so unless you know that InsertMenuItem() is a system function, it would otherwise be easily mistakable for a member function.)

There are a few places where we deviate from our Java pratice.

  • Member variables get a trailing _ e.g. webBrowser_. There are various justifications for this deviation: one would be that the .cpp/.h separation makes it harder to see what the member variables are.

Other C++ notes

  • We aren't currently using namespaces; instead we are namingspacing in the old-fashioned way by simply using a prefix HippoRegistrar, HippoPtr, etc. There's no special reason for this really, except it's not worth changing at this point.
  • Templates and other modern C++ features are fine to use, however the general flavour of the code should be pretty straightforward. Don't make us dig out the Stroustrup.
  • We aren't using exceptions since they mix pretty badly with COM programming; you have to be very careful not to throw an exception through a marshalled COM interface.
  • There are some custom utilities (e.g. HippoArray) from when we weren't using the STL for a couple obscure reasons. Many usages of these have been removed but some remain, feel free to clean them up, and don't use the old stuff in new code.
  • The common library uses the GLib and GObject libraries, so the Windows port does as well. To make GObject signals typesafe there are some simple wrappers.

C Style

The C style is essentially the "GLib/GTK+" style, which is in turn based on the GNU style, with a couple changes to be friendlier to Java programmers:

  • 4 space indents
  • no space before parentheses after function/macro names
  • put the brace after if/for on the same line, "if (blah) {"
Personal tools