Skip to content

Commit 1ff54d2

Browse files
committed
Limit use of conditional modifiers to short, simple cases.
1 parent b4cf871 commit 1ff54d2

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

ruby/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
[Sample 1](sample_1.rb) [Sample 2](sample_2.rb)
44

5+
> [!TIP] Click on the linked pull request, commit, or the guideline itself to
6+
> read more detailed explanations with examples and reasoning behind these
7+
> recommendations.
8+
59
- Use [standard]
6-
- Avoid conditional modifiers (lines that end with conditionals). [36491dbb9]
10+
- [Limit use of conditional modifiers to short, simple cases.](./conditional_modifiers.md)
711
- Avoid multiple assignments per line (`one, two = 1, 2`). [#109]
812
- Avoid organizational comments (`# Validations`). [#63]
913
- Avoid ternary operators (`boolean ? true : false`). Use multi-line `if`

ruby/conditional_modifiers.md

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Limit use of conditional modifiers to short, simple cases
2+
3+
Conditional modifiers (i.e., `if` or `unless` at the end of a line) be
4+
surprising when they appear on long or complex lines. The reader might not see
5+
them while scanning the code.
6+
7+
So, prefer to use them only for short, simple cases. For example:
8+
9+
```ruby
10+
do_later if async?
11+
```
12+
13+
The example above can read more naturally than:
14+
15+
```rb
16+
if async?
17+
do_later
18+
end
19+
```
20+
21+
## Complex conditions
22+
23+
However, if the line is too long (around 80 characters) or complex (e.g., an
24+
`if` with multiple conditions `if a && b`) prefer the multi-line form:
25+
26+
```ruby
27+
# Avoid
28+
do_something if condition && condition2
29+
30+
# Prefer
31+
if condition && condition2
32+
do_something
33+
end
34+
```
35+
36+
## An opportunity to refactor
37+
38+
If the conditions are related, consider extracting a method that groups them.
39+
This might allow you to use the conditional modifier form again.
40+
41+
```ruby
42+
def condition3 = condition && condition2
43+
44+
do_something if condition3
45+
```
46+
47+
## Conditional modifiers feel informal
48+
49+
The modifier form of conditionals can feel more casual than the multi-line form.
50+
Conversely, the multi-line form _draws attention_ to the conditional and the
51+
code that follows it. Use this to your advantage when you want to emphasize the
52+
conditional and the code that follows it.
53+
54+
```rb
55+
# Avoid
56+
def action
57+
return destroy_all if really?
58+
59+
do_nothing
60+
end
61+
62+
# Prefer
63+
def action
64+
if really?
65+
destroy_all
66+
else
67+
do_nothing
68+
end
69+
end
70+
```
71+
72+
You can also refactor the code so the less destructive action uses a conditional
73+
modifier, which pairs well with the informal feel of the modifier form:
74+
75+
```rb
76+
def action
77+
return do_nothing if chill?
78+
79+
destroy_all
80+
end
81+
```
82+
83+
## Use your best judgment
84+
85+
There might be cases where the conditional modifier work well with multiple
86+
conditions, so use your best judgment. Overall, prefer writing code that is easy
87+
to read and understand, while being idiomatic.

0 commit comments

Comments
 (0)