Artem Titov | a617867 | 2023-01-30 10:51:01 | [diff] [blame] | 1 | <!-- go/cmark --> |
| 2 | <!--* freshness: {owner: 'hta' reviewed: '2021-01-01'} *--> |
| 3 | |
Karl Wiberg | ab56670 | 2019-01-31 01:34:39 | [diff] [blame] | 4 | # How to write code in the `api/` directory |
| 5 | |
Artem Titov | a617867 | 2023-01-30 10:51:01 | [diff] [blame] | 6 | Mostly, just follow the regular [style guide](/g3doc/style-guide.md), but: |
Karl Wiberg | ab56670 | 2019-01-31 01:34:39 | [diff] [blame] | 7 | |
| 8 | * Note that `api/` code is not exempt from the “`.h` and `.cc` files come in |
| 9 | pairs” rule, so if you declare something in `api/path/to/foo.h`, it should be |
| 10 | defined in `api/path/to/foo.cc`. |
| 11 | * Headers in `api/` should, if possible, not `#include` headers outside `api/`. |
| 12 | It’s not always possible to avoid this, but be aware that it adds to a small |
| 13 | mountain of technical debt that we’re trying to shrink. |
| 14 | * `.cc` files in `api/`, on the other hand, are free to `#include` headers |
| 15 | outside `api/`. |
| 16 | |
| 17 | That is, the preferred way for `api/` code to access non-`api/` code is to call |
| 18 | it from a `.cc` file, so that users of our API headers won’t transitively |
| 19 | `#include` non-public headers. |
| 20 | |
| 21 | For headers in `api/` that need to refer to non-public types, forward |
| 22 | declarations are often a lesser evil than including non-public header files. The |
Artem Titov | a617867 | 2023-01-30 10:51:01 | [diff] [blame] | 23 | usual [rules](/g3doc/style-guide.md#forward-declarations) still apply, though. |
Karl Wiberg | ab56670 | 2019-01-31 01:34:39 | [diff] [blame] | 24 | |
| 25 | `.cc` files in `api/` should preferably be kept reasonably small. If a |
| 26 | substantial implementation is needed, consider putting it with our non-public |
| 27 | code, and just call it from the `api/` `.cc` file. |