STYLE 3.7 KiB raw
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 in functions.
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
* Functions should be named according to what they return, like `fn diff_document(...) -> Result<DiffDocument, Error>`.
49
50
HELPERS
51
52
* Do not create small helper functions when they are only used in one place.
53
* Generally avoid one-line helpers, unless it's a complicated line.
54
* Avoid free helper functions if they return a type defined in the same crate.
55
  Use a constructor function on that type instead, defined in the same file as
56
  the type.
57
58
DOCUMENTATION
59
60
* Start modules with `//!` documentation that states the module's role.
61
* Document all top-level types, functions, constants, and struct/enum fields with `///`.
62
* Replace magic numbers with named constants near related constants.
63
* Document constants by explaining the architectural or representation limit they encode.
64
* Never write documentation that refers to the current context in which the code is written.
65
* Avoid refering to specific code symbols unless it's a checkable link like [`symbol`].
66
67
RE-USE
68
69
* Always look for functions in the standard library that you can re-use, instead of implementing your own.
70
71
ENUMS & OPTIONS
72
73
* When mutable state is stored inside an enum payload, write updated payload values back to the owning enum.
74
* Use `let-else` and `if let` to unwrap optionals or match variants at the point where failure is handled.
75
76
ERRORS
77
78
* Use `panic!` for impossible internal invariants and `return` for recoverable or user-facing failures.
79
* Panic strings should be prefixed by the enclosing function name, and ":".
80
* Use `assert!` instead of `panic!` when it's clearer/simpler.
81
* Never do this: `f(g()?)` -- find a way to avoid having a `?` operator inside a function call.
82
83
MODULES
84
85
* Keep imports and sub-modules at the top, then constants, then types, then functions.
86
87
FUNCTIONS
88
89
* Prefer methods on types to free functions.
90
* Avoid private helpers that are only used once.
91
* Avoid accessor functions.
92
* Functions should take the narrowest types needed as arguments, eg. don't take
93
  `&User` if you only need `&UserId`.
94
95
TESTS
96
97
* Use `unwrap` in tests, not `expect`.
98
* Define tests in a module called `tests`.