# Naming

> One spelling for each kind of name in the framework, the reasons each spelling was chosen, each mistake as the tool that refuses it says it, and where each tool looks.

Source: https://omardev29.github.io/rmp-docs/guidelines/naming.html

Every name in the framework is spelled according to what it is, one spelling per kind, with no prefixes. The point is that you can tell what a name is at the place it is used, without going to look at where it was declared: `Align::TOP_LEFT` is a constant of a type, `current_theme()` is a function, `_slot` is somebody's private field. The examples follow the same rules, and a game that follows them reads like the framework it is built on.

## The table

| What | Spelling | Example |
|---|---|---|
| Types: class, struct, enum, alias, template parameter | `PascalCase` | [`rmp::ui::ButtonOptions`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ui.html#ButtonOptions) |
| Enum members | `CONSTANT_CASE` | `rmp::ui::Align::TOP_LEFT` |
| Constants: every `constexpr`, and a `const` at namespace, class or `static` scope | `CONSTANT_CASE` | [`rmp::behavior::MAX_SPAWNED`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-behavior.html#MAX_SPAWNED) |
| Functions, methods, variables, parameters, public fields, namespaces | `snake_case` | [`rmp::ui::current_theme()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ui.html#current_theme), [`gravity_scale`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#gravity_scale) |
| Private and protected data members | `_snake_case` | `_slot`, `_generation` |
| The hooks the framework calls on your type — these eight and no other method | `_snake_case` | `_ready` `_update` `_late_update` `_draw` `_collision` `_end` `_suspend` `_resume` |
| Functions you hand to the framework | `on_snake_case` | [`on_click`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#on_click), `on_ready` |
| File-scope state in the framework's sources and tests | a struct per concern, read as `concern.field` | `loop.quit_requested`, `pool.slots` |
| File-scope state in an example | `snake_case` | `health`, `portrait` |
| The framework's macros, and every value the `.toml` generates | `RMP_` and `CONSTANT_CASE` | [`RMP_GAME`](https://omardev29.github.io/rmp-docs/reference/macros.html#RMP_GAME), [`RMP_WINDOW_WIDTH`](https://omardev29.github.io/rmp-docs/reference/macros.html#toml-RMP_WINDOW_WIDTH) |

In code, all of it at once:

```cpp
namespace layer {
constexpr unsigned PLAYER = 1U << 0;
constexpr unsigned ENEMY = 1U << 1;
} // namespace layer

enum class Weapon { SWORD, BOW };

class Goblin : public rmp::Object {
public:
    int hp = 20;

    void _ready() override {
        collision_layer = layer::ENEMY;
        collision_mask = layer::PLAYER;
    }

    void hit(const int damage) {
        hp -= damage;
        ++_hits_taken;
    }

private:
    int _hits_taken = 0;
    Weapon _weapon = Weapon::SWORD;
};
```

## Who checks it, and where

Not a reviewer. Three tools hold the table, over different parts of the tree, and each mistake below is shown with what its tool prints about it:

| Tool | What it sees | Where it looks |
|---|---|---|
| `readability-identifier-naming`, in the framework's `.clang-tidy`; `rmp lint` | every declaration the compiler parses: types, enum members, constants, functions, variables, members and their underscore, the eight hook names, macros | `src/`, `tests/` and `examples/`, and the headers they include |
| `-Wshadow`, through clang-tidy; `rmp lint` | a local or a parameter that hides a file-state group | the same files |
| `tools/naming_check.sh`; `rmp test naming` | what a compiler cannot: every branch of every `#if`, a name where it is used, a macro named like someone else's, a retired name | `include/`, `src/`, `tests/` and `examples/` |

A game made with `rmp new` gets none of the three: the framework's `.clang-tidy` and `tools/naming_check.sh` stay in the framework, and a game has the first two, which come with that file, only if it [switches the framework's clang-tidy on](https://omardev29.github.io/rmp-docs/guidelines/index.html#switching-on-clang-tidy). The table is still the one to follow, because a game's code sits next to the framework's names in every line it writes. [What checks what](https://omardev29.github.io/rmp-docs/guidelines/enforcement.html) has every rule of these pages in one table.

## The reasons, and the mistakes

### One spelling per kind, and no prefixes

No `k` in front of a constant, no `g_` in front of a global, no trailing underscore. The spelling of the name itself says what it is, so a prefix would be saying it twice. A type and a value never look alike where they are used. raylib's own types are PascalCase too, so [`rmp::Texture`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#Texture) sits next to `Texture2D` without looking foreign; the namespace is what says whose is whose.

Compiles; `rmp test naming` refuses it: `R2`

```cpp
constexpr int kMaxLives = 3;
```

Compiles; `rmp test naming` refuses it: `R1`

```cpp
int g_score = 0;
```

Compiles; `rmp test naming` refuses it: `R8`

```cpp
struct Counter {
    int count_ = 0;
};
```

Compiles; `rmp lint` refuses it: `readability-identifier-naming`

```cpp
class player_state {};
```

```cpp
constexpr int MAX_LIVES = 3;

class PlayerState {
public:
    int count = 0;
};
```

`CONSTANT_CASE` means "known when the program is compiled". `const float half =
width / 2;` inside a function is a variable that happens not to change, and stays `snake_case`.

### An underscore never touches a dot

`foo_.x` and `other._x` are both wrong, in the framework as in a game. A private field belongs to its object, so another object reaches it through a method that says what is taken. In the framework's own code that reads `other.slot()` or `object.take_force()`. The one place `x._name` appears is a call to a hook: the collision pass calling `a._collision(b)`.

Compiles; `rmp test naming` refuses it: `R4`

```cpp
class Wallet {
public:
    [[nodiscard]] bool richer_than(const Wallet &other) const {
        return _coins > other._coins;
    }

private:
    int _coins = 0;
};
```

```cpp
class Wallet {
public:
    [[nodiscard]] int coins() const { return _coins; }
    [[nodiscard]] bool richer_than(const Wallet &other) const {
        return _coins > other.coins();
    }

private:
    int _coins = 0;
};
```

### A leading underscore on a method means the framework calls it

Only the eight hooks wear it, and clang-tidy is told those eight names and refuses any other method that starts with an underscore. So a private field never takes a hook's name, and a helper never looks like a hook. [The access rule](https://omardev29.github.io/rmp-docs/framework/why/the-access-rule.html) is the other half of this.

Compiles; `rmp lint` refuses it: `readability-identifier-naming`

```cpp
class Cannon : public rmp::Object {
public:
    void _ready() override { _fire(); }

private:
    void _fire() {}
};
```

```cpp
class Cannon : public rmp::Object {
public:
    void _ready() override { fire(); }

private:
    void fire() {}
};
```

### File state is grouped, and the dot is the mark

The `g_` prefix existed to make a global visible at every use. Without it, plain names collide: a variable `started` next to a function `started()`, a variable called `index`, `log` or `free` next to a C library that declares those on some platforms and not on others, a parameter `scale` silently hiding the file's `scale`. A struct per concern keeps the mark — `context.started` cannot be anything but file state — and cannot collide with a function. `-Wshadow` catches the local that hides a group. In the examples, where one variable is often the whole story, a plain name reads better and the same `-Wshadow` applies.

Compiles; `rmp lint` refuses it: `clang-diagnostic-shadow`

```cpp
struct Pass {
    int number = 0;
} pass;

void draw(const int pass) { DrawText(TextFormat("pass %d", pass), 10, 10, 20, WHITE); }
```

```cpp
struct ThisPass {
    int number = 0;
} this_pass;

void draw(const int pass) {
    this_pass.number = pass;
    DrawText(TextFormat("pass %d", this_pass.number), 10, 10, 20, WHITE);
}
```

### A constant's name must not be somebody else's macro

The preprocessor runs before the compiler and knows nothing of namespaces. `PI` and `DEG2RAD` are raylib's macros, `EPSILON` is raymath's, `MIN` and `MAX` are defined by the BSDs' system headers, `CHECK` by the test library, `DEBUG` by raymob's debug build. A constant with one of those names is replaced on exactly the platforms that define it: the build is green on Linux and red on NetBSD. This one compiles on Linux, which is the trouble with it:

Compiles; `rmp test naming` refuses it: `R6`

```cpp
constexpr int MAX = 10;
```

So a constant is named after what the number is for — `NEAR_ZERO`, `TOLERANCE`, `MAX_ENEMIES` — and pi is not a constant of yours at all: it is `std::numbers::pi_v<float>`, and clang-tidy refuses pi written out as a number:

Compiles; `rmp lint` refuses it: `modernize-use-std-numbers`

```cpp
float full_turn() { return 2.0f * 3.14159265f; }
```

```cpp
#include <numbers>

constexpr int MAX_ENEMIES = 10;

float full_turn() { return 2.0f * std::numbers::pi_v<float>; }
```

### Macros start with `RMP_`

Never with `__` or an underscore and a capital, which belong to the compiler and the C library. A value from the `.toml` is named after its table and key: `[window]
width` is `RMP_WINDOW_WIDTH`, `[project] name` is `RMP_PROJECT_NAME`. A vendored library's own switches keep their names, in the one file that configures it. [Macros](https://omardev29.github.io/rmp-docs/framework/why/macros.html) has the rest.

Compiles; `rmp test naming` refuses it: `R5`

```cpp
#define TILE_SIZE 16
```

A number does not need a macro at all; a constant does it with a type:

```cpp
constexpr int TILE_SIZE = 16;
```

### A retired name is not an error, it is 0

A renamed macro left behind in an `#if` is not an error: the preprocessor reads an undefined name as 0. An old spelling of the portable-saves switch in an `#if` would have switched portable saves off, on Windows only, with nothing saying so. The compiler has nothing to say about the block below; `tools/naming_check.sh` knows the names that are gone:

Compiles; `rmp test naming` refuses it: `R7`

```cpp
#if APP_SAVE_PORTABLE
constexpr bool PORTABLE_SAVES = true;
#else
constexpr bool PORTABLE_SAVES = false;
#endif
```

```cpp
#if RMP_SAVE_PORTABLE
constexpr bool PORTABLE_SAVES = true;
#else
constexpr bool PORTABLE_SAVES = false;
#endif
```

### A name in a comment is spelled like the code

So a search for a name finds its explanations too, and a rename renames the comments with it. No tool checks a comment; this one is for whoever reviews the change.

## What a compiler cannot see

clang-tidy reads what the compiler parses, and the compiler parses one branch of every `#if`: on a Linux machine, the web, iOS, Android and Windows code is not there for it. It sees a name where it is declared, not where it is used, so `other._coins` is not its business. And it says nothing about a macro named like someone else's. That is what `tools/naming_check.sh` is for: it blanks the comments and the strings, reads the text of every branch, and holds rules R1 to R8 over all of it.

The last rule, R8, is there because clang-tidy does not name a declaration a macro expands: a variable declared inside a macro's body is invisible to it, however it is spelled. The script reads the text, so it sees the name in the macro's definition.
