OpenTX Code Style Guide - stodev-com-br/opentx GitHub Wiki
We prefer Unix style line ends and will eventually convert all existing source files to Unix line ends.
Use two spaces for indentation.
- the beginning curly brace goes on the next line
- the name of the function (if composed of several words) uses lower case first word and Capital case for other words.
- the parameters and other variables follow the same syntax as function names.
- start the functions on one module with the same keyword if it makes sense. For example all functions dealing with sd card start with
sd
- e.g.sdOpen()
,sdClose()
Example:
int someFunction(const char * firstParameter, uin32_t secondParameter)
{
// one liner comment, use space before the comment char and first word
return 4;
}
Either write the call all on a single line, wrap the arguments at the parenthesis,
bool result = someFunction(longlonglonglonglongaFirstParameter,
secondParameter, thirdParameter);
or start the arguments on a new line indented by four spaces and continue at that 4 space indent.
bool result = someFunction(
firstParameter, secondParameter,
thirdParameter);
The if and else keywords belong on separate lines. There should be a space between the if and the open parenthesis, and between the close parenthesis and the curly brace (if any), but no space between the parentheses and the condition.
if (condition) { // no spaces inside parentheses
... // 2 space indent.
}
else if (...) { // The else goes on the same line as the closing brace.
...
}
else {
...
}
In general, curly braces are not required for single-line statements, but they are allowed
if (condition)
doSomething(); // 2 space indent.
Switch statements may use braces for blocks.
Blocks case in switch statements can have curly braces. If included, curly braces should be placed as in example:
switch (var) {
case 0: { // 2 space indent
... // 4 space indent
break;
}
case 1: {
...
break;
}
}
Braces are optional for single-statement loops.
for (int i = 0; i < n; ++i)
DoSomething();
Empty loop bodies should use either empty braces or contains continue.
while (condition) {
}
- use Camel Case for class names (all words with first letter capitalized)
- where possible, prefer classes
- use Camel Case for class names (all words with first letter capitalized)
- methods follow the same syntax as normal functions
- try to use the
const
function attribute where possible
Example:
class SomeClass {
public:
SomeClass();
private:
int classVariable;
int classPrivateInlineFunction(int someParam) const
{
return someParam * 2;
}
};
- block statements begin with an opening curly brace on the same line
Example:
if (foo == bar) {
// some code (usually multiple lines)
}
else if (foo >= bar || bar <= 6) {
// some code (usually multiple lines)
}
else {
// some code (usually multiple lines)
}
Enumerators (for both scoped and unscoped enums) should be named like constants: ENUM_NAME.
Example:
enum Analogs {
STICK,
TX_VOLTAGE,
};
They should be avoided when possible. If needed they should be named with all capitals and underscores.
Example:
#define ROUND(x) ...
#define PI_ROUNDED 3.0
The contents of namespaces are not indented.
namespace {
void foo() { // Correct. No extra indentation within namespace.
...
}
} // namespace