Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Indentation on multi-line method calls #80

Closed
Odaeus opened this Issue · 7 comments

6 participants

Andrew France Bozhidar Batsov Mark Burns Patrick Connolly Stephen Touset Adam Stegman
Andrew France

I can't see anything about indenting multi-line method calls, which are relatively common if you need to supply a lot of params to an object.

Examples of the styles:

# Original
def send_mail(source)
  Mailer.deliver(to: "bob@example.com", from: "us@example.com", subject: "Important message", body: source.text)
end

# Two-indent
def send_mail(source)
  Mailer.deliver(
    to: "bob@example.com",
    from: "us@example.com",
    subject: "Important message",
    body: source.text)
end

# Four-indent (my preferred)
def send_mail(source)
  Mailer.deliver(
      to: "bob@example.com",
      from: "us@example.com",
      subject: "Important message",
      body: source.text)
end

# Aligned
def send_mail(source)
  Mailer.deliver(to: "bob@example.com",
                 from: "us@example.com",
                 subject: "Important message",
                 body: source.text)
end

I prefer double my normal indentation (so four spaces) to make it clear these are params, but I don't think it's common at all. I'm not keen on aligned style even though it's more common because of the effort and maintenance required.

What do you think should be in the style guide?

Bozhidar Batsov
Owner

Definitely. My experience shows that the aligned style is the most popular style (and it's also my preferred style). Note that in that your example there is only one method param.

Andrew France

Oh yes, oops, I'm so used to supplying hashes to methods! Although a hash is probably most common when a method needs a lot of config passed.

Mark Burns
#I quite like these kind of things, but they're both very adhoc
Mailer.deliver(to:      "bob@example.com",
               from:    "us@example.com",
               subject: "Important message",
               body:    source.text)

#or inspired by rubinius stack traces:
Mailer.deliver(to: "bob@example.com",
             from: "us@example.com",
          subject: "Important message",
             body: source.text)
Bozhidar Batsov bbatsov closed this issue from a commit
Bozhidar Batsov fixes #80 - Indentation on multi-line method calls 4e98267
Bozhidar Batsov bbatsov closed this in 4e98267
Patrick Connolly

Just because I'm new to the ruby community, why is this not something people do:

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

Scatches the alignment itch without looking too adhoc with the bracket placement. Yeah, it's 2 extra lines, but that separation is at a visually useful place. And vertical space is in abundance, while horizontal space is a commodity. Adding 8+x extra spaces per line (where x can approach infinity) seems like funny use of space when 2 would do :)

But hey, guessing it's just how it is, hey

Stephen Touset

Just to throw in my two cents:

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

It preserves alignment of keys and values, maintains the symmetry of indentation for lines containing opening parens and closing parens (e.g., flush with the current indentation), and preserves a constant two-space indentation for all circumstances (I can't seem to stand indentation of anything other than exactly two spaces at a time).

Stephen Touset

Ah, yep. That's exactly what @patcon suggested.

Adam Stegman

:+1: to the suggestion by @patcon and @stouset, the extra horizontal space is invaluable, especially if dealing with an 80-character line limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.