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

Best practice for wrapping function arguments #532

Open
logashoff opened this issue Sep 30, 2015 · 11 comments
Open

Best practice for wrapping function arguments #532

logashoff opened this issue Sep 30, 2015 · 11 comments
Labels

Comments

@logashoff
Copy link

Style guide should advise wrapping function arguments that go over maximum line length.

function someVeryLongFunctionName(someVeryLongArgumentName1, someVeryLongArgumentName2, someVeryLongArgumentName3) {
  // Function body
}

or:

function myFunction(someArg1, someArg2, someArg3, someArg4, someArg5, someArg6, someArg7, someArg8) {
  // Function body
}

Wrapped:

function myFunction(someArg1, someArg2, someArg3, someArg4,
                    someArg5, someArg6, someArg7, someArg8) {
  // Function body
}
@elliotchance
Copy link

Is there any word on this? Compared to alternative which is common in PHP's PSR to chop down like:

function myFunction(
  someVeryLongArgumentName1,
  someVeryLongArgumentName2,
  someVeryLongArgumentName3
) {
  // Function body
}

It's better for version control to have one argument per line (the same reason we have trailing commas), is easer to read and is more friendly with default arguments.

Thoughts?

@goatslacker
Copy link
Collaborator

@logashoff
Copy link
Author

@elliotchance, @goatslacker Either of those is fine in my opinion.

I had Google JavaScript Style Guide in mind when I've created this issue. But Yandex approach seems cleaner. Which is similar to what you've mentioned.

@tleunen
Copy link

tleunen commented Nov 10, 2015

They should probably warn over the number of arguments instead of the number of characters.

@ljharb
Copy link
Collaborator

ljharb commented Nov 10, 2015

Yes - imo the problem there is that you have such a long function signature, not that it's longer than some arbitrary line length. The style guide should advise that you don't do that, instead of advising you how to paper over a bad pattern.

@logashoff
Copy link
Author

@ljharb Agree, but it's difficult to avoid when using something like Angular with its heavy use of dependency injections. Dependency injections are also used as module imports.

@jacobp100
Copy link

Feedback on this would be good—when using flow, the functions with relatively few arguments easily exceed the 100 character limit set by the style guide.

@ljharb
Copy link
Collaborator

ljharb commented Feb 2, 2016

Since flow isn't a standard part of JS, this guide won't be addressing it. However, #532 (comment) seems like the least objectionable option to me.

@jmlopez-rod
Copy link

@ljharb Will #532 this make it into the style? Regardless of the framework in some cases it is very easy to exceed the character limit. Take for instance this silly example

class A {
  someVeryDescriptiveMethodAboutSomething(thisIsTheFirstParam, defaultUrl = 'https://github.com/airbnb/javascript/issues/532') {
    // ...
  }
}

applying the rule makes it more readable

class A {
  someVeryDescriptiveMethodAboutSomething(
    thisIsTheFirstParam,
    defaultUrl = 'https://github.com/airbnb/javascript/issues/532'
  ) {
    // ...
  }
}

and as @elliotchance mentioned it is better for version control. Other typed languages do have to address this issue since adding a type to an argument already takes a lot of space. Take Scala for example and the convention databricks adopted.

@ljharb
Copy link
Collaborator

ljharb commented Sep 30, 2016

Yes, I think that's a section we need to add - and since trailing function commas are now supported by eslint, we'd require those as well.

In other words, a section that simultaneously discourages long function signatures/calls, but describes the proper way to indent them multiline with trailing commas, would be a welcome PR.

@jakubbarczyk
Copy link

I suggest watching any talk by Kevlin Henney related to code formatting, noise and style. May be an eye-opener to some developers out there.

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

No branches or pull requests

8 participants