Hacking:Code style conventions: Difference between revisions

Content added Content deleted
No edit summary
Line 1: Line 1:
== Code style conventions ==
== 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>


Don't use
==== Indentation ====
<source lang="cpp">
using namespace MyNamespace
</source>

C++ namespaces and the using namespace <code>MyNamespace</code> statement can confuse '''lupdate'''. It will interpret <code>VClass::tr</code> like <code>MyNamespace::VClass::tr</code>. Runtime translation of these strings will fail because of that.

== 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.
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 ====
== Spacing ==
Do not place spaces around unary operators.
Do not place spaces around unary operators.


Line 61: Line 66:
</source>
</source>


==== Line breaking ====
== 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 ====
== 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 ====
== 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>.


Negation operator should not be used in its short form :
==== Floating point literals ====
<source lang="cpp">
if (!cond) // error-prone
if (cond == false) // better
if (not cond) // better (alternative keyword, recommended way)
</source>

== 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.
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 ====
== 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 ===
== 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.


Negation operator should not be used in its short form :
<source lang="cpp">
<source lang="cpp">
MyClass::MyClass(Document* document)
if (!cond) // error-prone
: MySuperClass()
if (cond == false) // better
, m_myMember(0)
if (not cond) // better (alternative keyword, recommended way)
, m_document(document)
</source>
{
}


MyOtherClass::MyOtherClass()
Put spaces between function arguments in definition or call :
: MySuperClass()
<source lang="cpp">
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.
Don't use
Right:
<source lang="cpp">
<source lang="cpp">
for (auto& frameView : frameViews)
using namespace MyNamespace
frameView->updateLayoutAndStyleIfNeededRecursive();
</source>
OK:
<source lang="cpp">
unsigned frameViewsCount = frameViews.size();
for (unsigned i = 0; i < frameViewsCount; ++i)
frameViews[i]->updateLayoutAndStyleIfNeededRecursive();
</source>
</source>


== Classes ==
C++ namespaces and the using namespace <code>MyNamespace</code> statement can confuse '''lupdate'''. It will interpret <code>VClass::tr</code> like <code>MyNamespace::VClass::tr</code>. Runtime translation of these strings will fail because of that.

=== Classes ===
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 ===
== 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">