Skip to content
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

multiline chaining #169

Closed
lcowell opened this issue May 12, 2013 · 11 comments
Closed

multiline chaining #169

lcowell opened this issue May 12, 2013 · 11 comments

Comments

@lcowell
Copy link

lcowell commented May 12, 2013

I have a question about the multiline chaining example. You say:

When continuing a chained method invocation on another line keep the . on the second line.

# bad - need to consult first line to understand second line
one.two.three.
  four

# good - it's immediately clear what's going on the second line
one.two.three
  .four

However, the example labelled good does not work for me (using 1.9), while the example labelled bad does. I thought the dot at the end of the line was necessary in order to tell the ruby interpreter that the expression continued.

Here's an example:

1.to_i
  .to_s

syntax error, unexpected '.', expecting $end .to_s

Update:
OK, I've just figured out the issue is a 1.9 vs. 2.0 difference in syntax. The good example is only valid ruby in 2.0. I think there should be a note added to avoid confusion for people using 1.9. What is the recommended syntax for 1.9 ?

Thanks guys.

@cbandy
Copy link

cbandy commented May 15, 2013

Also, putting . on the second line precludes intervening comments in any version.

1.to_i
  # failure
  .to_s
1.to_i.
  # success
  to_s

@moreta
Copy link

moreta commented May 16, 2013

2013年5月16日木曜日 Chris Bandy notifications@github.com:

Also, putting . on the second line precludes intervening comments in any
version.

1.to_i

failure

.to_s

1.to_i.

success

to_s


Reply to this email directly or view it on GitHubhttps://github.com//issues/169#issuecomment-17971163
.

@adkron
Copy link
Contributor

adkron commented May 27, 2013

Without the period on the end it looks like a complete statement. This would cause me to stop reading for anything new.

Since the second has issues and is only valid in 1.9 why don't we stick with the period at the end.

Should there also be a note that long chains like this should be avoided.

lcowell added a commit to lcowell/ruby-style-guide that referenced this issue May 28, 2013
This changes where the '.' lives from the second line to the first line in the expression. Having no '.' at the end of the first line makes that line appear to be complete, but you must read ahead to the next line in order to see that the expression continues.

This closes issue rubocop#169
@equivalent
Copy link
Contributor

it appears to me that making dot on second line is completely inconsistent with multiple line or statements

 really_long_condition_that_needs_to_be_on_multiple_lines = nil 

 begin
    really_long_condition_that_needs_to_be_on_multiple_lines ||  true
 end
 #=> true

 begin
     really_long_condition_that_needs_to_be_on_multiple_lines 
        ||  true
  end
  #=> SyntaxError

@bbatsov
Copy link
Collaborator

bbatsov commented May 30, 2013

@equivalent Unfortunately this is true. But then again - Ruby is full of inconsistencies :-) Hopefully some day this will be fixed.

@equivalent
Copy link
Contributor

:) ... I like many of your points on syntax and it make sense that developer want to know what is happening on second line when he does multiple line chaining (specially in function returning value), however I find it completely inelegant to do stuff one way when chaining and another way when working with comparison operators

obviously I can do

begin
  really_long_condition_that_needs_to_be_on_multiple_lines \
    ||  true
end

but that is crushing my soul

one final argument for my theory:

I see your point that developer is lost when chaining on multiple lines without indentation and may assume that block below is just returning three

begin
 one.two.
 three   # what is happening here ?
end

... but when developer use extra indentation, he automatically knows that something else happened above

begin
 one.two.
   three    # extra indent makes me look one line up
end 

@fuadsaud
Copy link
Contributor

Btw, shouldn't the second line be indented as much as the last call on the first line?

one.two.three
       .four

This is the indentation proposed for multi line method calls

Mailer.deliver(to: 'bob@example.com',
               from: 'us@example.com',
               subject: 'Important message',
               body: source.text)

@fuadsaud
Copy link
Contributor

Oh, this has already been proposed by @jdalbert on #176.

@focused
Copy link

focused commented Jun 6, 2013

fuadsaud, this is the best style
+1

@philoserf
Copy link

i concur with the argument against.

#176 (comment)

@bbatsov
Copy link
Collaborator

bbatsov commented Feb 18, 2014

I decided to resolve this in a diplomatic manner, given the lack of consensus. See related commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants