Skip to content

Coding Style

Felix Turowsky edited this page Apr 25, 2021 · 6 revisions

Coding Style

This page is under construction. Check here for the discussion.

See https://wiki.qt.io/Qt_Coding_Style for general code style guidelines.

item convention example
Directory All lowercase with - for word separator if necessary. src/subdirectory/directory-of-doom/
File All lowercase, follow Qts conventions. src/subdirectory/directory-of-doom/adatabase.h
Class PascalCase, follow Qt convention but change Q to A class ADataBase
Methods camelCase methodOfClass
Member variable camelCase importantVariable
Free Function camelCase with an extra a aConquerTheWorld()
Static Function Without a and snake_case to emphasise limited scope void utility_function_one()
Limited scope variables snake_case temporary_variable
constants All capital snake_case const auto IMPORTANT_CONSTANT = ASecretOfUniverse(42);
Type Alias follow Qt Convention using NewType = QMap<QString, int>

Boolean Expressions

Consider the following:

if(!selected) { ... }

In this case what does selected mean? Selected or not, so bool? or the selected pointer is null? or perhaps selected is a numerical so 0? In order to avoid confusion and make the code more readable, be explicit when using boolean expressions to compare numerical values, enums and pointers.

  • Numeric

    if(i == 0) ...  // Good
    if(!i) ...      // Bad
  • Enum

    if(e == Enum::someValue) // Good
    if(e == 7)               // Bad
  • Pointers

    if(p == nullptr) // Good
    if(!p)           // Bad
  • For bool variables, the abbreviated notation is acceptable:

    if(!b)         // This is fine
    if(b == false) // This is also fine

Strings in openPilotLog

Aim to construct and use strings in the most efficient way.

  • For all user-facing strings use tr(), so that the app is translatable.

  • Use QStringLiteral where possible, except when using a function that has a char * overload.

  • For string comparisons using the == operator, use QLatin1String

  • For concatenations, openPilotLog uses QStringBuilder globally, so the + operator can be used.

  • (Debug etc. can be char *, QString or anything else)

Examples

const auto IMPORTANT_CONSTANT = ASecretOfUniverse(42);

APilotEntry(int throwaway_variable)
    : importantMemberVariable(throwaway_variable)
{}
- AFooBar           - an important custom class.
- FooBar            - alias of important type.
- aFooBar()         - an important free function
- foo_bar()         - a static "utility" function
- niceVariable      - an important variable
- nice_variable     - a "consumable", limited scope variable.
class Foo {
    int rowId;
    Foo(int row_id) : rowId(row_id) {}
    void set(int parameter);
}

void Foo::set(int parameter)
{
   auto calc_flag = false;
   // complex calculations
   if(!calc_flag)
       return;
   ...
}
Clone this wiki locally