-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
)
endwhat do you think?
Pros:
- IMO, it looks better.
- Makes diffs easier to read when you're only changing specific arguments of a method call.
- 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:
- Might be considered overly restrictive.
- 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.