Jump to content

Hacking:Code style conventions: Difference between revisions

(Created page Code style conventions.)
 
Line 2:
 
=== General ===
 
Use space instead real tabs that equal 4 spaces.
 
Max length of source code line equal 120 characters. Put "//---" cars in front of function definitions with maximum length (120) :
Line 10 ⟶ 8:
//---------------------------------------------------------------------------------------------------------------------
</source>
 
==== Indentation ====
Use spaces, not tabs. 4 spaces instead one real tab. Tabs should only appear in files that require them for semantic meaning, like Makefiles.
 
A case label should be indented with its switch statement. The case statement is indented too.
<source lang="cpp">
switch (condition)
{
case fooCondition:
case barCondition:
i++;
break;
default:
i--;
}
</source>
 
==== Spacing ====
Do not place spaces around unary operators.
 
<code>i++;</code>
 
Do place spaces around binary and ternary operators.
<source lang="cpp">
y = m * x + b;
f(a, b);
c = a | b;
return condition ? 1 : 0;
</source>
 
Do not place spaces before comma and semicolon.
<source lang="cpp">
for (int i = 0; i < 10; ++i)
{
doSomething();
}
 
f(a, b);
</source>
 
Place spaces between control statements and their parentheses.
<source lang="cpp">
if (condition)
{
doIt();
}
</source>
 
Do not place spaces between a function and its parentheses, or between a parenthesis and its content.
<source lang="cpp">
f(a, b);
</source>
 
==== Line breaking ====
Each statement should get its own line.
<source lang="cpp">
x++;
y++;
if (condition)
{
doIt();
}
</source>
 
==== Braces ====
 
Use typically trailing braces everywhere (if, else, functions, structures, typedefs, class definitions, etc.)
Line 31 ⟶ 94:
</source>
 
==== Null ====
In C++, the null pointer value should be written as <code>nullptr</code>. In C, it should be written as <code>NULL</code>.
 
==== Floating point literals ====
Unless required in order to force floating point math, do not append <code>.0</code>, <code>.f</code> and <code>.0f</code> to floating point literals.
<source lang="cpp">
const double duration = 60;
 
void setDiameter(float diameter)
{
radius = diameter / 2;
}
 
setDiameter(10);
 
const int framesPerSecond = 12;
double frameDuration = 1.0 / framesPerSecond;
</source>
 
==== Names ====
Function names start with an upper case:
<source lang="cpp">
Line 36 ⟶ 119:
</source>
 
In multi-word function names each word starts with an upper case, including all letters in an acronym:
<source lang="cpp">
void ThisFunctionDoesSomething(void);
void HTMLDocument;
</source>
 
Line 59 ⟶ 143:
<source lang="cpp">
#define SIDE_FRONT 0
</source>
 
Use full words, except in the rare case where an abbreviation would be more canonical and easier to understand.
<source lang="cpp">
size_t characterSize;
size_t length;
short tabIndex; // more canonical
</source>
 
Data members in C++ classes should be private. Static data members should be prefixed by “s_”. Other data members should be prefixed by “m_”.
<source lang="cpp">
class String
{
public:
...
 
private:
short m_length;
};
</source>
 
Line 75 ⟶ 178:
int const *p;
</source>
 
Precede boolean values with words like “is” and “did”.
<source lang="cpp">
bool isValid;
bool didSendData;
</source>
 
Precede getters that return values through out arguments with the word “get”.
<source lang="cpp">
void getInlineBoxAndOffset(InlineBox*&, int& caretOffset) const;
</source>
 
Use descriptive verbs in function names.
<source lang="cpp">
bool convertToASCII(short*, size_t);
</source>
 
Prefer <code>const</code> to <code>#define</code>. Prefer inline functions to macros.
 
Macros that expand to function calls or other non-constant computation: these should be named like functions, and should have parentheses at the end, even if they take no arguments (with the exception of some special macros like ASSERT). Note that usually it is preferable to use an inline function in such cases instead of a macro.
 
<code>#define</code>, <code>#ifdef</code> “header guards” should be named exactly the same as the file (including case), replacing the <code>.</code> with a <code>_</code>.
<source lang="cpp">
// mainwindowsnogui.h
#ifndef MAINWINDOWSNOGUI_H
#define MAINWINDOWSNOGUI_H
</source>
 
=== Other Punctuation ===
 
Negation operator should not be used in its short form :
Cookies help us deliver our services. By using our services, you agree to our use of cookies.