Difference between revisions of "Official:Code guidelines and formatting conventions"

From Official Kodi Wiki
Jump to: navigation, search
(Control statements: typo)
(Braces)
Line 7: Line 7:
  
 
== Braces ==
 
== Braces ==
Braces should go to newline and your code should looks like the following example:
+
Braces should go to newline and your code should look like the following example:
 
<pre>
 
<pre>
 
if (int i = 0; i < t; i++)
 
if (int i = 0; i < t; i++)

Revision as of 11:16, 24 March 2015

Home icon grey.png   ▶ Development ▶ Code guidelines and formatting conventions

These are conventions which we try to follow when writing code for Kodi. They are this way mainly for reasons of taste, however, sticking to a common set of formatting rules also makes it slightly easier to read through our sources. If you want to submit patches, please try to follow these rules.

As such we don't follow these rules slavishly, in certain cases it is ok (and in fact favorable) to stray from them.

1 Indentation

Use spaces as tab policy with an indentation size of 2

2 Braces

Braces should go to newline and your code should look like the following example:

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

class Dummy()
{
  [...]
}

3 Whitespaces

Conventional operators should be surrounded by a whitespace.

a = (b + c) * d;

Reserved words should separated from opening parentheses by a whitespace.

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

Commas should be followed by a whitespace.

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

Semicolons should be followed by a whitespace if there is more than one expression per line.

for (int i = 0; i < x; ++i)
doSomething(e); doSomething(f); // this is probably bad style anyway

4 Control statements

Insert new line before

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

4.1 if else

  • put then statement, return or throw to new line
  • keep else if on one line
if (true) 
  return;
if (true) 
{
  return;
} 
else if (false) 
{
  return;
} 
else
{
  return;
}

4.2 switch / case

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

5 Naming

5.1 Constants

Use upper case with underscore spacing where necessary.

const int MY_CONSTANT = 1;

5.2 Enums

Use CamelCase for the enum name and upper case for the values.

enum Dummy
{
  VALUE_X,
  VALUE_Y
};

5.3 Classes/Methods

We use CamelCase for class names and methods both with first letter in upper case.

class MyDummyClass();
void MyDummyClass::DoSomething();

5.4 Variables

We use CamelCase for variables and type prefixing is optional.

5.4.1 Global Variables

Prefix global variables with g_

int g_globalVariableA;

5.4.2 Member Variables

Prefix member variables with m_

int m_variableA;