Open main menu

Difference between revisions of "Code Formatting Conventions"

→‎Switch/Case constructs: enhanced to avoid ambiguity
m (Text replacement - "<source lang=" to "<syntaxhighlight lang=")
(→‎Switch/Case constructs: enhanced to avoid ambiguity)
 
(6 intermediate revisions by the same user not shown)
Line 140: Line 140:
int i = 0;
int i = 0;
int &ref = i;
int &ref = i;
</syntaxhighlight>
'''Arrow operator'''
We put no spaces around the arrow operator.
<syntaxhighlight lang="cpp">
int a = foo->bar;
</syntaxhighlight>
</syntaxhighlight>


Line 160: Line 167:
switch (cmd) {
switch (cmd) {
case kSomeCmd:
case kSomeCmd:
     someCmd();
     a = 1;
    func1();
    b = a + 1;
     // fall through
     // fall through
case kSomeVerySimilarCmd:
case kSomeVerySimilarCmd:
     someMoreCmd();
     someMoreCmd();
    a = 3;
     break;
     break;
case kSaveCmd:
case kSaveCmd:
Line 176: Line 186:
}
}
</syntaxhighlight>
</syntaxhighlight>
* Note comment on whether fall through is intentional. Use exactly this and not some variation both for consistency and so that the compiler will see it and suppress potential warnings.
* Note the comment on whether fall through is intentional. Use exactly this and not some variation both for consistency and so that the compiler will see it and suppress potential warnings.
 
== Vertical space ==
 
'''Avoid composite one liners'''
 
<syntaxhighlight lang="cpp">
if (condition) do_foo();  // <-- bad example
 
// Proper way of writing it
if (condition)
    do_foo();
</syntaxhighlight>
 
 
'''Split out variable declarations'''
<syntaxhighlight lang="cpp">
int a = 1;
int b = 2;
int c;
 
c = a + b;
</syntaxhighlight>
 
'''Split out logical program blocks'''
<syntaxhighlight lang="cpp">
void MacWindowManager::setScreen(ManagedSurface *screen) {
    _screen = screen;
    delete _screenCopy;
    _screenCopy = nullptr;
 
    if (_desktop)
        _desktop->free();
    else
        _desktop = new ManagedSurface();
 
    _desktop->create(_screen->w, _screen->h, _pixelformat);
    drawDesktop();
}
</syntaxhighlight>
 
== Preprocessor pragmas ==
All preprocessor pragrmas must start from the first column:
 
<syntaxhighlight lang="cpp">
#include "common/system.h"
 
#pragma mark --- Start of example ---
void example() {
    int i = 0;
 
#ifdef USE_FEATURE
    Feature f = new Feature();
#endif
 
    for (int x = 0; x < 10; x++) {
 
#define FOO f
        warning("%d", (FOO).code());
#undef FOO
    }
}
</syntaxhighlight>


== Naming ==
== Naming ==
Line 191: Line 263:
'''Type names'''
'''Type names'''


Camel case starting with upper case.
Camel case starting with upper case. This includes template types.


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
Line 197: Line 269:
struct MyStruct { /* ... */ };
struct MyStruct { /* ... */ };
typedef int MyInt;
typedef int MyInt;
template typename<FancyPants>
void bestClothes(FancyPants pants) {/* ... */ }
</syntaxhighlight>
</syntaxhighlight>


Line 213: Line 288:
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
void thisIsMyFancyMethod();
void thisIsMyFancyMethod();
</syntaxhighlight>
'''Non-member functions'''
Camel case, starting with lowercase.
<syntaxhighlight lang="cpp">
void thisIsMyFancyGlobalFunction();
</syntaxhighlight>
</syntaxhighlight>


Line 241: Line 324:
* '''TODO''' marks incomplete code, or things that could be done better but are left for the future.
* '''TODO''' marks incomplete code, or things that could be done better but are left for the future.
* '''WORKAROUND''' marks code that works around bugs in the original game, like script bugs. Sometimes these bugs worked in the original due to bugs in the original engine, sometimes the bug was visible in the original, too. It's important that you explain here what exactly you work around, and if applicable, refer to relevant tracker items!
* '''WORKAROUND''' marks code that works around bugs in the original game, like script bugs. Sometimes these bugs worked in the original due to bugs in the original engine, sometimes the bug was visible in the original, too. It's important that you explain here what exactly you work around, and if applicable, refer to relevant tracker items!
* '''I18N:''' Adds comment to translators for internationalization. See [[Supporting GUI Translation]].


=== Doxygen documentation comments ===
=== Doxygen documentation comments ===