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
For long function declarations, add newline before return arrow as well #17
Conversation
README.md
Outdated
@@ -334,7 +334,7 @@ _You can enable the following settings in Xcode by running [this script](resourc | |||
|
|||
</details> | |||
|
|||
* <a id='long-function-declaration'></a>(<a href='#long-function-declaration'>link</a>) **Separate [long](#column-width) function declarations with line breaks before each argument label.** Put the open curly brace on the next line so the first executable line doesn't look like it's another parameter. SwiftLint: [`multiline_parameters`](https://github.com/realm/SwiftLint/blob/master/Rules.md#multiline-parameters), [`vertical_parameter_alignment_on_call`](https://github.com/realm/SwiftLint/blob/master/Rules.md#vertical-parameter-alignment-on-call) | |||
* <a id='long-function-declaration'></a>(<a href='#long-function-declaration'>link</a>) **Separate [long](#column-width) function declarations with line breaks before each argument label and before return arrow.** Put the open curly brace on the next line so the first executable line doesn't look like it's another parameter. SwiftLint: [`multiline_parameters`](https://github.com/realm/SwiftLint/blob/master/Rules.md#multiline-parameters), [`vertical_parameter_alignment_on_call`](https://github.com/realm/SwiftLint/blob/master/Rules.md#vertical-parameter-alignment-on-call) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be "and before the return arrow"?
Does the func generateStars(
at location: Point,
count: Int,
color: StarColor,
withAverageDistance averageDistance: Float) -> String
{
populateUniverse()
} to func generateStars(
at location: Point,
count: Int,
color: StarColor,
withAverageDistance averageDistance: Float)
-> String
{
populateUniverse()
} which I think makes this a little confusing |
@thedrick It won't. This PR isn't adding any SwiftLint rules. Both of those examples are valid for the linter right now. |
@fdiaz what is our policy on rules like this that aren't easily corrected by the linter? The Airbnb codebase doesn't currently follow this proposal and it would take a lot of work to manually update it to do so. |
I don't think we have one. I'd be extra cautious to accept changes that aren't currently adopted in our codebase without consulting with the team (which is what I'll be doing this Friday on the internal newsletter). I'd hope more people would comment after that, but in general for breaking changes like this I'd expect a bigger acceptance rate to approve than for something we already use in our codebase. Does that make sense? |
Yeah that makes sense to me. I think I actually like this proposal, but I worry it won't be feasible for us to adopt it in our codebase wholesale which will leave us in a weird state where some of the code uses this new style and most of our code uses the old style. If we had a script or lint rule that could auto-correct this for everything I'd be more on board with this addition. |
I was trying to do this with find . -type f -name "*.swift" -exec sed -i '' 's/\(func.*\)->/\1->/g' {} \; |
I would advice against it. This style guide should be decoupled from our own codebase IMO. But I'd think it's also reasonable to require an autofix SwiftLint rule for this too given this is not our current adopted pattern at all (as in, this will need to change all usages). |
On the one hand, I understand that we don't want to accept a style guide rule that we don't have applied and implemented yet. On the other hand, it's hard to proceed with a better style if we don't have anything in the style guide that guides us towards a better future. If someone wants to create an autofix SwiftLint rule for this, that would solve the problem, but I think that's a non-trivial amount of work, and if no one wants to do that we'll be stuck in limbo with old style. I would like to see rules like this go through (if everyone indeed agrees it's a good style idea) so new and updated code can in fact be written to a better style. Our code already has violations of the style guide that are slowly updated, and this will just be another (albeit more common at first) instance of that. I would urge people to rate this rule notwithstanding the current state of our codebase. |
@teradyl well put, you've convinced me and I've changed my 👎to a 👍:) |
@teradyl could we collaborate on the regex that needs to be created to fix all instances? I started one here: If anyone wants to add complex examples to that regex101, we could work on this regex together |
@swift-ortal Thanks for making that regex! Looks good. I think the more annoying part is integrating it into SwiftLint. You did however remind me of the @fdiaz We can merge this on Wednesday correct? I can send out a PSA for people to start working this style into their workflows. |
I'm planning on approving this tomorrow but leave it open until we can add a SwiftLint rule so we can lint for it. SwiftLint does allow regex so if we're comfortable with that regex we can just use that but it'd be cool if we can also add a SwiftLint rule so we can contribute back to the community and it would be easier for people that want to adopt the same rule. |
ResolutionStatus: Approved ✅ / Waiting for SwiftLint rule to be merged.Reasoning:This had an overwhelming positive reaction from the team. There were some concerns regarding linting that should be solved by providing a SwiftLint rule before we merge but this is approved. |
color: StarColor, | ||
withAverageDistance averageDistance: Float) throws | ||
-> String { | ||
populateUniverse() // this line blends in with the argument list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can delete the comment // this line blends in with the argument list
on this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that comment still makes sense. We still want to make sure that we put the open curly brace on the next line.
@@ -334,7 +334,7 @@ _You can enable the following settings in Xcode by running [this script](resourc | |||
|
|||
</details> | |||
|
|||
* <a id='long-function-declaration'></a>(<a href='#long-function-declaration'>link</a>) **Separate [long](#column-width) function declarations with line breaks before each argument label.** Put the open curly brace on the next line so the first executable line doesn't look like it's another parameter. SwiftLint: [`multiline_parameters`](https://github.com/realm/SwiftLint/blob/master/Rules.md#multiline-parameters), [`vertical_parameter_alignment_on_call`](https://github.com/realm/SwiftLint/blob/master/Rules.md#vertical-parameter-alignment-on-call) | |||
* <a id='long-function-declaration'></a>(<a href='#long-function-declaration'>link</a>) **Separate [long](#column-width) function declarations with line breaks before each argument label and before the return signature.** Put the open curly brace on the next line so the first executable line doesn't look like it's another parameter. SwiftLint: [`multiline_parameters`](https://github.com/realm/SwiftLint/blob/master/Rules.md#multiline-parameters), [`vertical_parameter_alignment_on_call`](https://github.com/realm/SwiftLint/blob/master/Rules.md#vertical-parameter-alignment-on-call) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll want to convert these swiftlint links to shields.io badges that the style guide now uses
ce89a64
to
a0b8dc0
Compare
Just rebased to resolve conflicts and move this branch forward. Whenever the regex is ready we can add it to our swiftlint.yml file as a custom rule and then we can merge this. cc @teradyl @swift-ortal |
a0b8dc0
to
57f0185
Compare
Thanks @swift-ortal for helping me with the regex. I rebased and put the regex in the swiftlint. Please take a look. |
|
||
long_function_return_arrow_newline: | ||
name: "long_function_return_arrow_newline" | ||
regex: (\bfunc [^(]+\(\n[^)]+?\)) *(throws\b|->) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any way we could comment this regex? This will be hard to maintain as-is.
Also, I'm curious: how does this only affect long lines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dfed It's easier to just direct you at the regex link where Ortal & I created it so you can see for yourself the examples where it triggers and not, along with explanations for each part of the regex. https://regex101.com/r/h7qzfy/3
I updated the message in swiftlint to include that link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will check it out later today. In the meantime, could we link to that site in a comment? Keeping that info alive will help future maintainers.
Edit: You said you already did that! Thanks 😄. Not sure how I missed that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future I wonder if we should put the regex link as a code comment. Seems useful for maintainers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bachand It is added in the message https://github.com/airbnb/swift/pull/17/files#diff-7a1b543d4d8e0f3369a51e6e049c47d7R52 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Sorry I missed that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go team
ResolutionStatus: Approved ✅Reasoning:This had an overwhelming positive reaction from the team. |
Summary
Function declarations sometimes have long unwieldy lines with the return type. This can fix that problem.
This is the updated version of: #15
Reasoning
Stay within the 100 character limit. By doing this on ALL long function declarations, we keep some consistency with where to find the return type.
Clarification: One-line functions with return signatures that are under the 100 character limit should still be in one line.
Reviewers
cc @airbnb/swift-styleguide-maintainers
Please react with 👍/👎 if you agree or disagree with this proposal.