-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Limit use of conditional modifiers to short, simple cases. #738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0f20d49
Limit use of conditional modifiers to short, simple cases.
MatheusRich cf17df3
add link to the pull-request to surface additional conversation
vburzynski 0262088
Merge branch 'main' into conditional-modifiers-are-okay
vburzynski 3522c5b
Use callout syntax
vburzynski fbbc24e
keep title and filename in sync
vburzynski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
ruby/Limit use of conditional modifiers to short, simple cases.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # Limit use of conditional modifiers to short, simple cases | ||
|
|
||
| Conditional modifiers (i.e., `if` or `unless` at the end of a line) can be | ||
| surprising when they appear on long or complex lines. The reader might not see | ||
| them while scanning the code. | ||
|
|
||
| So, prefer to use them only for short, simple cases. For example: | ||
|
|
||
| ```ruby | ||
| do_later if async? | ||
| ``` | ||
|
|
||
| The example above can read more naturally than: | ||
|
|
||
| ```rb | ||
| if async? | ||
| do_later | ||
| end | ||
| ``` | ||
|
|
||
| ## Complex conditions | ||
|
|
||
| However, if the line is too long (around 80 characters) or complex (e.g., an | ||
| `if` with multiple conditions like `if a && b`) prefer the multi-line form: | ||
|
|
||
| ```ruby | ||
| # Avoid | ||
| block_access! if signed_in? && !current_user.active? | ||
|
|
||
| # Prefer | ||
| if signed_in? && !current_user.active? | ||
| block_access! | ||
| end | ||
| ``` | ||
|
|
||
| There might be cases where the conditional modifier work well with multiple | ||
| conditions, so use your best judgment. | ||
|
|
||
| ## An opportunity to refactor | ||
|
|
||
| If the conditions are related, consider extracting a method that groups them. | ||
| This might allow you to use the conditional modifier form again. | ||
|
|
||
| ```ruby | ||
| def inactive_user? | ||
| signed_in? && !current_user.active? | ||
| end | ||
|
|
||
| block_access! if inactive_user? | ||
| ``` | ||
|
|
||
| ## Conditional modifiers feel informal | ||
|
|
||
| The modifier form of conditionals can feel more casual than the multi-line form. | ||
| Conversely, the multi-line form _draws attention_ to the conditional and the | ||
| code that follows it. Use this to your advantage when you want to emphasize the | ||
| conditional and the code that follows it. | ||
|
|
||
| ```rb | ||
| # Avoid | ||
| def action | ||
| return destroy_all if really? | ||
|
|
||
| do_nothing | ||
| end | ||
|
|
||
| # Prefer | ||
| def action | ||
| if really? | ||
| destroy_all | ||
| else | ||
| do_nothing | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
| You can also refactor the code so the less destructive action uses a conditional | ||
| modifier, which pairs well with the informal feel of the modifier form: | ||
|
|
||
| ```rb | ||
| def action | ||
| return do_nothing if chill? | ||
|
|
||
| destroy_all | ||
| end | ||
| ``` | ||
|
|
||
| ## References | ||
|
|
||
| - You can see further discussion of this guideline here: [#738](https://github.com/thoughtbot/guides/pull/738) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This renders as
