Skip to content

Commit 488e6c5

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

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

ruby/README.md

Lines changed: 5 additions & 1 deletion
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]
6+
> Click on the linked pull request, commit, or the guideline itself to read more
7+
> detailed explanations with examples and reasoning behind these 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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
block_access! if signed_in? && !current_user.active?
29+
30+
# Prefer
31+
if signed_in? && !current_user.active?
32+
block_access!
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 inactive_user?
43+
signed_in? && !current_user.active?
44+
end
45+
46+
block_access! if inactive_user?
47+
```
48+
49+
## Conditional modifiers feel informal
50+
51+
The modifier form of conditionals can feel more casual than the multi-line form.
52+
Conversely, the multi-line form _draws attention_ to the conditional and the
53+
code that follows it. Use this to your advantage when you want to emphasize the
54+
conditional and the code that follows it.
55+
56+
```rb
57+
# Avoid
58+
def action
59+
return destroy_all if really?
60+
61+
do_nothing
62+
end
63+
64+
# Prefer
65+
def action
66+
if really?
67+
destroy_all
68+
else
69+
do_nothing
70+
end
71+
end
72+
```
73+
74+
You can also refactor the code so the less destructive action uses a conditional
75+
modifier, which pairs well with the informal feel of the modifier form:
76+
77+
```rb
78+
def action
79+
return do_nothing if chill?
80+
81+
destroy_all
82+
end
83+
```
84+
85+
## Use your best judgment
86+
87+
There might be cases where the conditional modifier work well with multiple
88+
conditions, so use your best judgment. Overall, prefer writing code that is easy
89+
to read and understand, while being idiomatic.

0 commit comments

Comments
 (0)