Reviewer · Code Style
Simplify control flow
Favor clear, minimal-nesting control flow over clever or deeply nested logic. Specifically: - When two branches are mutually exclusive, express that with an explicit `if`/`else` rather than an early `return` followed by fallthrough code, so the exclusivity is visually obvious:
Favor clear, minimal-nesting control flow over clever or deeply nested logic. Specifically:
- When two branches are mutually exclusive, express that with an explicit
if/elserather than an earlyreturnfollowed by fallthrough code, so the exclusivity is visually obvious:if self.insert(path, &storage_path)? { Ok(hash) } else { self.dedup(path, &storage_path)?; Ok(hash) } // or, simpler still when the success path is shared: if !self.insert(path, &storage_path)? { self.dedup(path, &storage_path)?; } Ok(hash) - Avoid unnecessary
returnstatements, especially as the trailing expression of a function/block (return self.default_lock_root();→self.default_lock_root()), and avoidreturnon simple expressions in builder-style functions. - Prefer
let-elseto flatten guard clauses instead of nesting the rest of the function inside anif let. - Avoid deeply nested
if let/ifchains for sequential checks; extract each check into its own early-return helper function (e.g.fn alt_paths(...) -> Option<...>) or use?/let-elseso the main function reads as a flat sequence of steps. - Write conditions in positive form when possible (checking the common/expected case first) instead of negating the primary condition, which is easier to follow at a glance.
- When a condition combines many clauses (e.g. several
&&/||terms with short-circuiting side effects), don’t just leave it as one large expression: add a comment explaining the overall intent, and consider extracting named closures or a well-documented helper function for each sub-check so the logic is easier to follow during review and maintenance. - Keep similar pieces of logic (e.g. multiple lint implementations) stylistically consistent with each other; don’t introduce one-off patterns (like inner functions) when the rest of the codebase handles similar cases differently, unless there’s a clear reason.
The goal is code where control flow can be understood at a glance: minimal nesting, no redundant returns, explicit branching for exclusive cases, and named/comment-explained logic for non-trivial conditions.