Skip to content

Commit 3e8a80d

Browse files
committed
Add "Readability over brevity" section in CODE_STYLE.md
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent fadf1a6 commit 3e8a80d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

CODE_STYLE.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,45 @@ Derive `Debug`, `Clone`, `Copy`, `PartialEq`, `Eq` and `Hash` for public types w
8888

8989
Derive `Default` when there is a reasonable default value for the type.
9090

91+
## Readability over brevity
92+
93+
Functional programming constructs are allowed, but the code must remain readable.
94+
Prefer explicit loops and conditionals over complex functional programming constructs when it improves readability,
95+
especially for complex logic with multiple nested conditions.
96+
97+
```rust
98+
// GOOD (more readable)
99+
fn has_nostr_event_uri(content: &str, event_id: &EventId) -> bool {
100+
const OPTS: NostrParserOptions = NostrParserOptions::disable_all().nostr_uris(true);
101+
102+
let parser = NostrParser::new().parse(content).opts(OPTS);
103+
104+
for token in parser.into_iter() {
105+
if let Token::Nostr(nip21) = token {
106+
if let Some(id) = nip21.event_id() {
107+
if &id == event_id {
108+
return true;
109+
}
110+
}
111+
}
112+
}
113+
114+
false
115+
}
116+
117+
// BAD (difficult to read)
118+
fn has_nostr_event_uri(content: &str, event_id: &EventId) -> bool {
119+
const OPTS: NostrParserOptions = NostrParserOptions::disable_all().nostr_uris(true);
120+
121+
NostrParser::new().parse(content).opts(OPTS).any(
122+
|token| matches!(token, Token::Nostr(uri) if uri.event_id().as_ref() == Some(event_id)),
123+
)
124+
}
125+
```
126+
127+
**Rationale:** Functional programming is powerful and often concise, but readability should not be sacrificed. Complex nested conditions, guard clauses, and intricate pattern matching within closures can make code difficult to understand and debug. Choose the approach that makes the intent clearest.
128+
129+
91130
## Full paths for logging
92131

93132
Always write `tracing::<op>!(...)` instead of importing `use tracing::<op>;` and invoking `<op>!(...)`.

0 commit comments

Comments
 (0)