Difference between revisions of "Code Formatting Conventions"

Jump to navigation Jump to search
m
Text replacement - "<source lang=" to "<syntaxhighlight lang="
m (Text replacement - "</source>" to "</syntaxhighlight>")
m (Text replacement - "<source lang=" to "<syntaxhighlight lang=")
Line 11: Line 11:
Braces in your code should look like the following example:
Braces in your code should look like the following example:


<source lang="cpp">
<syntaxhighlight lang="cpp">
for (int i = 0; i < t; i++) {
for (int i = 0; i < t; i++) {
     [...]
     [...]
Line 39: Line 39:
'''Conventional operators surrounded by a space character'''
'''Conventional operators surrounded by a space character'''


<source lang="cpp">
<syntaxhighlight lang="cpp">
a = (b + c) * d;
a = (b + c) * d;
</syntaxhighlight>
</syntaxhighlight>
Line 45: Line 45:
'''C++ reserved words separated from opening parentheses by a white space'''
'''C++ reserved words separated from opening parentheses by a white space'''


<source lang="cpp">
<syntaxhighlight lang="cpp">
while (true) {
while (true) {
</syntaxhighlight>
</syntaxhighlight>
Line 51: Line 51:
'''Commas followed by a white space'''
'''Commas followed by a white space'''


<source lang="cpp">
<syntaxhighlight lang="cpp">
someFunction(a, b, c);
someFunction(a, b, c);
</syntaxhighlight>
</syntaxhighlight>
<source lang="cpp">
<syntaxhighlight lang="cpp">
int d, e;
int d, e;
</syntaxhighlight>
</syntaxhighlight>
Line 60: Line 60:
'''Semicolons followed by a space character, if there is more on a line'''
'''Semicolons followed by a space character, if there is more on a line'''


<source lang="cpp">
<syntaxhighlight lang="cpp">
for (int a = 0; b < c; d++)
for (int a = 0; b < c; d++)
</syntaxhighlight>
</syntaxhighlight>
<source lang="cpp">
<syntaxhighlight lang="cpp">
doSomething(e); doSomething(f); // This is probably bad style anyway
doSomething(e); doSomething(f); // This is probably bad style anyway
</syntaxhighlight>
</syntaxhighlight>
Line 69: Line 69:
'''Mandatory ''{}'' for empty ''for''/''while'' loops'''
'''Mandatory ''{}'' for empty ''for''/''while'' loops'''


<source lang="cpp">
<syntaxhighlight lang="cpp">
while (i < length - 1 && array[++i] != item);  // bad
while (i < length - 1 && array[++i] != item);  // bad
while (i < length - 1 && array[++i] != item) {} // good
while (i < length - 1 && array[++i] != item) {} // good
Line 76: Line 76:
'''When declaring class inheritance and in a ? construct, colons should be surrounded by white space'''
'''When declaring class inheritance and in a ? construct, colons should be surrounded by white space'''


<source lang="cpp">
<syntaxhighlight lang="cpp">
class BusWheel : public RubberInflatable {
class BusWheel : public RubberInflatable {
</syntaxhighlight>
</syntaxhighlight>
<source lang="cpp">
<syntaxhighlight lang="cpp">
(isNight) ? colorMeDark() : colorMeBright();
(isNight) ? colorMeDark() : colorMeBright();
</syntaxhighlight>
</syntaxhighlight>
Line 85: Line 85:
'''Indentation level is not increased after namespace clause'''
'''Indentation level is not increased after namespace clause'''


<source lang="cpp">
<syntaxhighlight lang="cpp">
namespace Scumm {
namespace Scumm {


Line 98: Line 98:


'''Array delete operator has no whitespace before []'''
'''Array delete operator has no whitespace before []'''
<source lang="cpp">
<syntaxhighlight lang="cpp">
delete[] foo;
delete[] foo;
</syntaxhighlight>
</syntaxhighlight>
Line 105: Line 105:


No whitespace between template keyword and <
No whitespace between template keyword and <
<source lang="cpp">
<syntaxhighlight lang="cpp">
template<typename foo>
template<typename foo>
void myFunc(foo arg) {
void myFunc(foo arg) {
Line 115: Line 115:


Operator keyword is NOT separated from the name, except for type conversion operators where it is required.
Operator keyword is NOT separated from the name, except for type conversion operators where it is required.
<source lang="cpp">
<syntaxhighlight lang="cpp">
struct Foo {
struct Foo {
     void operator()() {
     void operator()() {
Line 130: Line 130:


No whitespace after a cast; and in a pointer, we write a whitespace before the star but not after it.
No whitespace after a cast; and in a pointer, we write a whitespace before the star but not after it.
<source lang="cpp">
<syntaxhighlight lang="cpp">
const char *ptr = (const char *)foobar;
const char *ptr = (const char *)foobar;
</syntaxhighlight>
</syntaxhighlight>
Line 137: Line 137:


We use the same rule for references as we do for pointers: use a whitespace before the "&" but not after it.
We use the same rule for references as we do for pointers: use a whitespace before the "&" but not after it.
<source lang="cpp">
<syntaxhighlight lang="cpp">
int i = 0;
int i = 0;
int &ref = i;
int &ref = i;
Line 145: Line 145:


When it adds to readability, a vertical alignment by means of extra tabs or spaces is allowed. However, it is not advised to have the opening and closing brackets/braces to occupy a single line.
When it adds to readability, a vertical alignment by means of extra tabs or spaces is allowed. However, it is not advised to have the opening and closing brackets/braces to occupy a single line.
<source lang="cpp">
<syntaxhighlight lang="cpp">
int foo    = 2;
int foo    = 2;
int morefoo = 3;
int morefoo = 3;
Line 157: Line 157:
== Switch/Case constructs ==
== Switch/Case constructs ==


<source lang="cpp">
<syntaxhighlight lang="cpp">
switch (cmd) {
switch (cmd) {
case kSomeCmd:
case kSomeCmd:
Line 193: Line 193:
Camel case starting with upper case.
Camel case starting with upper case.


<source lang="cpp">
<syntaxhighlight lang="cpp">
class MyClass { /* ... */ };
class MyClass { /* ... */ };
struct MyStruct { /* ... */ };
struct MyStruct { /* ... */ };
Line 203: Line 203:
Prefixed with '_' and in camel case (Yo! no underscore separators), starting with lowercase.
Prefixed with '_' and in camel case (Yo! no underscore separators), starting with lowercase.


<source lang="cpp">
<syntaxhighlight lang="cpp">
char *_someVariableName;
char *_someVariableName;
</syntaxhighlight>
</syntaxhighlight>
Line 211: Line 211:
Camel case, starting with lowercase.
Camel case, starting with lowercase.


<source lang="cpp">
<syntaxhighlight lang="cpp">
void thisIsMyFancyMethod();
void thisIsMyFancyMethod();
</syntaxhighlight>
</syntaxhighlight>
Line 219: Line 219:
Use camel case (Yo! no underscore separators), starting with lowercase.
Use camel case (Yo! no underscore separators), starting with lowercase.


<source lang="cpp">
<syntaxhighlight lang="cpp">
char *someVariableName;
char *someVariableName;
</syntaxhighlight>
</syntaxhighlight>
Line 229: Line 229:
In general you should avoid global variables, but if it can't be avoided, use 'g_' as prefix, camel case, and start with lowercase
In general you should avoid global variables, but if it can't be avoided, use 'g_' as prefix, camel case, and start with lowercase


<source lang="cpp">
<syntaxhighlight lang="cpp">
int g_someGlobalVariable;
int g_someGlobalVariable;
</syntaxhighlight>
</syntaxhighlight>
Line 250: Line 250:
There are many ways to mark such comments, but developers are encouraged to use the JavaDoc style:
There are many ways to mark such comments, but developers are encouraged to use the JavaDoc style:


<source lang="cpp">
<syntaxhighlight lang="cpp">
/**
/**
  * Move ("warp") the mouse cursor to the specified position in virtual
  * Move ("warp") the mouse cursor to the specified position in virtual
Line 264: Line 264:


If you want to add a brief explanation of a variable or function ''after'' its declaration, this is the correct syntax:
If you want to add a brief explanation of a variable or function ''after'' its declaration, this is the correct syntax:
<source lang="cpp">
<syntaxhighlight lang="cpp">
int16 x; ///< The horizontal part of the point
int16 x; ///< The horizontal part of the point
int16 y; ///< The vertical part of the point
int16 y; ///< The vertical part of the point
TrustedUser
2,147

edits

Navigation menu