Open main menu

Difference between revisions of "Code Formatting Conventions"

→‎Switch/Case constructs: enhanced to avoid ambiguity
m (→‎Whitespaces: grammar)
(→‎Switch/Case constructs: enhanced to avoid ambiguity)
 
(4 intermediate revisions by the same user not shown)
Line 167: 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 183: 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 198: 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 204: Line 269:
struct MyStruct { /* ... */ };
struct MyStruct { /* ... */ };
typedef int MyInt;
typedef int MyInt;
template typename<FancyPants>
void bestClothes(FancyPants pants) {/* ... */ }
</syntaxhighlight>
</syntaxhighlight>


Line 220: 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 248: 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 ===