Skip to content

Commit f25f585

Browse files
authored
Merge pull request #449 from github/styleguide-block-chaining-recommendation
Update STYLEGUIDE.md to clarify block usage
2 parents 67a7a67 + 6f8b0e5 commit f25f585

1 file changed

Lines changed: 3 additions & 17 deletions

File tree

STYLEGUIDE.md

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -840,11 +840,9 @@ end
840840
arr.each { |elem| puts elem }
841841
```
842842

843-
* Prefer `{...}` over `do...end` for single-line blocks. Avoid using
844-
`{...}` for multi-line blocks (multiline chaining is always
845-
ugly). Always use `do...end` for "control flow" and "method
846-
definitions" (e.g. in Rakefiles and certain DSLs). Avoid `do...end`
847-
when chaining.
843+
* Prefer `{...}` over `do...end` for single-line blocks. Avoid using
844+
`{...}` for multi-line blocks. Always use `do...end` for "control flow" and "method
845+
definitions" (e.g. in Rakefiles and certain DSLs).
848846
<a name="squiggly-braces"></a><sup>[[link](#squiggly-braces)]</sup>
849847
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleblockdelimiters">RuboCop rule: Style/BlockDelimiters</a>
850848

@@ -858,20 +856,8 @@ names.each { |name| puts name }
858856
names.each do |name|
859857
puts name
860858
end
861-
862-
# good
863-
names.select { |name| name.start_with?("S") }.map { |name| name.upcase }
864-
865-
# bad
866-
names.select do |name|
867-
name.start_with?("S")
868-
end.map { |name| name.upcase }
869859
```
870860

871-
* Some will argue that multiline chaining would look OK with the use of `{...}`,
872-
but they should ask themselves: is this code really readable and can't the block's
873-
contents be extracted into nifty methods?
874-
875861
* Avoid `return` where not required.
876862
<a name="avoid-return"></a><sup>[[link](#avoid-return)]</sup>
877863
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleredundantreturn">RuboCop rule: Style/RedundantReturn</a>

0 commit comments

Comments
 (0)