about summary refs log tree commit diff
path: root/docs/dev
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2021-05-04 22:20:04 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2021-05-04 22:41:46 +0300
commit3f6980e4e146163de85ff780432f6f0c7b7645e7 (patch)
tree71c60fd6b6d1cb07f56602024466cb8b41b97ea9 /docs/dev
parent95dc8ef265183a624593a5ef49df31e53daf160e (diff)
downloadrust-3f6980e4e146163de85ff780432f6f0c7b7645e7.tar.gz
rust-3f6980e4e146163de85ff780432f6f0c7b7645e7.zip
simplify macro expansion code
Using `Option` arguments such that you always pass `None` or `Some` at
the call site is a code smell.
Diffstat (limited to 'docs/dev')
-rw-r--r--docs/dev/style.md33
1 files changed, 33 insertions, 0 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md
index 6ab60b50ed7..00de7a71175 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -449,6 +449,39 @@ fn query_all(name: String, case_sensitive: bool) -> Vec<Item> { ... }
 fn query_first(name: String, case_sensitive: bool) -> Option<Item> { ... }
 ```
 
+## Prefer Separate Functions Over Parameters
+
+If a function has a `bool` or an `Option` parameter, and it is always called with `true`, `false`, `Some` and `None` literals, split the function in two.
+
+```rust
+// GOOD
+fn caller_a() {
+    foo()
+}
+
+fn caller_b() {
+    foo_with_bar(Bar::new())
+}
+
+fn foo() { ... }
+fn foo_with_bar(bar: Bar) { ... }
+
+// BAD
+fn caller_a() {
+    foo(None)
+}
+
+fn caller_b() {
+    foo(Some(Bar::new()))
+}
+
+fn foo(bar: Option<Bar>) { ... }
+```
+
+**Rationale:** more often than not, such functions display "`false sharing`" -- they have additional `if` branching inside for two different cases.
+Splitting the two different control flows into two functions simplifies each path, and remove cross-dependencies between the two paths.
+If there's common code between `foo` and `foo_with_bar`, extract *that* into a common helper.
+
 ## Avoid Monomorphization
 
 Avoid making a lot of code type parametric, *especially* on the boundaries between crates.