> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/dolphin-emu/dolphin/llms.txt
> Use this file to discover all available pages before exploring further.

# Code Style Guide

> C++ coding standards and formatting rules for Dolphin Emulator

## Overview

Dolphin uses **clang-format 19.1** to enforce consistent code formatting across the entire codebase. This guide details the specific coding standards and formatting rules.

<Info>
  In case of conflicts between this guide and clang-format rules, follow clang-format.
</Info>

## Setting Up clang-format

### Automated Formatting

<Tabs>
  <Tab title="Command Line">
    Format all staged files before committing:

    ```bash theme={null}
    git diff --cached --name-only | grep -E '[.](cpp|h|mm)$' | xargs -I {} clang-format -i {}
    ```
  </Tab>

  <Tab title="Pre-commit Hook">
    Enable automatic formatting checks:

    ```bash theme={null}
    ln -s ../../Tools/lint.sh .git/hooks/pre-commit
    ```
  </Tab>

  <Tab title="Git Filter Driver">
    Automatically reformat changes transparently:

    ```bash theme={null}
    git config filter.clang_format.smudge 'cat'
    git config filter.clang_format.clean 'clang-format %f'
    echo '/Source/Core/**/*.cpp filter=clang_format' >> .git/info/attributes
    echo '/Source/Core/**/*.h filter=clang_format' >> .git/info/attributes
    echo '/Source/Core/**/*.mm filter=clang_format' >> .git/info/attributes
    ```
  </Tab>

  <Tab title="Visual Studio">
    Press `Ctrl+K` followed by `Ctrl+D` (or select **Edit → Advanced → Format Document**).

    <Note>
      Visual Studio has built-in clang-format support - no separate installation required.
    </Note>
  </Tab>
</Tabs>

### Line Endings

<Warning>
  Windows users must configure git to checkout UNIX-style line endings:

  ```bash theme={null}
  git config core.autocrlf input
  ```
</Warning>

## General Formatting Rules

### Line Length and Indentation

* **Maximum line length**: 100 characters
* **Indentation**: 2 spaces per level (no tabs)
* **Tab width**: 2 spaces

<Note>
  Try to keep lines under 80-90 characters when possible for better readability.
</Note>

### Brace Placement

Opening braces go on the **next line** for:

* Namespaces
* Classes
* Functions
* Enums, structs, unions
* Conditionals and loops

```cpp theme={null}
// Correct
void MyFunction()
{
  if (condition)
  {
    DoSomething();
  }
}

// Exception: Array initializers and lambdas can keep braces on same line
auto lambda = [](int x) { return x * 2; };
int array[] = {1, 2, 3};
```

### Pointers and References

Place the `*` or `&` against the **type name**, not the variable name:

```cpp theme={null}
// Correct
int* pointer;
int& reference;

// Incorrect
int *pointer;
int &reference;
```

### Comments

Use single-line comments (`//`), not multi-line comments (`/* */`):

```cpp theme={null}
// This is correct
// Multiple lines use multiple single-line comments

/* This is incorrect */
```

### Conditionals and Loops

Don't collapse single-line bodies onto the same line as the header:

```cpp theme={null}
// Correct
if (condition)
  return 0;

while (var != 0)
  var--;

// Incorrect
if (condition) return 0;
while (var != 0) var--;
```

## Naming Conventions

### Classes, Enums, Functions, Structs

Use **UpperCamelCase**. Uppercase abbreviations:

```cpp theme={null}
class SomeClassName { };
enum IPCCommandType { };
void ProcessRequest();
struct GameMetadata { };
```

### Constants

Fully uppercase with underscores:

```cpp theme={null}
constexpr double PI = 3.14159;
constexpr int MAX_PATH = 260;
```

### Variables

Lowercase with underscores:

```cpp theme={null}
int this_variable_name;
float player_speed;
```

### Variable Prefixes

<Info>
  Do not use Hungarian notation except for these specific prefixes:
</Info>

| Prefix | Usage                  | Example            |
| ------ | ---------------------- | ------------------ |
| `g_`   | Global variables       | `g_video_backend`  |
| `m_`   | Class member variables | `m_width`          |
| `s_`   | Static variables       | `s_instance_count` |

```cpp theme={null}
class ExampleClass
{
private:
  int m_x;                    // Member variable
  static int s_instance_count; // Static variable
};

int g_global_counter;          // Global variable
```

## Conditional Statements

Don't leave `else` or `else if` dangling unless the `if` lacks braces:

```cpp theme={null}
// Correct
if (condition)
{
  // code
}
else
{
  // code
}

// Also acceptable
if (condition)
  // single line
else
  // single line

// Incorrect - inconsistent bracing
if (condition)
{
  // code
}
else
  // single line
```

## Classes and Structs

### When to Use Each

* **`struct`**: Use for [POD (Plain Old Data)](https://en.wikipedia.org/wiki/Passive_data_structure) types
* **`class`**: Use for everything else

### Class Layout

Order sections as: `public`, `protected`, `private`

Within each section, order as:

1. Constructor
2. Destructor
3. Operator overloads
4. Functions
5. Variables (static before non-static)

```cpp theme={null}
class ExampleClass : public SomeParent
{
public:
  ExampleClass(int x, int y);
  ~ExampleClass();

  int GetX() const;
  int GetY() const;

protected:
  virtual void SomeProtectedFunction() = 0;
  static float s_some_variable;

private:
  int m_x;
  int m_y;
};
```

### Final and Override Specifiers

Mark classes/functions that shouldn't be inherited/overridden with `final`:

```cpp theme={null}
class ClassName final : ParentClass
{
public:
  void Update() final;  // Cannot be overridden
};
```

Mark overridden functions with `override`:

```cpp theme={null}
class ClassName : ParentClass
{
public:
  void Update() override;  // Overrides parent's Update()
};
```

## Code-Specific Guidelines

### Modern C++ Practices

<Steps>
  <Step title="Use nullptr">
    Use `nullptr` instead of the `NULL` macro:

    ```cpp theme={null}
    int* ptr = nullptr;  // Correct
    int* ptr = NULL;     // Incorrect
    ```
  </Step>

  <Step title="Prefer range-based for loops">
    Use range-based for loops over iterators when possible:

    ```cpp theme={null}
    for (const auto& item : container)
    {
      // process item
    }
    ```
  </Step>

  <Step title="Avoid raw pointers">
    Prefer STL containers and smart pointers:

    ```cpp theme={null}
    std::vector<int> data;           // Instead of int*
    std::unique_ptr<Object> obj;     // For single ownership
    ```

    <Note>
      Raw pointers are acceptable when interfacing with C libraries or when unavoidable.
    </Note>
  </Step>

  <Step title="Limit auto usage">
    Only use `auto` when the type is obvious:

    ```cpp theme={null}
    // Good - type is clear
    auto it = map.begin();
    auto lambda = [](int x) { return x * 2; };

    // Bad - type is not obvious
    auto result = CalculateComplexValue();
    ```
  </Step>
</Steps>

### Header Files

<Steps>
  <Step title="Use #pragma once">
    This project uses `#pragma once` as header guards.
  </Step>

  <Step title="Include order">
    Order includes in source files as:

    1. The header for this source file
    2. Standard library headers (alphabetically)
    3. System-specific headers (in `#ifdef` blocks)
    4. Other Dolphin headers (alphabetically)

    ```cpp theme={null}
    #include "Core/MyClass.h"

    #include <algorithm>
    #include <vector>

    #ifdef _WIN32
    #include <windows.h>
    #endif

    #include "Common/CommonTypes.h"
    #include "Core/ConfigManager.h"
    ```
  </Step>

  <Step title="Relative paths">
    Include project headers relative to `[Dolphin Root]/Source/Core`:

    ```cpp theme={null}
    #include "VideoCommon/RenderBase.h"
    ```
  </Step>

  <Step title="Remove unused includes">
    Remove unnecessary or duplicate includes.
  </Step>
</Steps>

### Loops

```cpp theme={null}
// Infinite loops
while (true)  // Correct
{
}

for (;;)      // Incorrect
{
}

// Empty loops
while (condition) {}  // Correct - use braces
while (condition);    // Incorrect - don't use semicolon

// Do-while loops
do
{
  // code
} while (false);      // 'while' on same line as closing brace

// Prefer prefix increment
for (int i = 0; i < n; ++i)  // Correct
for (int i = 0; i < n; i++)  // Less preferred
```

### Functions

**Const-correctness for parameters:**

```cpp theme={null}
void ProcessData(const int* data, const size_t size);
```

**Pointer parameters for modification:**

Make modification syntactically obvious by using pointers:

```cpp theme={null}
// Correct - obvious that val is modified
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;
}

// Usage makes modification clear
Clamp(&var, 1000, 5000);
```

### Miscellaneous

* **Avoid `goto`** unless you have a really good reason
* **Fix compiler warnings** when found
* **Don't use** `using namespace [x];` in headers (avoid it elsewhere too)
* **Use prefix increment** in for-loops: `++var` instead of `var++`

## clang-format Configuration

The project's `.clang-format` file (located at `Source/.clang-format`) defines:

```yaml theme={null}
ColumnLimit: 100
IndentWidth: 2
PointerAlignment: Left
BreakBeforeBraces: Custom
BraceWrapping:
  AfterClass: true
  AfterControlStatement: true
  AfterEnum: true
  AfterFunction: true
  AfterNamespace: true
  AfterStruct: true
Standard: Latest
InsertNewlineAtEOF: true
```

<Info>
  View the complete configuration at `Source/.clang-format` in the repository.
</Info>

## Android Code Style

### Kotlin

Use the built-in official Kotlin code style in Android Studio.

### Java

Import the Dolphin Java code style:

<Steps>
  <Step title="Open Code Style settings">
    Navigate to Settings → Code Style in Android Studio.
  </Step>

  <Step title="Import scheme">
    Click the gear icon, select **Import Scheme**, and choose `dolphin/Source/Android/code-style-java.xml`.
  </Step>

  <Step title="Format code">
    Select code and press `Ctrl+Alt+L` to automatically format it.
  </Step>
</Steps>

## Next Steps

<CardGroup cols={2}>
  <Card title="Testing" icon="flask" href="/contributing/testing">
    Learn about testing practices and frameworks
  </Card>

  <Card title="Debugging" icon="bug" href="/contributing/debugging">
    Explore debugging tools and techniques
  </Card>
</CardGroup>
