Coding Standard - PLLUG/CPPQT-2019-1-FileServer GitHub Wiki
{
// code line 1
// code line 2
{
// inner block
//
}
}
if (longExpression
+ otherLongExpression
+ otherOtherLongExpression)
{
}
if (longExpression
+ otherLongExpression
+ otherOtherLongExpression)
{
}
Spaces.1 Always use blank lines to group statements together where suited. Always use only one blank line.
if (foo)
{
}
Spaces.3 For pointers or references, always use a single space between the type and * or &, but no space between the * or & and the variable name.
Spaces.4 No space is allowed between return type and * or & in function/method definitions/declarations.
Spaces.5 No space is allowed between function/method name and braces in function/method definitions/declarations/call.
const char* ch;
const void *const someConstant;
std::string &str = someString;
char* func1();
char *blockOfMemory = reinterpret_cast<char*>(malloc(data.size()));
if (foo)
{
}
// Wrong
if (address.isEmpty())
return false;
// Wrong
for (int i = 0; i < 10; ++i) qDebug("%i", i);
// Correct
if (address.isEmpty())
{
return false;
}
[%General]
DefaultFileEncoding=@ByteArray(UTF-8)
[textMarginSettings]
MarginColumn=100
ShowMargin=true
[textStorageSettings]
addFinalNewLine=true
cleanIndentation=true
cleanWhitespace=true
inEntireDocument=true
[textTabPreferences]
AutoSpacesForTabs=false
IndentSize=4
PaddingMode=1
SpacesForTabs=true
TabSize=4
[textTypingSettings]
AutoIndent=true
SmartBackspaceBehavior=1
TabKeyBehavior=0
// Wrong
if (a && b || c)
// Wrong
a + b & c
// Correct
if ((a && b) || c)
// Correct
(a + b) & c
Stat.3 Every case must have a break (or return) statement at the end or a comment to indicate that there’s intentionally no break.
switch (myEnum)
{
case Value1:
doSomething();
break;
case Value2:
// nothing
break;
default:
defaultHandling();
break;
}
Name.1 Use camel case style for names (e. g. each consecive word in a name starts with a capital letter)
Name.9 Local variables and function/method parameters that are not static or constant and do not return value from function/method - has no prefix.
Var.2 Avoid abbraviations, short, unclear or hard to understand/remember variable names whenever possible.
Var.3 Single character variable names are only okay for counters, where the purpose of the variable is obvious or in math calculation (only when meaning of variable is obvious).
// Wrong
int a, b;
char *c, *d;
int foo;
int bar, baz;
void *scsdSgPtr;
char *Var, VAR;
// Correct
int height;
int width;
char *userName;
QHash<int, QString> mNameByIndex;
QWidget *mTimeDisplayWidget;
QTimer mRequestTimeoutTimer;
for (int i = 0; i < count; ++i) {.....}
while (k < count) {++k}
Var.5 Use auto only for local variables where precision or range should not be specified explicitly.
// Not desirable
int i = 1.02; // Narrowing
int i = {1} ;
// Right
int i {1}; // Also saves from narrowing
QList<QString> vegetables{"potato", "tomato", "cabbage"};
auto i = 0;
enum class TrafficLightColors {Red, Green, Blue}
Here will be rules in addition to CPP Core Guidelines.