2018-06-14 04:22:46 +08:00
# Code Style Guidelines
2018-06-02 03:24:46 +08:00
2024-08-09 03:17:56 +08:00
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
2024-12-18 19:35:20 +08:00
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.
## clang-format
2024-08-09 03:17:56 +08:00
There is a [.clang-format ](https://github.com/aseprite/aseprite/blob/main/.clang-format )
2024-12-18 19:35:20 +08:00
file available for Aseprite and laf, and we are using it with
Clang 19. You have to configure a [pre-commit ](../CONTRIBUTING.md#pre-commit-hooks )
hook which will help you to do the formatting automatically before committing.
2024-08-09 03:17:56 +08:00
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
2024-12-18 19:35:20 +08:00
comment by [aseprite-bot ](https://github.com/aseprite-bot ) is
made. (Sometimes the bot will be wrong, so be careful.)
## Column limit
We use a [column limit ](https://clang.llvm.org/docs/ClangFormatStyleOptions.html#columnlimit )
of 100. clang-format will break lines to avoid excessing more than 100
lines, but in some extreme cases it might not break this limit, as
our [PenaltyExcessCharacter ](https://clang.llvm.org/docs/ClangFormatStyleOptions.html#penaltyexcesscharacter )
is not the highest value.
2024-08-09 03:17:56 +08:00
2018-06-02 03:24:46 +08:00
## Basics
Basic statements:
```c++
2024-12-18 19:35:20 +08:00
void function_with_long_args(const int argument1,
const int argument2,
const std::string& argument3,
const double argument4,
...)
2018-06-02 03:24:46 +08:00
{
2024-12-18 19:35:20 +08:00
}
2018-06-02 03:24:46 +08:00
2024-12-18 19:35:20 +08:00
void function_with_short_args(int arg1, const int arg2, const int arg3, ...)
{
const int constValue = 0;
int value;
2021-05-05 05:32:38 +08:00
// 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.
2024-12-18 19:35:20 +08:00
if (condition1 || condition2) {
2024-12-18 19:41:51 +08:00
...
2021-05-05 05:32:38 +08:00
return;
}
2018-06-02 03:24:46 +08:00
if (condition) {
...
}
2024-12-18 19:35:20 +08:00
for (int i = 0; i < 10 ; + + i ) {
2021-05-05 05:32:38 +08:00
// Same case as in if-return.
if (condition)
break;
2024-12-18 19:35:20 +08:00
2021-05-05 05:32:38 +08:00
...
2018-06-02 03:24:46 +08:00
}
while (condition) {
...
}
do {
...
2021-01-14 23:45:32 +08:00
} while (condition);
2018-06-02 03:24:46 +08:00
switch (condition) {
2024-12-18 19:35:20 +08:00
case 1: ... break;
2018-06-02 03:24:46 +08:00
case 2: {
int varInsideCase;
2024-12-18 19:35:20 +08:00
// ...
2018-06-02 03:24:46 +08:00
break;
}
2024-12-18 19:35:20 +08:00
default: break;
2018-06-02 03:24:46 +08:00
}
}
```
## Namespaces
Define namespaces with lower case:
```c++
namespace app {
2024-12-18 19:35:20 +08:00
...
2018-06-02 03:24:46 +08:00
} // namespace app
```
## Classes
Define classes with `CapitalCase` and member functions with `camelCase` :
```c++
class ClassName {
public:
2024-12-18 19:35:20 +08:00
ClassName() : m_memberVarA(1), m_memberVarB(2), m_memberVarC(3) {}
ClassName(int a, int b, int c, int d)
: m_memberVarA(a)
, m_memberVarB(b)
, m_memberVarC(c)
, m_memberVarD(d)
{
// ...
2021-05-05 05:32:38 +08:00
}
2018-06-02 03:24:46 +08:00
virtual ~ClassName();
2021-05-05 05:32:38 +08:00
// We can return in the same line for getter-like functions
2018-06-02 03:24:46 +08:00
int memberVar() const { return m_memberVar; }
void setMemberVar();
protected:
2024-12-18 19:35:20 +08:00
virtual void onEvent1() {} // Do nothing functions can be defined as "{}"
2021-05-05 05:32:38 +08:00
virtual void onEvent2() = 0;
2018-06-02 03:24:46 +08:00
private:
2021-05-05 05:32:38 +08:00
int m_memberVarA;
int m_memberVarB;
int m_memberVarC;
int m_memberVarD = 4; // We can initialize variables here too
};
class Special : public ClassName {
public:
Special();
protected:
2024-12-18 19:35:20 +08:00
void onEvent2() override
{
// No need to repeat virtual in overridden methods
2021-05-05 05:32:38 +08:00
...
}
2018-06-02 03:24:46 +08:00
};
```
2021-08-28 04:42:44 +08:00
## Const
2024-08-09 03:17:56 +08:00
We use the const-west notation:
2021-08-28 04:42:44 +08:00
* [NL.26: Use conventional const notation ](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#nl26-use-conventional-const-notation )
2024-08-09 03:17:56 +08:00
There is a problem with `clang-tidy` that will make comments using
2024-12-18 19:35:20 +08:00
East const notation:
[#4361 ](https://github.com/aseprite/aseprite/issues/4361 ), but
clang-format should fix the `const` position anyway.
2024-08-09 03:17:56 +08:00
2022-06-10 05:28:06 +08:00
## C++17
2018-06-02 03:24:46 +08:00
2023-01-05 21:21:50 +08:00
We are using C++17 standard. Some things cannot be used because we're
targetting macOS 10.9, some notes are added about this:
2018-06-02 03:24:46 +08:00
* Use `nullptr` instead of `NULL` macro
2024-08-09 03:17:56 +08:00
* Use `auto` /`auto*` for complex types/pointers, iterators, or when
the variable type is obvious (e.g. `auto* s = new Sprite;` )
2018-06-02 03:24:46 +08:00
* Use range-based for loops (`for (const auto& item : values) { ... }`)
* Use template alias (`template< typename T > alias = orig< T > ;`)
2023-01-05 21:21:50 +08:00
* Use generic lambda functions
2024-08-09 03:17:56 +08:00
* 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`
2023-01-05 21:21:50 +08:00
* 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<T>(&value)` instead of `T v = std::get<T>(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<T>(&value)` instead of `T v = std::any_cast<T>(value)` ([example](https://github.com/aseprite/aseprite/commit/c8d4c60f07df27590381ef28001a40f8f785f50e))
2020-04-24 23:07:52 +08:00
* Use `static constexpr T v = ...;`
2018-06-02 03:24:46 +08:00
* You can use `<atomic>` , `<thread>` , `<mutex>` , and `<condition_variable>`
2020-07-08 06:06:48 +08:00
* Prefer `using T = ...;` instead of `typedef ... T`
2022-06-30 07:14:03 +08:00
* Use `[[fallthrough]]` if needed
2024-07-20 04:10:39 +08:00
* 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;`).
2020-04-24 23:07:52 +08:00
* We use gcc 9.2 or clang 9.0 on Linux, so check the features available in
2021-05-29 21:59:06 +08:00
https://en.cppreference.com/w/cpp/compiler_support