From a01fd1af19314a73e5436a912f1cac2341ea11cd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 5 Apr 2021 13:02:47 +0300 Subject: internal: explain "extract if condition" refactoring --- docs/dev/style.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'docs/dev') diff --git a/docs/dev/style.md b/docs/dev/style.md index c594946be..48ce4b92a 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -842,7 +842,26 @@ Re-using originally single-purpose function often leads to bad coupling. ## Helper Variables -Introduce helper variables freely, especially for multiline conditions. +Introduce helper variables freely, especially for multiline conditions: + +```rust +// GOOD +let rustfmt_not_installed = + captured_stderr.contains("not installed") || captured_stderr.contains("not available"); + +match output.status.code() { + Some(1) if !rustfmt_not_installed => Ok(None), + _ => Err(format_err!("rustfmt failed:\n{}", captured_stderr)), +}; + +// BAD +match output.status.code() { + Some(1) + if !captured_stderr.contains("not installed") + && !captured_stderr.contains("not available") => Ok(None), + _ => Err(format_err!("rustfmt failed:\n{}", captured_stderr)), +}; +``` **Rationale:** like blocks, single-use variables are a cognitively cheap abstraction, as they have access to all the context. Extra variables help during debugging, they make it easy to print/view important intermediate results. -- cgit v1.2.3