@cratis/pi 7.189.5 → 7.190.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -43,6 +43,45 @@ are grounded in `Cratis/Chronicle.Elixir` 4.x (`Source/chronicle/mix.exs`,
|
|
|
43
43
|
operational logging and keep credentials out of messages and crash reports.
|
|
44
44
|
- Use American English in identifiers, comments and documentation.
|
|
45
45
|
|
|
46
|
+
## Anti-patterns to avoid
|
|
47
|
+
|
|
48
|
+
Check new Elixir against the official anti-pattern pages (code, design, process and meta-programming
|
|
49
|
+
anti-patterns in the Elixir documentation). The ones that matter most here:
|
|
50
|
+
|
|
51
|
+
- **Never create atoms from input.** `String.to_atom/1` on a name that a user, a file or a wire message
|
|
52
|
+
supplies leaks atoms. Compare as strings, or map through an explicit table or
|
|
53
|
+
`String.to_existing_atom/1`. A bounded, developer-written set of names is acceptable.
|
|
54
|
+
- **Match the shape you need.** Use `%{key: value} = map` or `Map.fetch!/2` for required data; `Map.get/3`
|
|
55
|
+
and `opts[:key]` are for optional data, and a default must not hide a missing required value
|
|
56
|
+
(`Map.get(actor, :id) || "unknown"`). Prefer `%{parts: parts} <- state` to `x when x != nil <- state`.
|
|
57
|
+
- **Do not rescue to find out whether something exists.** Check with `function_exported?/3` (after
|
|
58
|
+
`Code.ensure_loaded?/1`) or a pattern match. `rescue _ ->` that returns a default swallows real errors;
|
|
59
|
+
rescue narrowly, around foreign code only.
|
|
60
|
+
- **Do not let `with ... else` carry business logic.** Make each step return a tagged result
|
|
61
|
+
(`:ok` or `{:error, reason}`) so `else` is unnecessary or has one clause per distinct failure. Do not
|
|
62
|
+
mix a boolean into a `with` (`true <- cond`); wrap it in a function that returns a tagged result.
|
|
63
|
+
- **Return one shape.** Do not let an option or an argument change the return type or select between
|
|
64
|
+
different outcomes (`outcome: "applied" | "failed"` choosing the event); use two functions or commands.
|
|
65
|
+
Use `{:error, :not_found}` rather than `{:ok, nil}` when absence is a failure for the caller.
|
|
66
|
+
- **Keep parameter lists short.** Three or four. When a function needs more, or discards most of them,
|
|
67
|
+
pass one map or struct. Do not keep an unused parameter.
|
|
68
|
+
- **Supervise processes.** No bare `Task.start/1` or `spawn` for work that must not fail silently; start it
|
|
69
|
+
under a `Task.Supervisor` in the supervision tree. A process dictionary value does not reach a `Task`.
|
|
70
|
+
- **Libraries do not read global application configuration.** Take options from the caller. An
|
|
71
|
+
application may call `Application.get_env/2` for its own settings, ideally in one settings module.
|
|
72
|
+
Compile-time config belongs in `Application.compile_env/3`.
|
|
73
|
+
- **Macros earn their place.** Use a macro for syntax a function cannot take (a declaration DSL, a block)
|
|
74
|
+
and `use` only to define or register something, not to import functions.
|
|
75
|
+
- **Stringly typed values stay at the boundary.** Wire strings (ids, ISO 8601 times, enum names) are fine in
|
|
76
|
+
events and commands; decide on atoms or structs inside the code.
|
|
77
|
+
|
|
78
|
+
### Cohesion over file count
|
|
79
|
+
|
|
80
|
+
Do not split code only because a file is long or holds several modules. **High cohesion wins**: keep a
|
|
81
|
+
slice's command, its events and its specs next to each other, and a view's markup with the LiveView that
|
|
82
|
+
owns it. A module is too big when it has several reasons to change, not when it has many lines. These
|
|
83
|
+
anti-pattern fixes must not scatter a vertical slice across folders.
|
|
84
|
+
|
|
46
85
|
## Event-sourced applications on Arc and Chronicle
|
|
47
86
|
|
|
48
87
|
Read the **cratis-arc-elixir** and **cratis-chronicle-client-elixir** skills before touching a slice.
|
|
@@ -42,6 +42,45 @@ are grounded in `Cratis/Chronicle.Elixir` 4.x (`Source/chronicle/mix.exs`,
|
|
|
42
42
|
operational logging and keep credentials out of messages and crash reports.
|
|
43
43
|
- Use American English in identifiers, comments and documentation.
|
|
44
44
|
|
|
45
|
+
## Anti-patterns to avoid
|
|
46
|
+
|
|
47
|
+
Check new Elixir against the official anti-pattern pages (code, design, process and meta-programming
|
|
48
|
+
anti-patterns in the Elixir documentation). The ones that matter most here:
|
|
49
|
+
|
|
50
|
+
- **Never create atoms from input.** `String.to_atom/1` on a name that a user, a file or a wire message
|
|
51
|
+
supplies leaks atoms. Compare as strings, or map through an explicit table or
|
|
52
|
+
`String.to_existing_atom/1`. A bounded, developer-written set of names is acceptable.
|
|
53
|
+
- **Match the shape you need.** Use `%{key: value} = map` or `Map.fetch!/2` for required data; `Map.get/3`
|
|
54
|
+
and `opts[:key]` are for optional data, and a default must not hide a missing required value
|
|
55
|
+
(`Map.get(actor, :id) || "unknown"`). Prefer `%{parts: parts} <- state` to `x when x != nil <- state`.
|
|
56
|
+
- **Do not rescue to find out whether something exists.** Check with `function_exported?/3` (after
|
|
57
|
+
`Code.ensure_loaded?/1`) or a pattern match. `rescue _ ->` that returns a default swallows real errors;
|
|
58
|
+
rescue narrowly, around foreign code only.
|
|
59
|
+
- **Do not let `with ... else` carry business logic.** Make each step return a tagged result
|
|
60
|
+
(`:ok` or `{:error, reason}`) so `else` is unnecessary or has one clause per distinct failure. Do not
|
|
61
|
+
mix a boolean into a `with` (`true <- cond`); wrap it in a function that returns a tagged result.
|
|
62
|
+
- **Return one shape.** Do not let an option or an argument change the return type or select between
|
|
63
|
+
different outcomes (`outcome: "applied" | "failed"` choosing the event); use two functions or commands.
|
|
64
|
+
Use `{:error, :not_found}` rather than `{:ok, nil}` when absence is a failure for the caller.
|
|
65
|
+
- **Keep parameter lists short.** Three or four. When a function needs more, or discards most of them,
|
|
66
|
+
pass one map or struct. Do not keep an unused parameter.
|
|
67
|
+
- **Supervise processes.** No bare `Task.start/1` or `spawn` for work that must not fail silently; start it
|
|
68
|
+
under a `Task.Supervisor` in the supervision tree. A process dictionary value does not reach a `Task`.
|
|
69
|
+
- **Libraries do not read global application configuration.** Take options from the caller. An
|
|
70
|
+
application may call `Application.get_env/2` for its own settings, ideally in one settings module.
|
|
71
|
+
Compile-time config belongs in `Application.compile_env/3`.
|
|
72
|
+
- **Macros earn their place.** Use a macro for syntax a function cannot take (a declaration DSL, a block)
|
|
73
|
+
and `use` only to define or register something, not to import functions.
|
|
74
|
+
- **Stringly typed values stay at the boundary.** Wire strings (ids, ISO 8601 times, enum names) are fine in
|
|
75
|
+
events and commands; decide on atoms or structs inside the code.
|
|
76
|
+
|
|
77
|
+
### Cohesion over file count
|
|
78
|
+
|
|
79
|
+
Do not split code only because a file is long or holds several modules. **High cohesion wins**: keep a
|
|
80
|
+
slice's command, its events and its specs next to each other, and a view's markup with the LiveView that
|
|
81
|
+
owns it. A module is too big when it has several reasons to change, not when it has many lines. These
|
|
82
|
+
anti-pattern fixes must not scatter a vertical slice across folders.
|
|
83
|
+
|
|
45
84
|
## Event-sourced applications on Arc and Chronicle
|
|
46
85
|
|
|
47
86
|
Read the **cratis-arc-elixir** and **cratis-chronicle-client-elixir** skills before touching a slice.
|