Hacking:Code style conventions: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1: | Line 1: | ||
== |
== General == |
||
=== General === |
|||
Max length of source code line equal 120 characters. Put "//---" cars in front of function definitions with maximum length (120) : |
Max length of source code line equal 120 characters. Put "//---" cars in front of function definitions with maximum length (120) : |
||
Line 9: | Line 7: | ||
</source> |
</source> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Use spaces, not tabs. 4 spaces instead one real tab. Tabs should only appear in files that require them for semantic meaning, like Makefiles. |
Use spaces, not tabs. 4 spaces instead one real tab. Tabs should only appear in files that require them for semantic meaning, like Makefiles. |
||
Line 25: | Line 30: | ||
</source> |
</source> |
||
== Spacing == |
|||
Do not place spaces around unary operators. |
Do not place spaces around unary operators. |
||
Line 61: | Line 66: | ||
</source> |
</source> |
||
== Line breaking == |
|||
Each statement should get its own line. |
Each statement should get its own line. |
||
<source lang="cpp"> |
<source lang="cpp"> |
||
Line 72: | Line 77: | ||
</source> |
</source> |
||
== Braces == |
|||
Use typically trailing braces everywhere (if, else, functions, structures, typedefs, class definitions, etc.) |
Use typically trailing braces everywhere (if, else, functions, structures, typedefs, class definitions, etc.) |
||
Line 94: | Line 99: | ||
</source> |
</source> |
||
== Null and false == |
|||
In C++, the null pointer value should be written as <code>nullptr</code>. In C, it should be written as <code>NULL</code>. |
In C++, the null pointer value should be written as <code>nullptr</code>. In C, it should be written as <code>NULL</code>. |
||
⚫ | |||
⚫ | |||
<source lang="cpp"> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
</source> |
|||
⚫ | |||
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. |
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"> |
<source lang="cpp"> |
||
Line 113: | Line 125: | ||
</source> |
</source> |
||
== Names == |
|||
Function names start with an upper case: |
Function names start with an upper case: |
||
<source lang="cpp"> |
<source lang="cpp"> |
||
Line 206: | Line 218: | ||
</source> |
</source> |
||
== Other Punctuation == |
|||
Constructors for C++ classes should initialize all of their members using C++ initializer syntax. Each member (and superclass) should be indented on a separate line, with the colon or comma preceding the member on that line. |
|||
⚫ | |||
<source lang="cpp"> |
<source lang="cpp"> |
||
MyClass::MyClass(Document* document) |
|||
⚫ | |||
: MySuperClass() |
|||
⚫ | |||
, m_myMember(0) |
|||
⚫ | |||
, m_document(document) |
|||
⚫ | |||
{ |
|||
} |
|||
MyOtherClass::MyOtherClass() |
|||
Put spaces between function arguments in definition or call : |
|||
: MySuperClass() |
|||
⚫ | |||
int function(int a, int b) |
|||
{ |
{ |
||
called_function(a, b); |
|||
} |
} |
||
</source> |
</source> |
||
Prefer index over iterator in Vector iterations for terse, easier-to-read code. |
|||
⚫ | |||
Right: |
|||
<source lang="cpp"> |
<source lang="cpp"> |
||
for (auto& frameView : frameViews) |
|||
⚫ | |||
frameView->updateLayoutAndStyleIfNeededRecursive(); |
|||
</source> |
|||
OK: |
|||
<source lang="cpp"> |
|||
unsigned frameViewsCount = frameViews.size(); |
|||
for (unsigned i = 0; i < frameViewsCount; ++i) |
|||
frameViews[i]->updateLayoutAndStyleIfNeededRecursive(); |
|||
</source> |
</source> |
||
⚫ | |||
⚫ | |||
⚫ | |||
Class names start with "V" and each successive word starts with an upper case. |
Class names start with "V" and each successive word starts with an upper case. |
||
<source lang="cpp"> |
<source lang="cpp"> |
||
Line 318: | Line 336: | ||
Note. Don't make virtual methods inline. See Clang warning -Wweak-vtables. [https://stackoverflow.com/questions/23746941/meaning-of-wweak-vtables Discusion on StackOverflow]. |
Note. Don't make virtual methods inline. See Clang warning -Wweak-vtables. [https://stackoverflow.com/questions/23746941/meaning-of-wweak-vtables Discusion on StackOverflow]. |
||
== File names == |
|||
Each class should be in a separate source file unless it makes sense to group several smaller classes. The file name should be the same as the name of the class with the "V" prefix. |
Each class should be in a separate source file unless it makes sense to group several smaller classes. The file name should be the same as the name of the class with the "V" prefix. |
||
<source lang="cpp"> |
<source lang="cpp"> |