Difference between revisions of "Code Formatting Conventions"

From ScummVM :: Wiki
Jump to navigation Jump to search
m (update formatting)
(major formatting improvements)
Line 1: Line 1:
== 1. Use common sense ==
== 1. Use common sense ==


These are conventions which we try to follow when writing code for ScummVM. They are this way mainly for reasons of taste, however, sticking to a common set of formatting rules also makes it slightly easier to read through our sources. If you want to submit patches, please try to follow these rules.  
These are conventions which we try to follow when writing code for ScummVM. They are this way mainly for reasons of taste, however, sticking to a common set of formatting rules also makes it slightly easier to read through our sources. If you want to submit patches, please try to follow these rules.  


As such we don't follow these rules slavishly, in certain cases it is OK (and in fact favorable) to stray from them.  
As such we don't follow these rules slavishly, in certain cases it is OK (and in fact favorable) to stray from them.


== 2. Hugging braces ==
== 2. Hugging braces ==
Line 10: Line 9:
Braces in your code should look like the following example:
Braces in your code should look like the following example:


<pre>
if (int i = 0; i < t; i++) {
if (int i = 0; i < t; i++) {
[...]
[...]
Line 15: Line 15:
[...]
[...]
}
}
 
class Dummy() {
class Dummy() {
[...]
[...]
}
}
</pre>
 
Did you see the {}'s on that?
Did you see the {}'s on that?


== 3. Tab indents, with tabstop at four spaces ==
== 3. Tab indents, with tabstop at four spaces ==


Says it all, really.  
Says it all, really.


== 4. Whitespaces ==
== 4. Whitespaces ==


Conventional operators surrounded by a space character
Conventional operators surrounded by a space character
<pre>
a = (b + c) * d;
a = (b + c) * d;
</pre>
 
C++ reserved words separated from opening parentheses by a white space
C++ reserved words separated from opening parentheses by a white space
<pre>
while (true) {
while (true) {
</pre>
 
Commas followed by a white space
Commas followed by a white space
<pre>
someFunction(a, b, c);
someFunction(a, b, c);
int d, e;
int d, e;
</pre>
 
Semicolons followed by a space character, if there is more on line
Semicolons followed by a space character, if there is more on line
<pre>
for (int a = 0; b++; c < d)
for (int a = 0; b++; c < d)
doSomething(e); doSomething(f); // This is probably bad style anyway
doSomething(e); doSomething(f); // This is probably bad style anyway
</pre>
 
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
<pre>
class BusWheel : public RubberInflatable {
class BusWheel : public RubberInflatable {
(isNight) ? colorMeDark() : colorMeBright();
(isNight) ? colorMeDark() : colorMeBright();
</pre>
 
Indentation level is not increased after namespace clause
Indentation level is not increased after namespace clause
<pre>
namespace Scumm {
namespace Scumm {


Line 56: Line 74:


} // End of namespace Scumm
} // End of namespace Scumm
</pre>


== 5. Switch / Case constructs ==
== 5. Switch / Case constructs ==


<pre>
switch (cmd) {
switch (cmd) {
case kSaveCmd:
case kSaveCmd:
Line 70: Line 90:
Dialog::handleCommand(sender, cmd, data);
Dialog::handleCommand(sender, cmd, data);
}
}
</pre>


== 6. Naming ==
== 6. Naming ==
Line 75: Line 96:
Constants
Constants


Basically, you have two choices:  
Basically, you have two choices:
 
<pre>
kSomeKludgyConstantName // notice k prefix
kSomeKludgyConstantName // notice k prefix
</pre>
or  
 
or
 
<pre>
SOME_KLUDGY_CONSTANT_NAME
SOME_KLUDGY_CONSTANT_NAME
</pre>
 
Classes
Classes


Mixed case starting with upper case  
Mixed case starting with upper case
 
<pre>
class MeClass() {
class MeClass() {
</pre>
 
Class members
Class members


_ prefixed and in mixed case (Yo! no underscore separators), starting with lowercase.  
_ prefixed and in mixed case (Yo! no underscore separators), starting with lowercase.
 
<pre>
char *_someVariableName;
char *_someVariableName;
</pre>
 
Class methods
Class methods


mixed case, starting with lowercase.  
mixed case, starting with lowercase.
 
<pre>
void thisIsMyFancyMethod();
void thisIsMyFancyMethod();
</pre>

Revision as of 16:40, 13 February 2006

1. Use common sense

These are conventions which we try to follow when writing code for ScummVM. They are this way mainly for reasons of taste, however, sticking to a common set of formatting rules also makes it slightly easier to read through our sources. If you want to submit patches, please try to follow these rules.

As such we don't follow these rules slavishly, in certain cases it is OK (and in fact favorable) to stray from them.

2. Hugging braces

Braces in your code should look like the following example:

		if (int i = 0; i < t; i++) {
			[...]
		} else {
			[...]
		}

		class Dummy() {
			[...]
		}

Did you see the {}'s on that?

3. Tab indents, with tabstop at four spaces

Says it all, really.

4. Whitespaces

Conventional operators surrounded by a space character

	a = (b + c) * d;

C++ reserved words separated from opening parentheses by a white space

	while (true) {

Commas followed by a white space

	someFunction(a, b, c);
	int d, e;

Semicolons followed by a space character, if there is more on line

	for (int a = 0; b++; c < d)
	doSomething(e); doSomething(f);	// This is probably bad style anyway

When declaring class inheritance and in a ? construct, colons should be surrounded by white space

	class BusWheel : public RubberInflatable {
	(isNight) ? colorMeDark() : colorMeBright();

Indentation level is not increased after namespace clause

	namespace Scumm {

	byte Actor::kInvalidBox = 0;

	void Actor::initActorClass(ScummEngine *scumm) {
		_vm = scumm;
	}

	} // End of namespace Scumm

5. Switch / Case constructs

	switch (cmd) {
	case kSaveCmd:
		save();
		break;
	case kLoadCmd:
	case kPlayCmd:
		close();
		break;
	default:
		Dialog::handleCommand(sender, cmd, data);
	}

6. Naming

Constants

Basically, you have two choices:

	kSomeKludgyConstantName		// notice k prefix

or

	SOME_KLUDGY_CONSTANT_NAME

Classes

Mixed case starting with upper case

	class MeClass() {

Class members

_ prefixed and in mixed case (Yo! no underscore separators), starting with lowercase.

	char *_someVariableName;

Class methods

mixed case, starting with lowercase.

	void thisIsMyFancyMethod();