Update CodingStyle.md: placement of *, & and && declarators

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-07-26 14:01:16 +02:00 committed by Dirk Hohndel
parent c7f0e65b12
commit d3dc698bba

View file

@ -20,7 +20,6 @@ other editors that implement this coding style, please add them here.
continuation lines that are aligned with tabs and then spaces continuation lines that are aligned with tabs and then spaces
* all keywords followed by a '(' have a space in between * all keywords followed by a '(' have a space in between
``` ```
if (condition) if (condition)
@ -28,7 +27,6 @@ other editors that implement this coding style, please add them here.
``` ```
* function calls do NOT have a space between their name and argument * function calls do NOT have a space between their name and argument
``` ```
i = some_function(argument); i = some_function(argument);
``` ```
@ -41,7 +39,6 @@ other editors that implement this coding style, please add them here.
* all other opening curly braces follow at the end of the line, with a * all other opening curly braces follow at the end of the line, with a
space separating them: space separating them:
``` ```
if (condition) { if (condition) {
dosomething(); dosomething();
@ -50,7 +47,6 @@ other editors that implement this coding style, please add them here.
``` ```
* both sides of an if / else clause either use or do not use curly braces: * both sides of an if / else clause either use or do not use curly braces:
``` ```
if (condition) if (condition)
i = 4; i = 4;
@ -66,13 +62,11 @@ other editors that implement this coding style, please add them here.
``` ```
* use space to make visual separation easier * use space to make visual separation easier
``` ```
a = b + 3 + e / 4; a = b + 3 + e / 4;
``` ```
* continuation lines have the operator / comma at the end * continuation lines have the operator / comma at the end
``` ```
if (very_long_condition_1 || if (very_long_condition_1 ||
condition_2) condition_2)
@ -83,7 +77,6 @@ other editors that implement this coding style, please add them here.
* in a C++ constructor initialization list, the colon is on the same line and * in a C++ constructor initialization list, the colon is on the same line and
continuation lines are aligned as the rule above: continuation lines are aligned as the rule above:
``` ```
ClassName::ClassName() : x(1), ClassName::ClassName() : x(1),
y(2), y(2),
@ -93,14 +86,11 @@ other editors that implement this coding style, please add them here.
``` ```
* unfortunate inconsistency * unfortunate inconsistency
* C code usually uses underscores to structure names - C code usually uses underscores to structure names
``` ```
variable_in_C variable_in_C
``` ```
- In contrast, C++ code usually uses camelCase
* C++ code usually uses camelCase
``` ```
variableInCPlusPlus variableInCPlusPlus
``` ```
@ -110,7 +100,6 @@ other editors that implement this coding style, please add them here.
* switch statements with blocks are a little bit special (to avoid indenting * switch statements with blocks are a little bit special (to avoid indenting
too far) too far)
``` ```
switch (foo) { switch (foo) {
case FIRST: case FIRST:
@ -130,7 +119,22 @@ other editors that implement this coding style, please add them here.
In C code we really like them to be at the beginning of a code block, In C code we really like them to be at the beginning of a code block,
not interspersed in the middle. not interspersed in the middle.
in C++ we are a bit less strict about this - but still, try not to go in C++ we are a bit less strict about this - but still, try not to go
crazy. crazy. Notably, in C++ the lifetime of a variable often coincides with the
lifetime of a resource (e.g. file) and therefore the variable is defined
at the place where the resource is needed.
* The `*`, `&` and `&&` declarators are grouped with the name, not the type
(classical C-style) as in `char *string` instead of `char* string`. This
reflects the precedence rules of the language: `int &i` means that the name
`i` stands for a reference [to an object with type `int`], not that
`i` stands for an object of the type [reference to `int`].
Although this may seem like hairsplitting (both interpretations
have the same effect) it is crucial in the
definition of multiple variables, such
as
```
struct dive *next, **pprev;
```
* In C++ code, we generally use explicit types in variable declarations for clarity. * In C++ code, we generally use explicit types in variable declarations for clarity.
Use `auto` sparingly and only in cases where code readability improves. Use `auto` sparingly and only in cases where code readability improves.
@ -157,12 +161,11 @@ other editors that implement this coding style, please add them here.
spelling and terminology. spelling and terminology.
User-visible strings should be passed to the tr() function to enable User-visible strings should be passed to the tr() function to enable
translation into other languages. translation into other languages.
- like this
* like this
``` ```
QString msgTitle = tr("Submit user survey."); QString msgTitle = tr("Submit user survey.");
``` ```
* rather than - rather than
``` ```
QString msgTitle = "Submit user survey."; QString msgTitle = "Submit user survey.";
``` ```
@ -170,7 +173,7 @@ other editors that implement this coding style, please add them here.
This works by default in classes (indirectly) derived from QObject. Each This works by default in classes (indirectly) derived from QObject. Each
string to be translated is associated with a context, which corresponds string to be translated is associated with a context, which corresponds
to the class name. Classes that are not derived from QObject can generate to the class name. Classes that are not derived from QObject can generate
the tr() functions by using the Q_DECLARE_FUNCTIONS macro: the tr() functions by using the `Q_DECLARE_FUNCTIONS` macro:
``` ```
#include <QCoreApplication> #include <QCoreApplication>
@ -202,7 +205,7 @@ other editors that implement this coding style, please add them here.
when the particular translation string is encountered for the first time. when the particular translation string is encountered for the first time.
It remains valid during the whole application's life time. It remains valid during the whole application's life time.
Outside of function context, the QT_TRANSLATE_NOOP macro can be used as in Outside of function context, the `QT_TRANSLATE_NOOP` macro can be used as in
``` ```
struct ws_info_t ws_info[100] = { struct ws_info_t ws_info[100] = {
{ QT_TRANSLATE_NOOP("gettextFromC", "integrated"), 0 }, { QT_TRANSLATE_NOOP("gettextFromC", "integrated"), 0 },
@ -214,7 +217,7 @@ struct ws_info_t ws_info[100] = {
``` ```
Note that here, the texts will be scheduled for translation with the "gettextFromC" Note that here, the texts will be scheduled for translation with the "gettextFromC"
context, but the array is only initialized with the original text. The actual context, but the array is only initialized with the original text. The actual
translation has to be performed later in code. For C-code, the QT_TRANSLATE_NOOP translation has to be performed later in code. For C-code, the `QT_TRANSLATE_NOOP`
macro is defined in the "core/gettext.h" header. macro is defined in the "core/gettext.h" header.
* UI text style * UI text style
@ -236,11 +239,9 @@ struct ws_info_t ws_info[100] = {
* string manipulation * string manipulation
* user interface * user interface
In UI part of the code use of QString methods is preferred, see this pretty In UI part of the code use of QString methods is preferred, see this pretty
good guide in [QString documentation][1] good guide in [QString documentation][1]
* core components * core components
In the core part of the code, C-string should be used. In the core part of the code, C-string should be used.
C-string manipulation is not always straightforward specifically when C-string manipulation is not always straightforward specifically when
@ -248,7 +249,6 @@ struct ws_info_t ws_info[100] = {
to help with this. Documentation and usage examples can be found in to help with this. Documentation and usage examples can be found in
[core/membuffer.h][2] [core/membuffer.h][2]
## Sample Settings ## Sample Settings
### Emacs ### Emacs