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

For long function declarations, add newline before return arrow as well #17

Merged
merged 5 commits into from Oct 19, 2018

Conversation

teradyl
Copy link
Contributor

@teradyl teradyl commented Sep 10, 2018

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.

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)
Copy link
Collaborator

@fdiaz fdiaz Sep 11, 2018

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"?

@thedrick
Copy link

Does the vertical_parameter_alignment_on_call swift lint rule actually fix this completely? My guess is that it won't change

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

@fdiaz
Copy link
Collaborator

fdiaz commented Sep 11, 2018

@thedrick It won't. This PR isn't adding any SwiftLint rules. Both of those examples are valid for the linter right now.

@thedrick
Copy link

@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.

@fdiaz
Copy link
Collaborator

fdiaz commented Sep 11, 2018

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?

@thedrick
Copy link

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.

@bachand
Copy link
Contributor

bachand commented Sep 12, 2018

That's a good question @thedrick.

@teradyl do you think we could add a migration plan to the PR message? What do you think @fdiaz ?

@bachand
Copy link
Contributor

bachand commented Sep 12, 2018

I was trying to do this with sed via find, but ran into trouble with the newlines. Here's where I got (this is the identity function, making no changes).

find . -type f -name "*.swift" -exec sed -i '' 's/\(func.*\)->/\1->/g' {} \;

@fdiaz
Copy link
Collaborator

fdiaz commented Sep 12, 2018

do you think we could add a migration plan to the PR message?

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).

@teradyl
Copy link
Contributor Author

teradyl commented Sep 12, 2018

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.

@thedrick
Copy link

@teradyl well put, you've convinced me and I've changed my 👎to a 👍:)

@swiftal64
Copy link
Contributor

swiftal64 commented Sep 14, 2018

@teradyl could we collaborate on the regex that needs to be created to fix all instances?

I started one here: https://regex101.com/r/h7qzfy/1 https://regex101.com/r/h7qzfy/2

If anyone wants to add complex examples to that regex101, we could work on this regex together

@teradyl
Copy link
Contributor Author

teradyl commented Sep 17, 2018

@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 throws keyword that can be there, so I added examples with that.

@fdiaz We can merge this on Wednesday correct?

I can send out a PSA for people to start working this style into their workflows.

@fdiaz
Copy link
Collaborator

fdiaz commented Sep 18, 2018

@fdiaz We can merge this on Wednesday correct?

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.

@fdiaz
Copy link
Collaborator

fdiaz commented Sep 20, 2018

Resolution

Status: 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
Copy link
Contributor

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

Copy link
Contributor Author

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)
Copy link
Contributor

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

@fdiaz fdiaz force-pushed the dkh--newline-for-return-value-on-function branch from ce89a64 to a0b8dc0 Compare October 10, 2018 15:52
@fdiaz
Copy link
Collaborator

fdiaz commented Oct 10, 2018

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

@teradyl teradyl force-pushed the dkh--newline-for-return-value-on-function branch from a0b8dc0 to 57f0185 Compare October 11, 2018 22:05
@teradyl
Copy link
Contributor Author

teradyl commented Oct 11, 2018

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|->)
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

@dfed dfed Oct 19, 2018

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.

Copy link
Contributor

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go team

@fdiaz
Copy link
Collaborator

fdiaz commented Oct 19, 2018

Resolution

Status: Approved ✅

Reasoning:

This had an overwhelming positive reaction from the team.

@fdiaz fdiaz merged commit ee9ec10 into master Oct 19, 2018
@fdiaz fdiaz deleted the dkh--newline-for-return-value-on-function branch October 19, 2018 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants