diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-04-02 13:00:30 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-04-02 13:00:30 +0100 |
commit | 71ef64b673595807ccb4b3f5b7ad6ea55e63645b (patch) | |
tree | f5e95492a56a4e4d40bd66ce15b4ffe0884460eb /docs/dev/style.md | |
parent | 00ce7ae5248b41d5944abe6075407236cde1b075 (diff) | |
parent | 636c3c49d26f53a363a68a10b70ec5cf675df27c (diff) |
Merge #8293
8293: internal: document style for helper functions and variables r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'docs/dev/style.md')
-rw-r--r-- | docs/dev/style.md | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index 1b1c24b1e..c594946be 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md | |||
@@ -806,9 +806,48 @@ if let Some(expected_type) = ctx.expected_type.as_ref() { | |||
806 | } | 806 | } |
807 | ``` | 807 | ``` |
808 | 808 | ||
809 | **Rational:** `match` is almost always more compact. | 809 | **Rationale:** `match` is almost always more compact. |
810 | The `else` branch can get a more precise pattern: `None` or `Err(_)` instead of `_`. | 810 | The `else` branch can get a more precise pattern: `None` or `Err(_)` instead of `_`. |
811 | 811 | ||
812 | ## Helper Functions | ||
813 | |||
814 | Avoid creating singe-use helper functions: | ||
815 | |||
816 | ```rust | ||
817 | // GOOD | ||
818 | let buf = { | ||
819 | let mut buf = get_empty_buf(&mut arena); | ||
820 | buf.add_item(item); | ||
821 | buf | ||
822 | }; | ||
823 | |||
824 | // BAD | ||
825 | |||
826 | let buf = prepare_buf(&mut arena, item); | ||
827 | |||
828 | ... | ||
829 | |||
830 | fn prepare_buf(arena: &mut Arena, item: Item) -> ItemBuf { | ||
831 | let mut res = get_empty_buf(&mut arena); | ||
832 | res.add_item(item); | ||
833 | res | ||
834 | } | ||
835 | ``` | ||
836 | |||
837 | Exception: if you want to make use of `return` or `?`. | ||
838 | |||
839 | **Rationale:** single-use functions change frequently, adding or removing parameters adds churn. | ||
840 | A block serves just as well to delineate a bit of logic, but has access to all the context. | ||
841 | Re-using originally single-purpose function often leads to bad coupling. | ||
842 | |||
843 | ## Helper Variables | ||
844 | |||
845 | Introduce helper variables freely, especially for multiline conditions. | ||
846 | |||
847 | **Rationale:** like blocks, single-use variables are a cognitively cheap abstraction, as they have access to all the context. | ||
848 | Extra variables help during debugging, they make it easy to print/view important intermediate results. | ||
849 | Giving a name to a condition in `if` expression often improves clarity and leads to a nicer formatted code. | ||
850 | |||
812 | ## Token names | 851 | ## Token names |
813 | 852 | ||
814 | Use `T![foo]` instead of `SyntaxKind::FOO_KW`. | 853 | Use `T![foo]` instead of `SyntaxKind::FOO_KW`. |