ruby-style-guide icon indicating copy to clipboard operation
ruby-style-guide copied to clipboard

what to do when if conditions too long?

Open qoyyim opened this issue 10 years ago • 13 comments

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?

qoyyim avatar Aug 25 '15 04:08 qoyyim

I think extracting predicate method is best solution.

mentero avatar Aug 25 '15 06:08 mentero

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

juandazapata avatar Aug 25 '15 15:08 juandazapata

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 avatar Sep 22 '15 04:09 qoyyim

@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 avatar Sep 22 '15 12:09 onebree

@onebree hmm okay

qoyyim avatar Oct 01 '15 08:10 qoyyim

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()

qoyyim avatar Oct 01 '15 08:10 qoyyim

Wondering the same thing!!

gregblass avatar Dec 16 '16 01:12 gregblass

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

ssvb avatar Jan 02 '17 04:01 ssvb

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

benjineering avatar Nov 26 '17 03:11 benjineering

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

jtrees avatar May 16 '18 11:05 jtrees

I will steal this one, ty! =)

if some.long.method &&
  other_condition &&
  another_condition
then
  do_something
  do_something_else
end

abdullaachilov avatar Nov 06 '19 11:11 abdullaachilov

if(condition) {

						  if(condition) {
	            			
							  if(condition) {
	            			
								  if(condition) {
	                                                      }
                                                            }
                                                     }
                                               }

this is easy to understand your code also

Sanmarley avatar Jan 21 '21 08:01 Sanmarley

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.

pirj avatar Feb 21 '21 21:02 pirj