Add `STYLE` guide

c86fbbb141e09437ecbe8bd1e2a9b18a1bedeee6
Alexis Sellier committed ago 1 parent 061ce9b9
AGENTS.md +1 -0
1 1
# AGENTS
2 2
3 +
Before implementing a code change, check the `STYLE` guide.
3 4
After you're done implementing a code or test change:
4 5
5 6
* ALWAYS run `cargo fmt`
6 7
* ALWAYS run `cargo clippy --all-targets --all-features`
7 8
* ALWAYS run `cargo test --all-targets --all-features`
STYLE added +81 -0
1 +
=====
2 +
STYLE
3 +
=====
4 +
5 +
Coding style guide for the Radiance language.
6 +
7 +
BOOLEANS
8 +
9 +
* Use small enums for mode, role, and state choices instead of bare booleans.
10 +
11 +
MACROS
12 +
13 +
* Avoid creating new macros.
14 +
* Avoid using third-party macros.
15 +
16 +
CALLSITES
17 +
18 +
* Avoid boolean parameters when the meaning is not obvious at the callsite.
19 +
* Avoid `Option` parameters for the same reason.
20 +
* Use option structs when a function takes multiple configuration values, when that helps with clarity.
21 +
* Keep callsites readable without needing to inspect the callee signature.
22 +
23 +
TRAITS
24 +
25 +
* Prefer built-in traits to free functions or methods:
26 +
  * `FromStr` for implementing parsing.
27 +
  * `From` for implementing conversions.
28 +
  * `TryFrom` for implementing fallible conversions.
29 +
30 +
TYPES
31 +
32 +
* Prefer one enum that encodes valid state over several fields that can drift out of sync.
33 +
* Do not store fields that are only meaningful in one mode unless the mode is encoded in the type.
34 +
* Before creating a new type, check whether an existing type can be used as-is or
35 +
  modified slightly to serve both purposes, and choose the conceptually simpler
36 +
  design.
37 +
* When converting between two equivalent or intentionally related types, implement
38 +
  `From` for the conversion and use `from`/`into` at callsites instead of ad hoc
39 +
  match expressions at each use site.
40 +
* Always try to keep type definitions with their implementations.
41 +
42 +
NAMES
43 +
44 +
* Name fields after their role in the abstraction, not after an incidental implementation detail.
45 +
* Name callbacks after what the caller is asking them to do.
46 +
* When wrapping a lower-level operation, choose a name that distinguishes the wrapper from the operation it delegates to.
47 +
* Keep function names short, but not at the cost of hiding important context.
48 +
49 +
HELPERS
50 +
51 +
* Do not create small helper functions when they are only used in one place.
52 +
* Generally avoid one-line helpers, unless it's a complicated line.
53 +
54 +
DOCUMENTATION
55 +
56 +
* Start modules with `//!` documentation that states the module's role.
57 +
* Document all top-level types, functions, constants, and struct/enum fields with `///`.
58 +
* Replace magic numbers with named constants near related constants.
59 +
* Document constants by explaining the architectural or representation limit they encode.
60 +
* Never write documentation that refers to the current context in which the code is written.
61 +
62 +
RE-USE
63 +
64 +
* Always look for functions in the standard library that you can re-use, instead of implementing your own.
65 +
66 +
ENUMS & OPTIONS
67 +
68 +
* When mutable state is stored inside an enum payload, write updated payload values back to the owning enum.
69 +
* Use `let-else` and `if let` to unwrap optionals or match variants at the point where failure is handled.
70 +
71 +
ERRORS
72 +
73 +
* Use `panic!` for impossible internal invariants and `return` for recoverable or user-facing failures.
74 +
* Panic strings should be prefixed by the enclosing function name, and ":".
75 +
* Use `assert!` instead of `panic!` when it's clearer/simpler.
76 +
* Never do this: `f(g()?)` -- find a way to avoid having a `?` operator inside a function call.
77 +
78 +
MODULES
79 +
80 +
* For large modules, group related declarations with simple ASCII section banners such as `// Constants //` or `// Error Handling //`.
81 +
* Keep imports and sub-modules at the top, then constants, then types, then functions.