Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1# Developer Notes3## Coding Style (General)5Various coding styles have been used during the history of the codebase,6and the result is not very consistent. However, we're now trying to converge to7a single style, which is specified below. When writing patches, favor the new8style over attempting to mimic the surrounding style, except for move-only9commits.11Do not submit patches solely to modify the style of existing code.13## Coding Style (C++)15- **Indentation and whitespace rules** as specified in16[src/.clang-format](/src/.clang-format). You can use the provided17[clang-format-diff script](/contrib/devtools/README.md#clang-format-diffpy)18tool to clean up patches automatically before submission.19- Braces on new lines for classes, functions, methods.20- Braces on the same line for everything else (including structs).21- 4 space indentation (no tabs) for every block except namespaces.22- No indentation for `public`/`protected`/`private` or for `namespace`.23- No extra spaces inside parentheses; don't do `( this )`.24- No space after function names; one space after `if`, `for` and `while`.25- If an `if` only has a single-statement `then`-clause, it can appear26on the same line as the `if`, without braces. In every other case,27braces are required, and the `then` and `else` clauses must appear28correctly indented on a new line.29- There's no hard limit on line width, but prefer to keep lines to <10030characters if doing so does not decrease readability. Break up long31function declarations over multiple lines using the Clang Format32[AlignAfterOpenBracket](https://clang.llvm.org/docs/ClangFormatStyleOptions.html)33style option.35- **Symbol naming conventions**. These are preferred in new code, but are not36required when doing so would need changes to significant pieces of existing37code.38- Variable (including function arguments) and namespace names are all lowercase and may use `_` to39separate words (snake_case).40- Class member variables have a `m_` prefix.41- Global variables have a `g_` prefix.42- Constant names are all uppercase, and use `_` to separate words.43- Enumerator constants may be `snake_case`, or `PascalCase`. They should not44be `ALL_CAPS`, according to the [C++ Core45Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#renum-caps),46to avoid clashing with macros.47- Class names, function names, and method names are UpperCamelCase48(PascalCase). Do not prefix class names with `C`. See [Internal interface49naming style](#internal-interface-naming-style) for an exception to this50convention.52- Test suite naming convention: The Boost test suite in file53`src/test/foo_tests.cpp` should be named `foo_tests`. Test suite names54must be unique.56- **Miscellaneous**57- `++i` is preferred over `i++`.58- `static_assert` is preferred over `assert` where possible. Generally; compile-time checking is preferred over run-time checking.59- Use a named cast or functional cast, not a C-Style cast. When casting60between integer types, use functional casts such as `int(x)` or `int{x}`61instead of `(int) x`. When casting between more complex types, use `static_cast`.62Use `reinterpret_cast` and `const_cast` as appropriate.63- Prefer [`list initialization ({})`](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#res-list) where possible.64For example `int x{0};` instead of `int x = 0;` or `int x(0);`66For function calls a namespace should be specified explicitly, unless such functions have been declared within it.67Otherwise, [argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl), also known as ADL, could be68triggered that makes code harder to maintain and reason about:69```c++70#include <filesystem>72namespace fs {73class path : public std::filesystem::path74{75};76// The intention is to disallow this function.77bool exists(const fs::path& p) = delete;78} // namespace fs80int main()81{82//fs::path p; // error83std::filesystem::path p; // compiled84exists(p); // ADL being used for unqualified name lookup85}86```88Block style example:89```c++90int g_count{0};92namespace foo {93class Class94{95std::string m_name;97public:98bool Function(const std::string& s, int n)99{100// Comment summarising what this section of code does101for (int i = 0; i < n; ++i) {102int total_sum{0};103// When something fails, return early104if (!Something()) return false;105...106if (SomethingElse(i)) {107total_sum += ComputeSomething(g_count);108} else {109DoSomething(m_name, total_sum);110}111}113// Success return is usually at the end114return true;115}116}117} // namespace foo118```120### Coding Style (C++ functions and methods)122- When ordering function parameters, place input parameters first, then any123in-out parameters, followed by any output parameters.125- *Rationale*: API consistency.127- Prefer returning values directly to using in-out or output parameters. Use128`std::optional` where helpful for returning values.130- *Rationale*: Less error-prone (no need for assumptions about what the output131is initialized to on failure), easier to read, and often the same or better132performance.134- Generally, use `std::optional` to represent optional by-value inputs (and135instead of a magic default value, if there is no real default). Non-optional136input parameters should usually be values or const references, while137non-optional in-out and output parameters should usually be references, as138they cannot be null.140### Coding Style (clang-tidy rules)142The clang-tidy tool is used to check some rules. Please refer to the [upstream143documentation](https://clang.llvm.org/extra/clang-tidy/checks/list.html) about144the details and rationale for each rule.146#### C++ Recursive Functions148Recursion is checked by clang-tidy and thus must be made explicit. Use149`NOLINTNEXTLINE(misc-no-recursion)` to suppress the check.151#### C++ named arguments153When passing named arguments, use a format that clang-tidy understands. The154argument names can otherwise not be verified by clang-tidy.156For example:158```c++159void function(Addrman& addrman, bool clear);161int main()162{163function(g_addrman, /*clear=*/false);164}165```167#### Running clang-tidy169To run clang-tidy on Ubuntu/Debian, install the dependencies:171```sh172apt install clang-tidy clang173```175Configure with clang as the compiler with the below command that should create a `compile_commands.json` file within the build directory:177```sh178cmake -B build -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON179```181The output is denoised of errors from external dependencies.183To run clang-tidy on all source files using the checks mentioned in the `./src/.clang-tidy` file:185```sh186( cd ./src/ && run-clang-tidy -p ../build -j $(nproc) )187```189To run clang-tidy on one file:190```sh191( cd ./src/ && run-clang-tidy -p ../build -j $(nproc) ./path/to/single_file.cpp )192```194Optionally, append the `run-clang-tidy` command with the `-quiet` option to suppress printing of statistics and ignored warnings that can clutter the output. The `-fix` option also comes in handy to apply the fixes suggested by the tool but need to ensure that unrelated changes in the file are not committed.196To run clang-tidy on the changed source lines:198```sh199git diff | ( cd ./src/ && clang-tidy-diff -p2 -path ../build -j $(nproc) )200```202## Coding Style (Doxygen-compatible comments)204Bitcoin Core uses [Doxygen](https://www.doxygen.nl/) to generate its official documentation.206Use Doxygen-compatible comment blocks for functions, methods, and fields.208For example, to describe a function use:210```c++211/**212* ... Description ...213*214* @param[in] arg1 input description...215* @param[in] arg2 input description...216* @param[out] arg3 output description...217* @return Return cases...218* @throws Error type and cases...219* @pre Pre-condition for function...220* @post Post-condition for function...221*/222bool function(int arg1, const char *arg2, std::string& arg3)223```225A complete list of `@xxx` commands can be found at https://www.doxygen.nl/manual/commands.html.226As Doxygen recognizes the comments by the delimiters (`/**` and `*/` in this case), you don't227*need* to provide any commands for a comment to be valid; just a description text is fine.229To describe a class, use the same construct above the class definition:230```c++231/**232* Alerts are for notifying old versions if they become too obsolete and233* need to upgrade. The message is displayed in the status bar.234* @see GetWarnings()235*/236class CAlert237```239To describe a member or variable, place the comment on the line(s) before it, using `/**` and `*/`, `//!`, or `///`:240```c++241//! Description before the member242int var;243```245Avoid trailing (inline) member comments like `int var; //!< Description after the member`.247- *Rationale*: Forgetting the `<` silently breaks Doxygen output.249Also OK:250```c++
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.