diff options
author | Aleksey Kladov <[email protected]> | 2020-08-24 11:19:12 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-08-24 11:22:28 +0100 |
commit | d7ece3028de09f30af4384726eca1eff2cd52dff (patch) | |
tree | aba886ff55f59399c1affebc3bbf867b5b2573af /docs | |
parent | e65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff) |
Add Early Return rule to style
Diffstat (limited to 'docs')
-rw-r--r-- | docs/dev/style.md | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index 44f0956c2..bb7a351f3 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md | |||
@@ -181,6 +181,30 @@ fn frobnicate(walrus: Option<Walrus>) { | |||
181 | } | 181 | } |
182 | ``` | 182 | ``` |
183 | 183 | ||
184 | # Early Returns | ||
185 | |||
186 | Do use early returns | ||
187 | |||
188 | ```rust | ||
189 | // Good | ||
190 | fn foo() -> Option<Bar> { | ||
191 | if !condition() { | ||
192 | return None; | ||
193 | } | ||
194 | |||
195 | Some(...) | ||
196 | } | ||
197 | |||
198 | // Not as good | ||
199 | fn foo() -> Option<Bar> { | ||
200 | if condition() { | ||
201 | Some(...) | ||
202 | } else { | ||
203 | None | ||
204 | } | ||
205 | } | ||
206 | ``` | ||
207 | |||
184 | # Getters & Setters | 208 | # Getters & Setters |
185 | 209 | ||
186 | If a field can have any value without breaking invariants, make the field public. | 210 | If a field can have any value without breaking invariants, make the field public. |
@@ -189,7 +213,7 @@ Never provide setters. | |||
189 | 213 | ||
190 | Getters should return borrowed data: | 214 | Getters should return borrowed data: |
191 | 215 | ||
192 | ``` | 216 | ```rust |
193 | struct Person { | 217 | struct Person { |
194 | // Invariant: never empty | 218 | // Invariant: never empty |
195 | first_name: String, | 219 | first_name: String, |