Naming
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 |
| 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 |
| Functions, methods, variables, parameters, public fields, namespaces | snake_case | rmp::ui::current_theme(), 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, 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, RMP_WINDOW_WIDTH |
In code, all of it at once:
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. 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 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 sits next to Texture2D
without looking foreign; the namespace is what says whose is whose.
rmp test naming refuses it: R2constexpr int kMaxLives = 3;FAIL snippet.cpp:1: R2 kMaxLives -- a `kName` constant: constants are CONSTANT_CASE
rmp test naming refuses it: R1int g_score = 0;FAIL snippet.cpp:1: R1 g_score -- a `g_` name: file state is a struct per concern (`frame.open`), examples use plain snake_case
rmp test naming refuses it: R8struct Counter {
int count_ = 0;
};FAIL snippet.cpp:2: R8 count_ -- a name ending in `_`: no trailing underscore, anywhere
rmp lint refuses it: readability-identifier-namingclass player_state {};snippet.cpp:1:7: warning: invalid case style for class 'player_state' [readability-identifier-naming]
1 | class player_state {};
| ^~~~~~~~~~~~
| PlayerStateconstexpr 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).
rmp test naming refuses it: R4class Wallet {
public:
[[nodiscard]] bool richer_than(const Wallet &other) const {
return _coins > other._coins;
}
private:
int _coins = 0;
};FAIL snippet.cpp:4: R4 ._coins -- another object's private member (`x._name`): reach it through a method that says what is taken
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 is the other half of this.
rmp lint refuses it: readability-identifier-namingclass Cannon : public rmp::Object {
public:
void _ready() override { _fire(); }
private:
void _fire() {}
};snippet.cpp:6:10: warning: invalid case style for method '_fire' [readability-identifier-naming]
3 | void _ready() override { _fire(); }
| ~~~~~
| fire
4 |
5 | private:
6 | void _fire() {}
| ^~~~~
| fireclass 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.
rmp lint refuses it: clang-diagnostic-shadowstruct Pass {
int number = 0;
} pass;
void draw(const int pass) { DrawText(TextFormat("pass %d", pass), 10, 10, 20, WHITE); }snippet.cpp:5:21: warning: declaration shadows a variable in the global namespace [clang-diagnostic-shadow]
5 | void draw(const int pass) { DrawText(TextFormat("pass %d", pass), 10, 10, 20, WHITE); }
| ^
snippet.cpp:3:3: note: previous declaration is here
3 | } pass;
| ^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:
rmp test naming refuses it: R6constexpr int MAX = 10;FAIL snippet.cpp:1: R6 MAX is a macro elsewhere -- a constant named like a macro somebody else defines, or starting with RMP_
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:
rmp lint refuses it: modernize-use-std-numbersfloat full_turn() { return 2.0f * 3.14159265f; }snippet.cpp:1:35: warning: prefer 'std::numbers::pi_v<float>' to this literal, differs by '8.74e-08' [modernize-use-std-numbers]
1 | float full_turn() { return 2.0f * 3.14159265f; }
| ^~~~~~~~~~~
| std::numbers::pi_v<float>#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 has the rest.
rmp test naming refuses it: R5#define TILE_SIZE 16FAIL snippet.cpp:1: R5 #define TILE_SIZE -- a macro without the RMP_ prefix
A number does not need a macro at all; a constant does it with a type:
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:
rmp test naming refuses it: R7#if APP_SAVE_PORTABLE
constexpr bool PORTABLE_SAVES = true;
#else
constexpr bool PORTABLE_SAVES = false;
#endifFAIL snippet.cpp:1: R7 APP_SAVE_PORTABLE -- a retired name
#if RMP_SAVE_PORTABLE
constexpr bool PORTABLE_SAVES = true;
#else
constexpr bool PORTABLE_SAVES = false;
#endifA 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.