diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-05-04 20:42:30 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-05-04 20:42:30 +0100 |
commit | 5b663f1b07233442a3b0b58db453504dcc51ddc9 (patch) | |
tree | 83e7822f41dd04db2f5e79d75f88a9c838beceee /docs/dev | |
parent | 010e4c8fe018d703aac38c54307ddbee35edc380 (diff) | |
parent | 1ea4dae59699a103209a7ecfc1a03c8df0d211af (diff) |
Merge #8732
8732: internal: refactor expansion queries r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'docs/dev')
-rw-r--r-- | docs/dev/style.md | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index 6ab60b50e..00de7a711 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> { ... } | |||
449 | fn query_first(name: String, case_sensitive: bool) -> Option<Item> { ... } | 449 | fn query_first(name: String, case_sensitive: bool) -> Option<Item> { ... } |
450 | ``` | 450 | ``` |
451 | 451 | ||
452 | ## Prefer Separate Functions Over Parameters | ||
453 | |||
454 | 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. | ||
455 | |||
456 | ```rust | ||
457 | // GOOD | ||
458 | fn caller_a() { | ||
459 | foo() | ||
460 | } | ||
461 | |||
462 | fn caller_b() { | ||
463 | foo_with_bar(Bar::new()) | ||
464 | } | ||
465 | |||
466 | fn foo() { ... } | ||
467 | fn foo_with_bar(bar: Bar) { ... } | ||
468 | |||
469 | // BAD | ||
470 | fn caller_a() { | ||
471 | foo(None) | ||
472 | } | ||
473 | |||
474 | fn caller_b() { | ||
475 | foo(Some(Bar::new())) | ||
476 | } | ||
477 | |||
478 | fn foo(bar: Option<Bar>) { ... } | ||
479 | ``` | ||
480 | |||
481 | **Rationale:** more often than not, such functions display "`false sharing`" -- they have additional `if` branching inside for two different cases. | ||
482 | Splitting the two different control flows into two functions simplifies each path, and remove cross-dependencies between the two paths. | ||
483 | If there's common code between `foo` and `foo_with_bar`, extract *that* into a common helper. | ||
484 | |||
452 | ## Avoid Monomorphization | 485 | ## Avoid Monomorphization |
453 | 486 | ||
454 | Avoid making a lot of code type parametric, *especially* on the boundaries between crates. | 487 | Avoid making a lot of code type parametric, *especially* on the boundaries between crates. |