# Code Style Guidelines Some general rules to write code: Try to follow the same style/format of the file that you are editing (naming, indentation, etc.) or the style of the module (some [submodules](https://github.com/aseprite/aseprite/blob/main/.gitmodules), created by us, or by third-parties, have their own style). There is a [.clang-format](https://github.com/aseprite/aseprite/blob/main/.clang-format) file available but we are not using it at the moment, probably we should start using some [clang-format-diff.py](https://clang.llvm.org/docs/ClangFormat.html#script-for-patch-reformatting) for patches, but this wasn't yet adopted in the development process. There is a [.clang-tidy](https://github.com/aseprite/aseprite/blob/main/.clang-tidy) file used [in the GitHub actions](https://github.com/aseprite/aseprite/blob/main/.github/workflows/clang_tidy.yml) executed on each PR. These rules are adopted progressively on patches because are only executed in the diff, and if some rule is violated a comment by [aseprite-bot](https://github.com/aseprite-bot) is made. ## Basics Basic statements: ```c++ void global_function(int arg1, const int arg2, // You can use "const" preferably const int arg3, ...) { int value; const int constValue = 0; // We prefer to use "var = (condition ? ...: ...)" instead of // "var = condition ? ...: ...;" to make clear about the // ternary operator limits. int conditionalValue1 = (condition ? 1: 2); int conditionalValue2 = (condition ? longVarName: otherLongVarName); // If a condition will return, we prefer the "return" // statement in its own line to avoid missing the "return" // keyword when we read code. if (condition) return; // You can use braces {} if the condition has multiple lines // or the if-body has multiple lines. if (condition1 || condition2) { return; } if (condition) { ... ... ... } // We prefer to avoid whitespaces between "var=initial_value" // or "var alias = orig;`) * Use generic lambda functions * Use `std::shared_ptr`, `std::unique_ptr`, or `base::Ref`, but generally we'd prefer value semantics instead of smart pointers * Use `std::min`/`std::max`/`std::clamp` * Use `std::optional` but taking care of some limitations from macOS 10.9: * Use `std::optional::has_value()` instead of `std::optional::operator bool()` ([example](https://github.com/aseprite/laf/commit/81622fcbb9e4a0edc14a02250c387bd6fa878708)) * Use `std::optional::operator*()` instead of `std::optional::value()` ([example](https://github.com/aseprite/aseprite/commit/4471dab289cdd45762155ce0b16472e95a7f8642)) * Use `std::variant` but taking care of some limitations from macOS 10.9: * Use `T* p = std::get_if(&value)` instead of `T v = std::get(value)` or create an auxiliary `get_value()` using `std::get_if` function ([example](https://github.com/aseprite/aseprite/commit/dc0e57728ae2b10cd8365ff0a50263daa8fcc9ac#diff-a59e14240d83bffc2ea917d7ddd7b2762576b0e9ab49bf823ba1a89c653ff978R98)) * Don't use `std::visit()`, use some alternative with switch-case and the `std::variant::index()` ([example](https://github.com/aseprite/aseprite/commit/574f58375332bb80ce5572fdedb1028617786e45)) * Use `std::any` but taking care of some limitations from macOS 10.9: * Use `T* p = std::any_cast(&value)` instead of `T v = std::any_cast(value)` ([example](https://github.com/aseprite/aseprite/commit/c8d4c60f07df27590381ef28001a40f8f785f50e)) * Use `static constexpr T v = ...;` * You can use ``, ``, ``, and `` * Prefer `using T = ...;` instead of `typedef ... T` * Use `[[fallthrough]]` if needed * Use `= {}` only to specify a default argument value of an user-defined type in a function declaration, e.g. `void func(const std::string& s = {}) { ... }`. In other cases (e.g. a member variable of an user-defined type) it's not required or we prefer to use the explicit value for built-in types (`int m_var = 0;`). * We use gcc 9.2 or clang 9.0 on Linux, so check the features available in https://en.cppreference.com/w/cpp/compiler_support