Official:Code guidelines and formatting conventions: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
(Created page with "== Use common sense == 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 se...")
 
No edit summary
(22 intermediate revisions by 7 users not shown)
Line 1: Line 1:
== Use common sense ==
{{mininav|[[Development]]}}
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.
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. For historic reason the current code doesn't always follow these guidelines however any new code should certainly follow them.


As such we don't follow these rules slavishly, in certain cases it is ok (and in fact favorable) to stray from them.
These coding guidelines and formatting conventions are part of our Doxygen documentation which can be generated from code. For easy viewing we also made these guidelines available online https://codedocs.xyz/xbmc/xbmc/code_guidelines.html
== Indentation ==
Use spaces as tab policy with an indentation size of 2


== Braces ==
Braces should go to newline and your code should looks like the following example:
<pre>
if (int i = 0; i < t; i++)
{
  [...]
}
else
{
  [...]
}


class Dummy()
[[Category:Development]]
{
  [...]
}
</pre>
 
== Whitespaces ==
Conventional operators should be surrounded by a whitespace
<pre>
a = (b + c) * d;
</pre>
 
Reserved words should separated from opening parentheses by a whitespace
<pre>
while (true)
</pre>
 
Commas should be followed by a whitespace
<pre>
void Dummy::Method(int a, int b, int c);
int d, e;
</pre>
 
Semicolons should be followed by a whitespace, if there is more than one expression per line
<pre>
for (int a = 0; b++; c < d)
doSomething(e); doSomething(f); // this is probably bad style anyway
</pre>
 
== Switch / case ==
<pre>
switch (cmd)
{
  case x:
  {
    doSomething();
    break;
  }
  case x:
  case z:
    return true;
  default:
    doSomething();
}
</pre>
 
== Naming ==
=== Constants ===
Use upper case with underscore spacing where necessary.
<pre>
const int MY_CONSTANT = 1;
</pre>
 
=== Enums ===
Use CamelCase for the enum name and upper case for the values.
<pre>
enum Dummy
{
  VALUE_X,
  VALUE_Y
};
</pre>
 
=== Classes/Methods ===
We use CamelCase for class names and methods both with first letter in upper case.
<pre>
class MyDummyClass();
void MyDummyClass::DoSomething();
</pre>
 
=== Variables ===
We use CamelCase for variables and type prefixing is optional.
 
==== Global Variables ====
Prefix global variables with ''g_''
<pre>
int g_globalVariableA;
</pre>
 
==== Member Variables ====
Prefix member variables with ''m_''
<pre>
int m_variableA;
</pre>

Revision as of 08:27, 30 April 2016

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. For historic reason the current code doesn't always follow these guidelines however any new code should certainly follow them.

These coding guidelines and formatting conventions are part of our Doxygen documentation which can be generated from code. For easy viewing we also made these guidelines available online https://codedocs.xyz/xbmc/xbmc/code_guidelines.html