Code Style
From Synergy Wiki
(Redirected from CodeStyle)
Tabs are used for indentation (not spaces)
\t printf("hello world"); // correct
\s\s\s\s printf("hello world"); // incorrect
Class, struct, and enum names are PascalCase and prefixed
class CMyClass { }; // class prefix is C
struct CMyStruct { }; // struct prefix is C
enum EMyEnum { }; // enum prefix is E
Member, and enum constant names are lower camelCase
class CMyClass {
public:
void helloWorld();
};
Member variables (public and private) begin with m_
class CMyClass {
public:
CMyClass* m_myClass1;
private:
CMyClass* m_myClass2;
};
Enum constants are prefixed with k
enum EMyEnum {
kValue1,
kValue2
};
Static variables are prefixed with s_
static CMyClass* s_myClass;
Comments and debug messages need not be grammatically correct
// i'm using bad grammar. but i like to use full stops DEBUG((CLOG_INFO "hello world"));
Multiline comments use single-line commenting (//)
// for long block comments, instead of using the slash asterisk // comments, we use the two-slash comments
Function return types must go on the line above the function name
// correct
int
CMyClass::helloWorld()
{
}
// incorrect
int CMyClass::helloWorld()
{
}
For functions, curly braces start on the next line after the function name
// correct
int
CMyClass::helloWorld()
{
// ...
}
// incorrect
int
CMyClass::helloWorld() {
// ...
}
For conditional statements, curly braces start on the same line
// correct
if (a == b) {
// ...
}
// correct
for (int i = 0; i < a; i++) {
// ...
}
// incorrect
if (a == b)
{
// ...
}
// incorrect
for (int i = 0; i < a; i++)
{
// ...
}
A space is used between operators and operands
// correct
a == b;
if (c == d) {
}
// incorrect
a=b;
if (c==d) {
}
No space is inserted after or before conditions within parenthesis
// correct
if (a == b) {
// ...
}
// incorrect
if ( a == b ) {
// ...
}
The void keyword is not used for functions that do not take parameters
// correct void helloWorld(); // incorrect void helloWorld(void);
The const keyword is used regularly
class CMyClass {
public:
const char* helloWorld();
};
CMyClass::CMyClass(const char* helloWorld)
{
}
Spaces must exist between conditional statement parenthesis and the keyword
// correct
if (a == b) {
}
// incorrect
if(a == b) {
}
No spaces between type and reference and pointer operator
// correct char* helloWorld; // incorrect char *helloWorld;
Pre-processor commands within #if are indented
// correct #if HELLO_WORLD # include "HellWorld.h" #endif // incorrect #if HELLO_WORLD #include "HellWorld.h" #endif
Pointers and references do not have prefixes
// correct CMyClass* myClass1 = new CMyClass(); CMyClass& myClass2 = *myClass1; // incorrect CMyClass* p_myClass1 = new CMyClass(); CMyClass& p_myClass2 = *p_myClass1; // incorrect CMyClass* pMyClass1 = new CMyClass(); CMyClass& pMyClass2 = *pMyClass1;
The left comparator operand is a variable
// correct
if (a == "hello world") {
}
// incorrect
if ("hello world" == a) {
}
The else statement always goes on a new line
// correct
if (a == b) {
// ...
}
else {
// ...
}
// incorrect
if (a == b) {
// ...
} else {
// ...
}