=====
STYLE
=====

Coding style guide for the Radiance language.

BOOLEANS

* Use small enums for mode, role, and state choices instead of bare booleans.

MACROS

* Avoid creating new macros.
* Avoid using third-party macros.

CALLSITES

* Avoid boolean parameters when the meaning is not obvious at the callsite.
* Avoid `Option` parameters in functions.
* Use option structs when a function takes multiple configuration values, when that helps with clarity.
* Keep callsites readable without needing to inspect the callee signature.

TRAITS

* Prefer built-in traits to free functions or methods:
  * `FromStr` for implementing parsing.
  * `From` for implementing conversions.
  * `TryFrom` for implementing fallible conversions.

TYPES

* Prefer one enum that encodes valid state over several fields that can drift out of sync.
* Do not store fields that are only meaningful in one mode unless the mode is encoded in the type.
* Before creating a new type, check whether an existing type can be used as-is or
  modified slightly to serve both purposes, and choose the conceptually simpler
  design.
* When converting between two equivalent or intentionally related types, implement
  `From` for the conversion and use `from`/`into` at callsites instead of ad hoc
  match expressions at each use site.
* Always try to keep type definitions with their implementations.

NAMES

* Name fields after their role in the abstraction, not after an incidental implementation detail.
* Name callbacks after what the caller is asking them to do.
* When wrapping a lower-level operation, choose a name that distinguishes the wrapper from the operation it delegates to.
* Keep function names short, but not at the cost of hiding important context.
* Functions should be named according to what they return, like `fn diff_document(...) -> Result<DiffDocument, Error>`.

HELPERS

* Do not create small helper functions when they are only used in one place.
* Generally avoid one-line helpers, unless it's a complicated line.
* Avoid free helper functions if they return a type defined in the same crate.
  Use a constructor function on that type instead, defined in the same file as
  the type.

DOCUMENTATION

* Start modules with `//!` documentation that states the module's role.
* Document all top-level types, functions, constants, and struct/enum fields with `///`.
* Replace magic numbers with named constants near related constants.
* Document constants by explaining the architectural or representation limit they encode.
* Never write documentation that refers to the current context in which the code is written.
* Avoid refering to specific code symbols unless it's a checkable link like [`symbol`].

RE-USE

* Always look for functions in the standard library that you can re-use, instead of implementing your own.

ENUMS & OPTIONS

* When mutable state is stored inside an enum payload, write updated payload values back to the owning enum.
* Use `let-else` and `if let` to unwrap optionals or match variants at the point where failure is handled.

ERRORS

* Use `panic!` for impossible internal invariants and `return` for recoverable or user-facing failures.
* Panic strings should be prefixed by the enclosing function name, and ":".
* Use `assert!` instead of `panic!` when it's clearer/simpler.
* Never do this: `f(g()?)` -- find a way to avoid having a `?` operator inside a function call.

MODULES

* Keep imports and sub-modules at the top, then constants, then types, then functions.

FUNCTIONS

* Prefer methods on types to free functions.
* Avoid private helpers that are only used once.
* Avoid accessor functions.
* Functions should take the narrowest types needed as arguments, eg. don't take
  `&User` if you only need `&UserId`.

TESTS

* Use `unwrap` in tests, not `expect`.
* Define tests in a module called `tests`.
