Jump to content

Hacking:Code style conventions: Difference between revisions

no edit summary
No edit summary
Line 1:
== Code style conventionsGeneral ==
 
=== General ===
 
Max length of source code line equal 120 characters. Put "//---" cars in front of function definitions with maximum length (120) :
Line 9 ⟶ 7:
</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.
 
Line 25 ⟶ 30:
</source>
 
==== Spacing ====
Do not place spaces around unary operators.
 
Line 61 ⟶ 66:
</source>
 
==== Line breaking ====
Each statement should get its own line.
<source lang="cpp">
Line 72 ⟶ 77:
</source>
 
==== Braces ====
 
Use typically trailing braces everywhere (if, else, functions, structures, typedefs, class definitions, etc.)
Line 94 ⟶ 99:
</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>.
 
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.
<source lang="cpp">
Line 113 ⟶ 125:
</source>
 
==== Names ====
Function names start with an upper case:
<source lang="cpp">
Line 206 ⟶ 218:
</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.
 
Negation operator should not be used in its short form :
<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>
 
Prefer index over iterator in Vector iterations for terse, easier-to-read code.
Don't use
Right:
<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>
 
=== 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.
<source lang="cpp">
Line 318 ⟶ 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].
 
=== 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.
<source lang="cpp">
Cookies help us deliver our services. By using our services, you agree to our use of cookies.