rubocop / rubocop/ruby-style-guide
formatting multiline boolean expressions
Nobody has claimed this yet.
- Dominant language
- No language data
- Stars
- 16.5k
- Forks
- 3.3k
- PR merge metrics
- No merged PRs in 30d
Description
How should one format multiline boolean expressions? For instance maybe each condition is a long statement and it would be too long or unreadable on a single line. Or should this be avoided altogether? If so, what's the alternative?
I couldn't find any guidance on this in the style guide. Should it be added, whatever the verdict is?
One option:
def my_predicate?
first_long_condition ||
second_long_condition ||
third_long_condition ||
last_long_condition
end
Or maybe with indentation after the first one?
def my_predicate?
first_long_condition ||
second_long_condition ||
third_long_condition ||
last_long_condition
end
Or wrap only when you hit the line limit? With or without indenting?
def my_predicate?
first_long_condition || second_long_condition ||
third_long_condition || last_long_condition
end
Of course you could give each condition its own predicate method, but in many cases that's overkill:
def predicate_one?
first_long_condition
end
# and so forth ...
def my_predicate?
predicate_one? || predicate_two? || predicate_three? || predicate_four?
end
You could evaluate them separately and assign the results, but you forfeit short-circuit evaluation:
def my_predicate?
a = first_long_condition
b = second_long_condition
c = third_long_condition
d = last_long_condition
a || b || c || d
end
This is related to #476 but not exactly the same.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Review the existing style guide and related issue #476, then resolve the formatting alternatives raised in this discussion. Done means the community-approved guidance for multiline boolean expressions is added to the style guide with examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100