diff options
Diffstat (limited to 'docs/dev')
| -rw-r--r-- | docs/dev/style.md | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index 84485ea2845..f56f36e71c9 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -891,7 +891,6 @@ let buf = { }; // BAD - let buf = prepare_buf(&mut arena, item); ... @@ -909,6 +908,37 @@ Exception: if you want to make use of `return` or `?`. A block serves just as well to delineate a bit of logic, but has access to all the context. Re-using originally single-purpose function often leads to bad coupling. +## Local Helper Functions + +Put nested helper functions at the end of the enclosing functions +(this requires using return statement). +Don't nest more than one level deep. + +```rust +// GOOD +fn dfs(graph: &Graph, v: Vertex) -> usize { + let mut visited = FxHashSet::default(); + return go(graph, &mut visited, v); + + fn go(graph: &Graph, visited: &mut FxHashSet<Vertex>, v: usize) -> usize { + ... + } +} + +// BAD +fn dfs(graph: &Graph, v: Vertex) -> usize { + fn go(graph: &Graph, visited: &mut FxHashSet<Vertex>, v: usize) -> usize { + ... + } + + let mut visited = FxHashSet::default(); + go(graph, &mut visited, v) +} + +``` + +**Rationale:** consistency, improved top-down readability. + ## Helper Variables Introduce helper variables freely, especially for multiline conditions: |
