Contributing code: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
(Add toclimit-2 class to TOC div tag)
(Add toclimit-2 class to TOC div tag)
 
(No difference)

Latest revision as of 14:28, 29 September 2021

As a large and mature open source software project, Team Kodi has to integrate the work of hundreds of developers into a single, cohesive codebase. This is accomplished by insisting that those who wish to contribute do so according to a clear and explicit set of conventions, procedures and rubrics. This article is meant to organize them on a single page but is far from exhaustive; deeper study is almost certain to be required.

Motivation

When working in a large group, the two most important values are readability and maintainability. We code for other people, not computers. To accomplish these goals, we have created a unified set of code conventions. Conventions can be bent or broken in the interest of making code more readable and maintainable. However, if you submit a patch that contains excessive style conflicts, you may be asked to improve your code before your pull request is fully reviewed.

In the repository root directory, there is a .clang-format file that implements the rules as specified here. You are encouraged to run clang-format on any newly created files. It is currently not recommended to do so on preexisting files because all the formatting changes will clutter your commits and pull request.

Language standard

We currently target the C++14 language standard, so do use C++14 features when possible, but do not use C++17 features.

Formatting

Line length

The ColumnLimit in .clang-format is set to 100, which defines line length (i.e. where lines should be broken) that allows two documents to be displayed side-by-side on a 1080p screen for diffs at a standard DPI.

Braces

Curly braces always go on a new line.

for (int i = 0; i < t; i++)
{
  [...]
}

if (true)
{
  [...]
}

class Dummy
{
  [...]
};

Indentation

We use spaces as opposed to tabs to indent, with each level of indentation being two spaces further from the left margin than its parent. Opening curly braces increase the level of indentation for the code enclosed by them, and likewise closing curly braces decrease the level of indentation.

Exception: Do not indent namespaces to simplify nesting them and wrapping .cpp files in a namespace.

namespace KODI
{
namespace UTILS
{

class ILogger
{
public:
  virtual void Log(...) = 0;
  virtual ~ILogger() {}
}

}
}

Control statements

Insert a new line before every:

  • else in an if statement
  • catch in a try statement
  • while in a do statement

If/else

Put the consequent on a new line, if not in curly braces anyway. Keep else if (elif) statements on one line.

if (true)
  return;

if (true)
{
  [...]
}
else if (false)
{
  return;
}
else
  return;

Switch cases

switch (cmd)
{
  case x:
  {
    doSomething();
    break;
  }
  case x:
  case z:
    return true;
  default:
    doSomething();
}

Try/catch

try
{
  [...]
}
catch (std::exception& e)
{
  [...]
  throw;
}
catch (...)
{
  [...]
}

Whitespace

Conventional operators have to be surrounded by one space on each side.

a = (b + c) * d;

Control statement keywords have to be separated from opening parentheses by one space.

while (true);
for (int i = 0; i < x; i++);

Commas have to be followed by one space.

void Dummy::Method(int a, int b, int c);

Initializer lists have one space after each element (including the comma), but no surrounding spaces.

constexpr int aNiceArray[] = {1, 2, 3};

Vertical alignment

Do not use whitespace to vertically align around operators or values. This causes problems on code review if one needs to realign all values to their new position, producing unnecessarily large diffs.

✅ Good:

int value1{1};
int value2{2};
CExampleClass* exampleClass{};
CBiggerExampleClass* biggerExampleClass{};
exampleClass = new CExampleClass(value1, value2);
biggerExampleClass = new CBiggerExampleClass(value1, value2);
exampleClass->InitExample();
biggerExampleClass->InitExample();

❌ Bad:

int                  value1             {1};
int                  value2             {2};
[...]
CExampleClass       *exampleClass       {};
CBiggerExampleClass *biggerExampleClass {};
[...]
exampleClass       = new CExampleClass      (value1, value2);
biggerExampleClass = new CBiggerExampleClass(value1, value2);
[...]
exampleClass      ->InitExample();
biggerExampleClass->InitExample();

Superfluous void

Do not write void in empty function parameter declarations.

✅ Good:

void Test();

❌ Bad:

void Test(void);

Exceptions to formatting rules

There are some special situations where vertical alignment and longer lines do greatly aid readability, for example the initialization of some table-like multiple row structures. In these rare cases, exceptions can be made to the formatting rules on vertical alignment, and the defined line length may be exceeded as well if it provides an obvious benefit.

The layout can be protected from being reformatted when clang-format is applied by adding // clang-format off and // clang-format on statements both above and below the lines of code which violate the rules. For example,

// clang-format off
static const CGUIDialogMediaFilter::Filter filterList[] = {
  { "movies",       FieldTitle,         556,    SettingType::String,  "edit",   "string",   CDatabaseQueryRule::OPERATOR_CONTAINS },
  { "movies",       FieldRating,        563,    SettingType::Number,  "range",  "number",   CDatabaseQueryRule::OPERATOR_BETWEEN },
  { "movies",       FieldUserRating,    38018,  SettingType::Integer, "range",  "integer",  CDatabaseQueryRule::OPERATOR_BETWEEN },
  ...
  { "songs",        FieldSource,        39030,  SettingType::List,    "list",   "string",   CDatabaseQueryRule::OPERATOR_EQUALS },
};  
// clang-format on

but the other code guidelines will still need to be applied within the delimited lines of code, and with clang-format off, care will be needed to enforce these manually. Using vertical alignment means that sometimes the entire block of code may need to be realigned, and good judgement should be used in each case with the objective of preserving readability yet minimizing impact.

This is to be used with discretion; marking large amounts of code to be left unformatted by clang-format without reasonable justification will result in rejection of the submission.

Statements

Multiple statements

Do not put multiple statements on a single line; always use a new line for a new statement. It is much easier to debug if one can pinpoint a precise line number.

✅ Good:

std::vector<std::string> test;
test.push_back("foobar");

❌ Bad:

std::vector<std::string> test; test.push_back("foobar");

Switch statement default case

In every switch structure, always include a default case, unless switching on an enum and all enum values are explicitly handled.

Declarations

Multiple declarations

Do not put multiple declarations on a single line, this avoids confusion with differing pointers, references, and initialization values on the same line (cf. ISO C++ guidelines).

✅ Good:

char* a;
char b;

❌ Bad:

char* a, b;

Pointer and reference types

Left-align * and & to the base type they modify.

✅ Good:

char* a;
void test(const std::string& b);

❌ Bad:

char *a;
char * b;
void test(const std::string &b);

This is adopted from the HP C++ Coding Guidelines:

The characters * and & are to be written with the type of variables instead of with the name of variables in order to emphasize that they are part of the type definition.

Const and other modifiers

Place const and similar modifiers in front of the type they modify.

✅ Good:

void Test(const std::string& a);
const int* const someIntPointer;

❌ Bad:

void Test(std::string const& a);
int const * const someIntPointer;

Initialization

Make sure that variables are initialized appropriately at declaration or soon afterwards. This is especially important for fundamental type variables that do not have any constructor. Zero-initialize with {}.

✅ Good:

int x{};
int* y{};
CLog::Log("test: {} {}", x, y);

❌ Bad:

int x; // used uninitialized
int* y = nullptr; // default-initialization not using {}
CLog::Log("test: {} {}", x, y);

In general, prefer the {} initializer syntax over alternatives. This syntax is less ambiguous and disallows narrowing (cf. ISO C++ guidelines).

✅ Good:

int x{5};
int y{x};

Scoping

Namespaces

Try to put all code into appropriate namespaces (following the directory structure) and avoid polluting the global namespace.

Local functions

Put functions local to a compilation unit into an anonymous namespace.

✅ Good:

namespace
{

void test();

}

❌ Bad:

static void test();

Headers

Included header files have to be sorted (case-sensitively) alphabetically to prevent duplicates and allow better overview, with an empty line clearly separating sections.

The header order has to be:

  • Eponymous header file
  • Platform-independent Kodi includes
  • platform-specific Kodi includes
  • C and C++ system files
  • Other libraries' header files
  • Special Kodi headers (i.e. PlatformDefs.h, system.h and system_gl.h)
#include "PVRManager.h"

#include "Application.h"
#include "ServiceBroker.h"
#include "addons/AddonInstaller.h"
#include "dialogs/GUIDialogExtendedProgressBar.h"
#include "messaging/ApplicationMessenger.h"
#include "messaging/ThreadMessage.h"
#include "messaging/helpers/DialogHelper.h"
#include "music/MusicDatabase.h"
#include "music/tags/MusicInfoTag.h"
#include "network/Network.h"
#include "pvr/addons/PVRClients.h"
#include "pvr/channels/PVRChannel.h"
#include "settings/Settings.h"
#include "threads/SingleLock.h"
#include "utils/JobManager.h"
#include "utils/Variant.h"
#include "utils/log.h"
#include "video/VideoDatabase.h"

#include <cassert>
#include <utility>

#include <libavutil/pixfmt.h>

If the headers aren't sorted, either do your best to match the existing order, or precede your commit with an alphabetization commit. If possible, avoid including headers in another header. Instead, you can forward-declare the class and use a std::unique_ptr (or similar):

class CFileItem;

class Example
{
  ...
  std::unique_ptr<CFileItem> m_fileItem;
}

Naming

Namespaces

Use upper case with underscores.

namespace KODI
{
[...]
}

Constants

Use upper case with underscores.

constexpr int MY_CONSTANT = 1;

Enums

Use PascalCase for the enum name and upper case with underscores for the values.

enum class Dummy
{
  VALUE_X,
  VALUE_Y
};

Interfaces

Use PascalCase and prefix with an uppercase I. The filename has to match the interface name without the prefixed I, like this example from Logger.h:

class ILogger
{
public:
  virtual void Log(...) = 0;
  virtual ~ILogger() {}
}

Classes

Use PascalCase and prefix with an uppercase C. Again, the filename has to match the class name without the prefixed C, as shown here from Logger.cpp:

class CLogger : public ILogger
{
public:
  void Log(...) override;
}

Methods

Use PascalCase always, uppercasing the first letter even if the methods are private or protected.

void MyDummyClass::DoSomething();

Variables

Use CamelCase. Type prefixing (a/k/a Systems Hungarian notation) is discouraged.

Member variables

Prefix non-static member variables with m_. Prefix static member variables with ms_.

int m_variableA;
static int ms_variableB;

Global variables

Prefix global variables with g_

int g_globalVariableA;
Stop hand.png Avoid use of globals as far as reasonably possible. We generally do not want to introduce new global variables.


Comments

General

Use // for inline single-line and multi-line comments. Use /* */ for the copyright comment at the beginning of the file. SPDX license headers are required for all code files (see example below).

✅ Good:

/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

// Nice comment

// This can also continue for multiple lines:
// I am the second line.

❌ Bad:

/* A comment */
//another comment

Doxygen

New classes and functions are expected to have Doxygen comments describing their purpose, parameters, and behavior in the header file. However, do not describe trivialities - it only adds visual noise. Use the Qt style with an exclamation point (/*! */) and backslash for doxygen commands (e.g. \brief).

✅ Good:

/*!
 * \brief Splits the given input string using the given delimiter into separate strings.
 *
 * If the given input string is empty, nothing will be put into the target iterator.
 *
 * \param destination the beginning of the destination range
 * \param input input string to be split
 * \param delimiter delimiter to be used to split the input string
 * \param maxStrings (optional) maximum number of splitted strings
 * \return output iterator to the element in the destination range one past the last element
 *         that was put there
 */
template<typename OutputIt>
static OutputIt SplitTo(OutputIt destination, const std::string& input, const std::string& delimiter, unsigned int maxStrings = 0);

❌ Bad:

/**
 * @brief Function for documentation purposes (javadoc style)
 */
void TestFunction();

void ReallyComplicatedFunction(); // does something really complicated

/*!
 * \brief Frobnicate a parameter
 * \param param parameter to frobnicate
 * \result frobnication result
 */
int Frobnicate(int param);

Logging

Use the provided logging function CLog::Log. Do not log to standard output or standard error using, for example, printf or std::cout.

The Log function uses the fmt library for formatting log messages. Basically, you can use {} as a placeholder for anything and list the parameters to insert after the message, similar to printf. See here for the detailed syntax, and below for an example.

✅ Good:

CLog::Log(LOGDEBUG, "Window size: {}x{}", width, height);

❌ Bad:

CLog::Log(LOGWARNING, "Window size: %dx%d", width, height); // printf-style format strings are possible, but discouraged; also the message does not warrant the warning level
printf("Window size: %dx%d", width, height);
std::cout << "Window size: " << width << "x" << height << std::endl;

The predefined logging levels are DEBUG, INFO, NOTICE, WARNING, ERROR, SEVERE, and FATAL. Use anything INFO and above sparingly, since it will be written to the log by default. Too many messages will clutter the log and reduce visibility of important information. DEBUG messages are only written when debug logging is enabled.

Classes

Member visibility

Make class data members private. Think twice before using protected for data members and functions, as its level of encapsulation is effectively equivalent to public.

Const correctness

Try to mark member functions of classes as const whenever and wherever reasonable.

Overriding virtual functions

When overriding virtual functions of a base class, add the override keyword. Do not add the virtual keyword.

✅ Good:

class CLogger : public ILogger
{
public:
  void Log(...) override;
}

❌ Bad:

class CLogger : public ILogger
{
public:
  virtual void Log(...) override;
}

Default member initialization

Use default member initialization instead of initializer lists or constructor assignments whenever it makes sense.

class Foo
{
  bool bar{false};
};

Destructors in interfaces

A class with any virtual functions should have a destructor that is either public and virtual, or else protected and non-virtual (cf. ISO C++ guidelines).

Constructor Initialization Lists

For lines up to #Line length, everything stays on one line, excluding the braces, which must be on the adjacent lines in both directions.

MyClass::CMyClass(bool bBoolArg, int iIntegerArg) : m_bArg(bBoolArg), m_iArg(iIntegerArg)
{
}

For longer lines, break before a colon and after a comma.

MyClass::CMyClass(bool bBoolArg,
                  int iIntegerArg,
                  const std::string& strSomeText,
                  const std::shared_ptr<CMyOtherClass>& myOtherClass)
  : m_bBoolArg(bBoolArg),
    m_iIntegerArg(iIntegerArg),
    m_strSomeText(strSomeText),
    m_myOtherClass(myOtherClass)
{
}

Other conventions

Output parameters

For functions that have multiple output values, prefer using a struct or tuple return value over output parameters that use pointers or references (cf. ISO C++ guidelines). In general, try to avoid output parameters completely (cf. ISO C++ guidelines), Google C++ Style Guide). At the function call site, it is completely invisible that, actually, a reference is being passed and the value might be modified, whereas return semantics make it clear what is happening.

Casts

New code has to use C++ style casts and not older C style casts. When modifying existing code, the developer can choose to update it to C++ style casts or leave as is. Whenever a dynamic_cast is used to cast to a pointer type, the result can be nullptr and needs to be checked accordingly.

NULL vs nullptr

Prefer the use of nullptr instead of NULL; nullptr is a type-safe version and as such, can't be implicitly converted to int or anything else.

Auto

Feel free to use auto wherever it improves readability, which is not as often the case as it may appear prima facie. Good places are iterators, or when dealing with containers, while bad places are in code that expects a certain type that is not immediately clear from the context.

✅ Good:

auto i = var.begin();

std::vector<CSizeInt> list;
for (const auto j : list)
{
  [...]
}

❌ Bad:

std::map<std::string, std::vector<int>>::iterator i = var.begin();

For loops

Use range-based for loops wherever it makes sense. If iterators are used, see above about using #Auto.

for (const auto& : var)
{
  [...]
}

Remove const if the value has to be modified. Do not use references to fundamental types that are not modified.

Include guards

Use #pragma once.

✅ Good:

#pragma once

❌ Bad:

#ifndef SOME_FILE_H_INCLUDED
#define SOME_FILE_H_INCLUDED
[...]
#endif

Type aliases

Use the C++ using syntax when aliasing types (highly encouraged when it improves readability).

✅ Good:

using SizeType = int;

❌ Bad:

typedef int SizeType;

Goto

The usage of goto is discouraged.

Macros

Try to avoid using C macros; in many cases, they can be easily substituted with other non-macro constructs.

Constexpr

Prefer constexpr over const for constants when possible. Try to mark functions constexpr when reasonable.