From 3be9ebe2c3726c7efc77ebdddb4445f7aff4260e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 4 Jul 2021 11:12:41 +0300 Subject: minor: style --- docs/dev/style.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'docs/dev') 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, v: usize) -> usize { + ... + } +} + +// BAD +fn dfs(graph: &Graph, v: Vertex) -> usize { + fn go(graph: &Graph, visited: &mut FxHashSet, 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: -- cgit 1.4.1-3-g733a5