openFrameworks Coding style guidelines

Christoph Buchner edited this page Jan 14, 2015 · 22 revisions

This page details the code style guidelines for openFrameworks. A common code style gives a homogeneous look to our source files, and makes reading/understanding/developing code for OF easier. All OF contributors should abide by this code style.

OF code style

The page oF-code-style contains the OF code style specification. It's based on the Qt code style, but has been modified for openFrameworks' needs.

How to follow the guidelines

There are several automatic tools available in the main IDEs, which can make it easier for developers to keep their code in compliance. However, keep in mind that automatic tools only go so far, and cannot check all the requirements.

A detailed policy on how and when code compliance checks should happen will be outlined here.

Tools

Feedback regarding the performance/suitability of these tools is welcome!

Uncrustify

Uncrustify is a highly configurable, easily modifiable source code beautifier. It offers more options than AStyle, and is the basic tool for OF code formatting by means of a script users can run. A configuration file, formatting scripts, and associated information to use uncrustify can be found in the scripts/dev/style folder.

XCode

You could maybe use Uncrustify Automator Services to leverage the existing uncrustify config in XCode4. This is untested, though, please report back if try it out! There's also rudimentary AStyle support: http://eatmyrandom.blogspot.com/2011/03/xcode-astyle-part-2-for-xcode-4x.html

Eclipse

Eclipse has an automatic source formatter which can be configured in detail. An importable settings file detailing the OF style can be found here. It can fulfill most of the requirements of the code style specification, except for preprocessor indentation. This config file makes sure that any new code you write already gets formatted correctly, and additionally you can select code portions and only format those by clicking Source->Format.

Code::Blocks

There's an AStylePlugin for code::blocks. It's also possible to use Uncrustify within codeblocks, a user has put together a helpful guide here.

Visual Studio

If you have a supported version of Visual Studio, there is an AStyle Extension.

AStyle

AStyle is an automatic tool for code formatting. It supports a wide variety of style ootb, but its controls are not very granular.

Other code styles

There are a few sources which served as a basis for how the oF code style looks like:

  • A Pirate Pad document made during ofdev-con in early 2011.
  • The Qt Coding Style is a comprehensive document detailing the coding style for Qt. Barring objections, this will be the base template which will be modified to suit the needs of the oF community.
  • The K&R Java style, but this seems to cover mostly indentation, so it's not really complete.