If you make any contributions to Dolphin after December 1st, 2014, you are agreeing that any code you have contributed will be licensed under the GNU GPL version 2 (or any later version).
This guide is for developers who wish to contribute to the Dolphin codebase. It will detail how to properly style and format code to fit this project. This guide also offers suggestions on specific functions and other varia that may be used in code.
Following this guide and formatting your code as detailed will likely get your pull request merged much faster than if you don't (assuming the written code has no mistakes in itself).
int* var
, not int *var
./* Comment text */
), use single-line comments (// Comment text
) instead.Don't collapse single line conditional or loop bodies onto the same line as its header. Put it on the next line.
if (condition)
return 0;
while (var != 0)
var--;
if (condition) return 0;
while (var != 0) var--;
class SomeClassName
enum IPCCommandType
const double PI = 3.14159;
const int MAX_PATH = 260;
int this_variable_name;
g_
m_
s_
Do not leave else
or else if
conditions dangling unless the if
condition lacks braces.
if (condition)
{
// code
}
else
{
// code
}
if (condition)
// code line
else
// code line
if (condition)
{
// code
}
else
// code line
struct
for this. Use a class
otherwise.public
, protected
, and then private
.
static
variables before the non-static ones.class ExampleClass : public SomeParent
{
public:
ExampleClass(int x, int y);
int GetX() const;
int GetY() const;
protected:
virtual void SomeProtectedFunction() = 0;
static float s_some_variable;
private:
int m_x;
int m_y;
};
NULL
.goto
unless you have a really good reason for it.new
) as much as possible. There are cases where using a raw pointer is unavoidable, and in these situations it is OK to use them. An example of this is functions from a C library that require them. In cases where it is avoidable, the STL usually has a means to solve this (vector
, unique_ptr
, etc).auto
keyword everywhere. While it's nice that the type can be determined by the compiler, it cannot be resolved at 'readtime' by the developer as easily. Use auto only in cases where it is obvious what the type being assigned is (note: 'obvious' means not having to open other files or reading the header file). Some situations where it is appropriate to use auto
is when iterating over a std::map
container in a foreach loop, or to shorten the length of container iterator variable declarations.using namespace [x];
in headers. Try not to use it at all if you can.++var
).#ifdef
block unless the source file itself is system-specific).[Dolphin Root]/Source/Core
directory.#pragma once
as header guards.for (;;)
, use while (true)
.while (condition) {}
while (condition);
do
{
} while (false);
const
.Functions that specifically modify their parameters should have the respective parameter(s) marked as a pointer so that the variables being modified are syntaxically obvious.
template<class T>
inline void Clamp(T& val, const T& min, const T& max)
{
if (val < min)
val = min;
else if (val > max)
val = max;
}
Example call: Clamp(var, 1000, 5000);
template<class T>
inline void Clamp(T* val, const T& min, const T& max)
{
if (*val < min)
*val = min;
else if (*val > max)
*val = max;
}
Example call: Clamp(&var, 1000, 5000);
Class member functions that you do not want to be overridden in inheriting classes should be marked with the final
specifier.
class ClassName : ParentClass
{
public:
void Update() final;
};
override
specifier to make it easier to see which functions belong to the parent class. class ClassName : ParentClass
{
public:
void Update() override;
};
final
specifier. class ClassName final : ParentClass
{
// Class definitions
};
The Android project is currently written in Java. If you are using Android Studio to contribute, you can import the project's code style from code-style-java.jar
, located in [Dolphin Root]/Source/Android
. Please organize imports before committing.