ruby-style-guide
ruby-style-guide copied to clipboard
what to do when if conditions too long?
Which syntax is preferred for multi line conditions on if, like when the conditions are too long that they over 80 chars limit
if some.long.method && other_condition && another_condition
do_something
do_something_else
end
my guess is normal indent
if some.long.method &&
other_condition &&
another_condition
do_something
do_something_else
end
or double indent
if some.long.method &&
other_condition &&
another_condition
do_something
do_something_else
end
i think the second one is more readable or anyone have better idea?
I think extracting predicate method is best solution.
Extract the conditional and use it like:
if my_condition?
do_something
end
And define the method:
def my_condition?
some.long.method && other_condition && another_condition
end
but if they need many variables to compare it's will still over 80 char limit on if condition clause (due to passing variables as parameter ) so i don't think that's work on my case but thanks for suggestion :+1:
@qoyyim know that although the 80 char limit is in the style guide, many people also agree on the 120 (github side-by-side diff) or 100 (in between both) char limits.
@onebree hmm okay
btw just realized that python also have same problem https://www.python.org/dev/peps/pep-0008/#id12
When the conditional part of an if -statement is long enough to require that it be written across multiple lines, it's worth noting that the combination of a two character keyword (i.e. if ), plus a single space, plus an opening parenthesis creates a natural 4-space indent for the subsequent lines of the multiline conditional. This can produce a visual conflict with the indented suite of code nested inside the if -statement, which would also naturally be indented to 4 spaces. This PEP takes no explicit position on how (or whether) to further visually distinguish such conditional lines from the nested suite inside the if -statement. Acceptable options in this situation include, but are not limited to:
No extra indentation.
if (this_is_one_thing and that_is_another_thing): do_something()Add a comment, which will provide some distinction in editors supporting syntax highlighting.
if (this_is_one_thing and that_is_another_thing): # Since both conditions are true, we can frobnicate. do_something()Add some extra indentation on the conditional continuation line.
if (this_is_one_thing and that_is_another_thing): do_something()
Wondering the same thing!!
I'm personally making use of the then keyword in such cases as an explicit separator:
if some.long.method &&
other_condition &&
another_condition
then
do_something
do_something_else
end
I like the use of the then keyword @ssvb suggested, but without the extra indentation.
if some.long.method &&
other_condition &&
another_condition
then
do_something
do_something_else
end
So far I've been doing this:
if (
some.long.method &&
other_condition &&
another_condition
)
do_something
do_something_else
end
But I actually prefer the use of then, as in @ssvb's and @benjineering's examples. Thanks!
I think in future, I'll go with this, so that the conditions are all aligned and visually wrapped by if and then:
if
some.long.method &&
other_condition &&
another_condition
then
do_something
do_something_else
end
I will steal this one, ty! =)
if some.long.method &&
other_condition &&
another_condition
then
do_something
do_something_else
end
if(condition) {
if(condition) {
if(condition) {
if(condition) {
}
}
}
}
this is easy to understand your code also
I have a completely different suggestion. I clearly understand isn't a silver bullet.
Double indent just for the reference:
if @bisect_runner_class &&
value != @bisect_runner &&
allow_multiple_bisects?
log "error message"
fail "oh noes!"
end
Temporary named variable:
bisect_already_in_use = @bisect_runner_class &&
value != @bisect_runner &&
allow_multiple_bisects?
if bisect_already_in_use
log "error message"
fail "oh noes!"
end
The reasoning is to ease up the understanding of what all this construct really means.