rubocop / rubocop/ruby-style-guide

If the arguments of a method call span more than one line, only allow one argument per line

Open
#968 2 comments 0 reactions 0 assignees View on GitHub

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

I wonder what people would think of extending Method Arguments Alignment (or perhaps creating a new guideline) to include the following rule: If the arguments of a method call span more than one line, only allow one argument per line.

Here are some code examples based on the Method Arguments Alignment guideline I linked above:

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

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

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

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

what do you think?

Pros:

  1. IMO, it looks better.
  2. Makes diffs easier to read when you're only changing specific arguments of a method call.
  3. If turned into an autocorrectable cop/configuration option in RuboCop, it could improve the results of formatting with the Layout/LineLength cop. Right now when a method call extends past the line length character limit, oftentimes it will cut off the last argument and put it on the next line, with all the other arguments still on the original line, which looks awkward, especially with DSLs.

Cons:

  1. Might be considered overly restrictive.
  2. If you have a method which takes a ton of arguments (probably too many), then it might be nice to not have the method call take up too many lines. (that said, in this case, I would still prefer each argument on its own line to make diffs easier to read)

Sidenote - We could also add corresponding rules for Array and Hash alignment, but I wanted to start with this one since I wasn't 100% sure about those.


I was originally planning on opening an issue in https://github.com/rubocop/rubocop, but after looking through some of the feature request issue there it seems like this is the better repository to start in. Let me know if it would be better to raise this issue as a new cop idea in RuboCop instead.

Also, even if we decide that this isn't the right fit for the ruby style guide, we could still maybe add a configuration option to Layout/ArgumentAlignment which is disabled by default, or a new cop which is disabled by default, which allows users to enforce this this.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the Method Arguments Alignment section of the Ruby Style Guide and the linked Layout/ArgumentAlignment documentation. Compare the proposed examples with the existing guidance, including the possible array and hash rules, and determine whether this belongs in the style guide or as a RuboCop cop or configuration option. Done requires an agreed rule and documented scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
documentation
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.