Skip to content

Code Style

athairus edited this page Mar 24, 2016 · 8 revisions

About

This is a code style guide for the project. For the sake of consistency, try to follow it!

Code specs

  • LF line endings (Windows: Git can be configured to automatically change line endings)
  • utf8 encoding, no BOM
  • Aim for 120-width columns. You can just barely go over, but try not to most of the time.

astyle

To enforce a consistent code style, athairus has written a .astylerc with settings all C/C++ code written for the project will use. You can find it in the root directory of the Phoenix repo.

Setup

  1. Windows, Linux: Help -> About Plugins... OS X: Qt Creator-> About Plugins...
  2. Check "Beautifier" under C++
  3. Tools -> Options -> Beautifier
  4. Point Qt Creator to your copy of astyle
  5. Check the first two options under Options
  6. (Optional) Copy .astylerc to your home folder to make it usable in all Qt Creator projects (and possibly more).
  7. (Optional) Set up a keyboard shortcut to invoke astyle

Misc. code style

  • MOC macros like Q_UNUSED() should "stick" to the top of function definitions as they are not C++ code.
  • Newlines at the end of files.
  • Don't have multiple blank lines.

Examples:

int foo( int bar ) {
    Q_UNUSED( bar );

    doSomething();
    doSomething();
    doSomething();
}

if( foo ) {
    bar();
}

if( foobar ) {
    if( barfoo ) {
        fubar();
    } else {
        // Execute secret code
        *( int * )( 0 ) = 0xDEADBEEF;
    }

    barfoo();
}