# Google's guide, and where this one differs

> Every place the framework's rules and Google's C++ Style Guide part ways or pull against each other, with the reason the framework's rule is what it is and what holds the code to it — and where the two agree.

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

[Google's C++ Style Guide](https://google.github.io/styleguide/cppguide.html) decides every question the framework's own rules leave open. Where the framework has decided otherwise, the decision was made for a reason, and this page lists each one: what Google says, what the framework does instead, why, and what holds the code to it. Where the two agree and a tool checks it, that is at the end.

Above both sits the rule that outranks the others: the API is as simple as possible for whoever uses it, and a main menu is [three functions](https://omardev29.github.io/rmp-docs/framework/why/the-three-function-menu.html). More than one difference below comes from there.

## Where the framework's rule wins

### Naming

Google spells functions `PascalCase`, constants and enumerators `kName`, and a class's data members with a trailing underscore, `table_`. The framework spells functions `snake_case`, constants and enumerators `CONSTANT_CASE`, and private members with a leading one, `_table`. One spelling per kind of name, so a type, a value and a constant never look alike where they are used — and raylib's types are `PascalCase` too, so the framework's sit beside `Texture2D` and `Vector2` without looking foreign. [Naming](https://omardev29.github.io/rmp-docs/guidelines/naming.html) has the whole table.

Google moved its enumerators from `ENUM_NAME` to `kEnumName` because the upper-case names collided with macros. The framework keeps the upper case and answers the collision with a check instead: rule R6 of `tools/naming_check.sh` refuses a constant named like a macro of the vendored headers, or like one of the C library's and the BSDs' that are known to collide.

The private member's underscore is at the front so that it never touches a dot or an arrow: `other._table` is refused (rule R4), and so is `table_.size()` (rule R3). Checked by `readability-identifier-naming` and `tools/naming_check.sh`, in the framework and the examples.

### #pragma once

Google asks for `#define` guards named after the file's path. A header here starts with `#pragma once` instead: one line that cannot be misspelt, copied into the wrong file or collide with another guard, and every compiler the framework builds with supports it. `portability-avoid-pragma-once`, which would refuse it, is switched off. Nothing checks that a header has it; a header without it is a question for review, and the framework has one: `tests/smoke_test.h`, which the plain C example includes too, keeps a guard.

### Forward declarations between the framework's own headers

Google: avoid forward declarations, include the header you need. Its decision makes one exception, for entities your project owns, when a forward declaration is the better way to fix a slowdown. That is the case here, and it is measured rather than assumed: the framework's headers forward-declare each other instead of including, a header that cannot — `rmp/scene.h` holds an `rmp::Tilemap` by value — says why at its top, and `tools/header_budget.txt` holds what each one costs to include. [One header per module](https://omardev29.github.io/rmp-docs/framework/why/one-header-per-module.html) has the numbers. A header from outside the framework — the standard library's, raylib's — is included, as Google says.

### Formatting

Google indents by 2 and stops at 80 columns; the framework indents by 4 and stops at 90. Google writes `char* c`; the framework writes `char *c`, as raylib does. Google indents `public:` by one space; the framework puts it at the class's own indentation. Google writes a braced list as `{1, 2}`; the framework as `{ 1, 2 }`, so the theme's colour literals keep their spaces. Google sorts includes into five groups, alphabetically; the framework orders them by hand, each with a comment saying what it is for. Both let a brief `if` go without braces; the framework writes every guard that way, `if (!ready)
return;`, and switches off `readability-braces-around-statements`, which would refuse it. All of it is `.clang-format`, which `rmp fmt` applies; [Formatting](https://omardev29.github.io/rmp-docs/guidelines/formatting.html) has each setting.

### .cpp

Google names a source file `.cc`. The framework's are `.cpp`, and so are a game's, from the `src/main.cpp` that `rmp new` writes.

### const and constexpr, further than Google

Google recommends `const` in APIs and says `const` on a local variable is "neither encouraged nor discouraged". The framework requires it on every local that does not change, so the value at the bottom of a function is known, at its declaration, to be the one at the top. On a by-value parameter the two agree that `const` stays out of the declaration, where it means nothing to the caller. Google's guide says nothing of the definition; the framework puts it there. Checked by `misc-const-correctness`, in the framework and the examples. [const](https://omardev29.github.io/rmp-docs/guidelines/const.html) has the rest.

Google also warns against making a function harder to read so that it can be `constexpr`. The framework asks for `constexpr`, `consteval` and `constinit` wherever they apply. The two pull against each other only where making a function `constexpr` would make it worse to read, and no tool checks either: it is a question for review.

### No raw pointer, even where Google uses one

Google uses a pointer to say "this may be absent": a `const T *` for an optional input, a `T *` for an optional output, a returned pointer when the result may be null. The framework's rule allows no raw pointer outside the few places [Pointers and RAII](https://omardev29.github.io/rmp-docs/guidelines/pointers-and-raii.html#allowed) names, most of them the C libraries it is built on: a result that may be missing is a `std::optional`, an optional input is a second overload, a reference that may be empty is an [`rmp::Ref`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Ref.html), and an object kept across frames is an [`rmp::Handle`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Handle.html). A pointer asks its reader whether it may be null, who owns it and how long it lives, and its type answers none of the three. Checked by `custom-rmp-raw-pointer` in the framework and the examples, and by `tools/pointer_check.py` on every declaration a game can name. [Pointers and RAII](https://omardev29.github.io/rmp-docs/guidelines/pointers-and-raii.html) has the replacements.

## Deviations, recorded

Each of these is a rule of Google's that the framework breaks on purpose, in a known place.

### <filesystem>

Google does not allow `<filesystem>`. The framework uses it in two files: `src/rmp/app/app.cpp`, to find the folder the executable is in, and `src/rmp/save/save.cpp`, for the save folder and its files, and on Windows to turn the wide paths the system hands back into UTF-8 and back. The standard library does both on every platform the framework ships to, which nothing else the framework has would.

It has one cost a game sees: Apple's C++ library has it only from iOS 13, so `tools/configure.py` refuses an `[ios] deployment_target` below that. No tool keeps it to those two files: a use anywhere else is a question for review.

### -std=gnu++20

Google's guide asks for C++20 and no nonstandard extensions. The framework asks CMake for C++20 and leaves CMake's default for extensions alone, so GCC and Clang compile with `-std=gnu++20`. Two builds use the standard without GNU's extensions: the iOS project, which `tools/configure.py` writes, asks for `c++20`, and MSVC, which builds `windows-arm64`, has no GNU mode at all. The code compiles under both.

### Two variadic functions

The framework's rule is no C variadic function ([Modern C++](https://omardev29.github.io/rmp-docs/guidelines/modern-cpp.html#varargs)), and it has two, side by side: the functions behind its said-once warnings, which hand a `printf` format to raylib's `TraceLog()`, one of them with a key. Each is marked with a `format(printf)` attribute through a macro of the framework's internal header, so GCC and Clang check every format against its values, as an error. That is the form in which Google allows a nonstandard extension: a portability wrapper, defined once, in a header the framework's sources share.

### Statics that are not trivially destructible

Google allows an object with static storage duration only when its destructor does nothing, because destructors at exit run in an order nobody chooses. The framework's file-scope state is a struct per concern, and some of those hold vectors and strings. What they hold is released before the window closes, in an order written down in `src/rmp/app/app.cpp` — scenes, then globals, then the interface's font, then sounds, then everything left in the resource table — rather than by destructors at exit.

### Public data members

Google makes a class's data members private, and keeps `struct` for passive data. [`rmp::Object`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html) is a class whose [`position`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#position), [`velocity`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#velocity) and [`shape`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#shape) are public fields, and every options struct is public data. `ball.position.x += 4;` is the simplest way to move a ball, and a setter would make it longer for nothing: `misc-non-private-member-variables-in-classes` is switched off for that reason.

### Implicit conversions

Google requires `explicit` on every one-argument constructor and conversion operator, and grants a waiver for types designed to be interchangeable. The framework's API has a few such conversions, each because the simple call would be longer without it, and each with a `NOLINT` for `google-explicit-constructor` and its reason beside it:

- a `bool`, a number or a piece of text becomes an [`rmp::Value`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Value.html): `save["score"] = 3;`;
- a `Value::Ref`, what `save["score"]` gives, reads as the `Value` it names;
- an asset's handle — `rmp::Texture`, `rmp::Font`, `rmp::Sound` and the rest — passes where raylib takes the plain struct, `DrawTexture(logo, 0, 0, WHITE)`, from a named handle only;
- a lambda becomes an [`rmp::Callback`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Callback.html), so it can be written straight into a behavior's options;
- a number or a [`rmp::ui::Size`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ui.html#Size) becomes the size of a control: `.size = 34` and `.size = rmp::ui::Size::LARGE` both read as what they mean.

A new conversion in the API is refused by `google-explicit-constructor` until it is given the same: a `NOLINT` and a reason someone reviewing it can accept.

### No TODO, with or without a name

Google asks for a TODO with a name in brackets after it, so that it can be traced to someone. The framework has no TODO at all: a comment says what is, and what is to be done goes in the plan, where somebody reads it. Checked by rule S3 of `tools/style_check.sh`, which also refuses `FIXME` and `XXX`, in the framework and the examples.

## Where the two read differently

### The top of a file

Google starts every file with its licence. The framework's licence is the repository's `LICENSE`, and the vendored libraries' are in `THIRD_PARTY_LICENSES.md`; a file starts with what it is for, and why it is built the way it is.

### Long functions

Google suggests thinking about splitting a function past about 40 lines, and sets no limit. The framework sets none either: `readability-function-cognitive-complexity` is switched off, because a render switch is one function on purpose.

### shared_ptr inside a Callback

Google says not to design for shared ownership without a very good reason. A [`rmp::Callback`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Callback.html) keeps the lambda it was given in a `std::shared_ptr<void>`, and its ownership is still single — a `Callback` cannot be copied. The `shared_ptr` is there because it carries the lambda's deleter inside it, which erases the lambda's type with no `delete` of the framework's and without `<functional>`. [Ownership](https://omardev29.github.io/rmp-docs/framework/why/ownership.html#callbacks) has why not `std::function`.

### Casts

Google's first choice for converting one arithmetic type to another is braces, `std::int64_t{ x }`, which refuse to compile a conversion that loses anything; the framework writes `static_cast`, and braces where they apply. Google's advice for reading one type's bits as another is `std::bit_cast`, which the oldest compiler a game is built with does not have: copying the bytes with `std::memcpy` does the same, and compiles there. [Modern C++](https://omardev29.github.io/rmp-docs/guidelines/modern-cpp.html#casts) has both.

### Conditional compilation

Google's advice on using a macro to compile code conditionally is not to. A framework built from one source for every platform cannot avoid it, and it is one of the four uses of a macro the framework allows. What it costs is that a compiler, and clang-tidy, read only the branch of the machine they run on, so the framework's text gates — `tools/naming_check.sh` and `tools/style_check.sh` — read every branch.

### A macro in a header

Google: no macro defined in a header, and none used to declare a piece of an API, without a waiver. `rmp/app.h` defines [`RMP_GAME`](https://omardev29.github.io/rmp-docs/reference/macros.html#RMP_GAME), the line that starts a game, because a program's entry point is a different set of functions on each platform and only the preprocessor can choose among them. It is named, with the framework's few other macros, in `cppcoreguidelines-macro-usage`'s list of allowed ones, and a new one is a question for review. [Macros](https://omardev29.github.io/rmp-docs/guidelines/macros.html) has the rule.

## Where they agree, and what checks it

| Google's rule | Checked by |
|---|---|
| No exceptions: the framework throws none, and catches the standard library's where it can throw ([No exceptions](https://omardev29.github.io/rmp-docs/framework/why/no-exceptions.html)) | a `throw` under `src/rmp/` or `include/rmp/`: `tests/campaign_test.py`, in `rmp test property`; the catching: review |
| C++ casts, not C's | `modernize-avoid-c-style-cast`, and S1 of `tools/style_check.sh` |
| `nullptr`, not `NULL` or `0` | `modernize-use-nullptr`, and S2 |
| `++i` unless the old value is used | S6 |
| No user-defined literals | S5 |
| `int`, and `<cstdint>`'s fixed widths | `google-runtime-int` |
| `float` and `double`, never `long double` | `google-runtime-float` |
| `explicit` conversions | `google-explicit-constructor` |
| No default argument on a virtual function | `google-default-arguments` |
| No using-directive | `google-build-using-namespace` |
| No unnamed namespace in a header | `google-build-namespaces` |
| No using-declaration at global scope in a header | `google-global-names-in-headers` |
| No overloaded unary `&` | `google-runtime-operator` |
| `std::make_pair` deduces its types | `google-build-explicit-make-pair` |
| A namespace constant in a header has no internal linkage: it is `inline constexpr` | `custom-rmp-header-constant` |
| Single owners; `std::unique_ptr` for what moves | `cppcoreguidelines-owning-memory`, `cppcoreguidelines-no-malloc`, and O1 of `tools/ownership_check.sh` |
| Designated initialisers only in C++20's order | the compiler, with `-Werror=reorder-init-list` under Clang |
| Type deduction where it makes the code clearer | `modernize-use-auto`, for a cast, in the framework; review for the rest |
| RTTI in tests only: nothing in the framework uses `dynamic_cast` or `typeid` | review |
| No modules and no coroutines | review |

"S1", "S2", "S5" and "S6" are rules of `tools/style_check.sh`, the `style` stage of `rmp test`. Every check in the table runs in the framework, and all but `modernize-use-auto`, O1 and the test that looks for a `throw` in the examples too; [what checks what](https://omardev29.github.io/rmp-docs/guidelines/enforcement.html) has each one's exact reach.
