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.In case of conflicts between this guide and clang-format rules, follow clang-format.
Setting Up clang-format
Automated Formatting
- Command Line
- Pre-commit Hook
- Git Filter Driver
- Visual Studio
Format all staged files before committing:
Line Endings
General Formatting Rules
Line Length and Indentation
- Maximum line length: 100 characters
- Indentation: 2 spaces per level (no tabs)
- Tab width: 2 spaces
Try to keep lines under 80-90 characters when possible for better readability.
Brace Placement
Opening braces go on the next line for:- Namespaces
- Classes
- Functions
- Enums, structs, unions
- Conditionals and loops
Pointers and References
Place the* or & against the type name, not the variable name:
Comments
Use single-line comments (//), not multi-line comments (/* */):
Conditionals and Loops
Don’t collapse single-line bodies onto the same line as the header:Naming Conventions
Classes, Enums, Functions, Structs
Use UpperCamelCase. Uppercase abbreviations:Constants
Fully uppercase with underscores:Variables
Lowercase with underscores:Variable Prefixes
Do not use Hungarian notation except for these specific prefixes:
Conditional Statements
Don’t leaveelse or else if dangling unless the if lacks braces:
Classes and Structs
When to Use Each
struct: Use for POD (Plain Old Data) typesclass: Use for everything else
Class Layout
Order sections as:public, protected, private
Within each section, order as:
- Constructor
- Destructor
- Operator overloads
- Functions
- Variables (static before non-static)
Final and Override Specifiers
Mark classes/functions that shouldn’t be inherited/overridden withfinal:
override:
Code-Specific Guidelines
Modern C++ Practices
1
Use nullptr
Use
nullptr instead of the NULL macro:2
Prefer range-based for loops
Use range-based for loops over iterators when possible:
3
Avoid raw pointers
Prefer STL containers and smart pointers:
Raw pointers are acceptable when interfacing with C libraries or when unavoidable.
4
Limit auto usage
Only use
auto when the type is obvious:Header Files
1
Use #pragma once
This project uses
#pragma once as header guards.2
Include order
Order includes in source files as:
- The header for this source file
- Standard library headers (alphabetically)
- System-specific headers (in
#ifdefblocks) - Other Dolphin headers (alphabetically)
3
Relative paths
Include project headers relative to
[Dolphin Root]/Source/Core:4
Remove unused includes
Remove unnecessary or duplicate includes.
Loops
Functions
Const-correctness for parameters:Miscellaneous
- Avoid
gotounless 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:
++varinstead ofvar++
clang-format Configuration
The project’s.clang-format file (located at Source/.clang-format) defines:
View the complete configuration at
Source/.clang-format in the repository.Android Code Style
Kotlin
Use the built-in official Kotlin code style in Android Studio.Java
Import the Dolphin Java code style:1
Open Code Style settings
Navigate to Settings → Code Style in Android Studio.
2
Import scheme
Click the gear icon, select Import Scheme, and choose
dolphin/Source/Android/code-style-java.xml.3
Format code
Select code and press
Ctrl+Alt+L to automatically format it.Next Steps
Testing
Learn about testing practices and frameworks
Debugging
Explore debugging tools and techniques