Skip to content

Commit 839223d

Browse files
committed
Extract "Conditional Expressions" section
1 parent fe295a7 commit 839223d

File tree

1 file changed

+58
-50
lines changed

1 file changed

+58
-50
lines changed

STYLEGUIDE.md

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ This is GitHub's Ruby Style Guide, inspired by [RuboCop's guide][rubocop-guide].
2323
15. [Methods](#methods)
2424
1. [Method definitions](#method-definitions)
2525
2. [Method calls](#method-calls)
26-
27-
16. [Syntax](#syntax)
26+
16. [Conditional Expressions](#conditional-expressions)
27+
1. [Conditional keywords](#conditional-keywords)
28+
2. [Ternary operator](#ternary-operator)
29+
17. [Syntax](#syntax)
2830

2931
## Layout
3032

@@ -595,25 +597,9 @@ f (3 + 2) + 1
595597
f(3 + 2) + 1
596598
```
597599

598-
## Syntax
600+
## Conditional Expressions
599601

600-
* Never use `for`, unless you know exactly why. Most of the time iterators
601-
should be used instead. `for` is implemented in terms of `each` (so
602-
you're adding a level of indirection), but with a twist - `for`
603-
doesn't introduce a new scope (unlike `each`) and variables defined
604-
in its block will be visible outside it.
605-
606-
``` ruby
607-
arr = [1, 2, 3]
608-
609-
# bad
610-
for elem in arr do
611-
puts elem
612-
end
613-
614-
# good
615-
arr.each { |elem| puts elem }
616-
```
602+
### Conditional keywords
617603

618604
* Never use `then` for multi-line `if/unless`.
619605

@@ -629,38 +615,8 @@ if some_condition
629615
end
630616
```
631617

632-
* Avoid the ternary operator (`?:`) except in cases where all expressions are extremely
633-
trivial. However, do use the ternary operator(`?:`) over `if/then/else/end` constructs
634-
for single line conditionals.
635-
636-
``` ruby
637-
# bad
638-
result = if some_condition then something else something_else end
639-
640-
# good
641-
result = some_condition ? something : something_else
642-
```
643-
644-
* Use one expression per branch in a ternary operator. This
645-
also means that ternary operators must not be nested. Prefer
646-
`if/else` constructs in these cases.
647-
648-
``` ruby
649-
# bad
650-
some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else
651-
652-
# good
653-
if some_condition
654-
nested_condition ? nested_something : nested_something_else
655-
else
656-
something_else
657-
end
658-
```
659-
660618
* The `and` and `or` keywords are banned. It's just not worth it. Always use `&&` and `||` instead.
661619

662-
* Avoid multi-line `?:` (the ternary operator), use `if/unless` instead.
663-
664620
* Favor modifier `if/unless` usage when you have a single-line
665621
body.
666622

@@ -706,6 +662,58 @@ if x > 10
706662
end
707663
```
708664

665+
### Ternary operator
666+
667+
* Avoid the ternary operator (`?:`) except in cases where all expressions are extremely
668+
trivial. However, do use the ternary operator(`?:`) over `if/then/else/end` constructs
669+
for single line conditionals.
670+
671+
``` ruby
672+
# bad
673+
result = if some_condition then something else something_else end
674+
675+
# good
676+
result = some_condition ? something : something_else
677+
```
678+
679+
* Avoid multi-line `?:` (the ternary operator), use `if/unless` instead.
680+
681+
* Use one expression per branch in a ternary operator. This
682+
also means that ternary operators must not be nested. Prefer
683+
`if/else` constructs in these cases.
684+
685+
``` ruby
686+
# bad
687+
some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else
688+
689+
# good
690+
if some_condition
691+
nested_condition ? nested_something : nested_something_else
692+
else
693+
something_else
694+
end
695+
```
696+
697+
## Syntax
698+
699+
* Never use `for`, unless you know exactly why. Most of the time iterators
700+
should be used instead. `for` is implemented in terms of `each` (so
701+
you're adding a level of indirection), but with a twist - `for`
702+
doesn't introduce a new scope (unlike `each`) and variables defined
703+
in its block will be visible outside it.
704+
705+
``` ruby
706+
arr = [1, 2, 3]
707+
708+
# bad
709+
for elem in arr do
710+
puts elem
711+
end
712+
713+
# good
714+
arr.each { |elem| puts elem }
715+
```
716+
709717
* Prefer `{...}` over `do...end` for single-line blocks. Avoid using
710718
`{...}` for multi-line blocks (multiline chaining is always
711719
ugly). Always use `do...end` for "control flow" and "method

0 commit comments

Comments
 (0)