Open main menu

Difference between revisions of "Code Formatting Conventions"

m
Text replacement - "</source>" to "</syntaxhighlight>"
(Update convention for empty loops as discussed on -devel)
m (Text replacement - "</source>" to "</syntaxhighlight>")
Line 27: Line 27:
     [...]
     [...]
};
};
</source>
</syntaxhighlight>


Did you see the {}'s on that?
Did you see the {}'s on that?
Line 41: Line 41:
<source lang="cpp">
<source lang="cpp">
a = (b + c) * d;
a = (b + c) * d;
</source>
</syntaxhighlight>


'''C++ reserved words separated from opening parentheses by a white space'''
'''C++ reserved words separated from opening parentheses by a white space'''
Line 47: Line 47:
<source lang="cpp">
<source lang="cpp">
while (true) {
while (true) {
</source>
</syntaxhighlight>


'''Commas followed by a white space'''
'''Commas followed by a white space'''
Line 53: Line 53:
<source lang="cpp">
<source lang="cpp">
someFunction(a, b, c);
someFunction(a, b, c);
</source>
</syntaxhighlight>
<source lang="cpp">
<source lang="cpp">
int d, e;
int d, e;
</source>
</syntaxhighlight>


'''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'''
Line 62: Line 62:
<source lang="cpp">
<source lang="cpp">
for (int a = 0; b < c; d++)
for (int a = 0; b < c; d++)
</source>
</syntaxhighlight>
<source lang="cpp">
<source lang="cpp">
doSomething(e); doSomething(f); // This is probably bad style anyway
doSomething(e); doSomething(f); // This is probably bad style anyway
</source>
</syntaxhighlight>


'''Mandatory ''{}'' for empty ''for''/''while'' loops'''
'''Mandatory ''{}'' for empty ''for''/''while'' loops'''
Line 72: Line 72:
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
</source>
</syntaxhighlight>


'''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'''
Line 78: Line 78:
<source lang="cpp">
<source lang="cpp">
class BusWheel : public RubberInflatable {
class BusWheel : public RubberInflatable {
</source>
</syntaxhighlight>
<source lang="cpp">
<source lang="cpp">
(isNight) ? colorMeDark() : colorMeBright();
(isNight) ? colorMeDark() : colorMeBright();
</source>
</syntaxhighlight>


'''Indentation level is not increased after namespace clause'''
'''Indentation level is not increased after namespace clause'''
Line 95: Line 95:


} // End of namespace Scumm
} // End of namespace Scumm
</source>
</syntaxhighlight>


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


'''Template definitions'''
'''Template definitions'''
Line 110: Line 110:
     // ...
     // ...
}
}
</source>
</syntaxhighlight>


'''Operator overloading'''
'''Operator overloading'''
Line 125: Line 125:
     }
     }
};
};
</source>
</syntaxhighlight>


'''Pointers and casts'''
'''Pointers and casts'''
Line 132: Line 132:
<source lang="cpp">
<source lang="cpp">
const char *ptr = (const char *)foobar;
const char *ptr = (const char *)foobar;
</source>
</syntaxhighlight>


'''References'''
'''References'''
Line 140: Line 140:
int i = 0;
int i = 0;
int &ref = i;
int &ref = i;
</source>
</syntaxhighlight>


'''Vertical alignment'''
'''Vertical alignment'''
Line 153: Line 153:
                                   x + w,
                                   x + w,
                                   y + h);
                                   y + h);
</source>
</syntaxhighlight>


== Switch/Case constructs ==
== Switch/Case constructs ==
Line 175: Line 175:
     Dialog::handleCommand(sender, cmd, data);
     Dialog::handleCommand(sender, cmd, data);
}
}
</source>
</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 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.


Line 197: Line 197:
struct MyStruct { /* ... */ };
struct MyStruct { /* ... */ };
typedef int MyInt;
typedef int MyInt;
</source>
</syntaxhighlight>


'''Class member variables'''
'''Class member variables'''
Line 205: Line 205:
<source lang="cpp">
<source lang="cpp">
char *_someVariableName;
char *_someVariableName;
</source>
</syntaxhighlight>


'''Class methods'''
'''Class methods'''
Line 213: Line 213:
<source lang="cpp">
<source lang="cpp">
void thisIsMyFancyMethod();
void thisIsMyFancyMethod();
</source>
</syntaxhighlight>


'''Local variables'''
'''Local variables'''
Line 221: Line 221:
<source lang="cpp">
<source lang="cpp">
char *someVariableName;
char *someVariableName;
</source>
</syntaxhighlight>


Note that for POD structures it is fine to use this rule too.
Note that for POD structures it is fine to use this rule too.
Line 231: Line 231:
<source lang="cpp">
<source lang="cpp">
int g_someGlobalVariable;
int g_someGlobalVariable;
</source>
</syntaxhighlight>


== Special comments ==
== Special comments ==
Line 258: Line 258:
  */
  */
virtual void warpMouse(int x, int y) = 0;
virtual void warpMouse(int x, int y) = 0;
</source>
</syntaxhighlight>
(See [http://doxygen.scummvm.org/d9/df4/classOSystem.html#ecab84670def917107d6c1b5ca3b82c3 here] for the docs generated from this.)
(See [http://doxygen.scummvm.org/d9/df4/classOSystem.html#ecab84670def917107d6c1b5ca3b82c3 here] for the docs generated from this.)


Line 267: Line 267:
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
</source>
</syntaxhighlight>
(See [http://doxygen.scummvm.org/d7/d66/structCommon_1_1Point.html#2d868735aeaaf391ce9b3df9232c031f here] for the docs generated from this.)
(See [http://doxygen.scummvm.org/d7/d66/structCommon_1_1Point.html#2d868735aeaaf391ce9b3df9232c031f here] for the docs generated from this.)


TrustedUser
2,147

edits