Skip to content

Omit explicit returns where possible #2146

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

Conversation

natikgadzhi
Copy link
Contributor

Motivation

A separate formatting PR to make space for @Matejkob and @ahoppen and @kimdv to review how our codebase would look with omitting return statements where it's possible, a follow-up to #2136. Since this topic might spawn a longer discussion, I've put that rule in a separate PR.

Changes

@kimdv
Copy link
Contributor

kimdv commented Sep 3, 2023

Personally I'm not in favor of omitting returns.
I think it makes it harder to read code.
I known that the SwiftSyntaxBuilder in code gen don't use, just like SwiftUI. But for me it makes sense and is fine.

But a normal variable or function need a return IMO

@natikgadzhi
Copy link
Contributor Author

I'm only a beginner in Swift, but so far omitting returns feels good in:

  • one-line closures
  • result builders
  • one-line / one-statement dynamic property bodies
  • perhaps functions that don't have any control flow in them and are very short (one statement)

But, I agree that making this a mandated format might not work well in situations where explicit return keywords make the code much more readable.

While a lot of swift-syntax code are small functions, and they look pretty nice without return to a newcomer like myself, I don't have a strong opinion, and don't have the experience to back up my opinions even if I had one.

So, that PR is for demonstration purposes. If we chose to enable the rule, we can do so at any point in the future.

@xedin
Copy link
Contributor

xedin commented Sep 5, 2023

Just a general note about return omission. As per proposal, return statements could only be omitted if body constitutes of a single expression, anything more context always requires an explicit return.

@natikgadzhi
Copy link
Contributor Author

@ahoppen, I think we should probably close this for now? We know this happened, and if we want to return to that — the PR will be here. @Matejkob, good with you too?

@Matejkob
Copy link
Contributor

Matejkob commented Sep 8, 2023

I would personally advocate for merging. I might be a bit biased since I'm accustomed to omitting the return keyword in my projects. I also appreciate the consistency it brings. We already have numerous instances where return is omitted, so implementing this rule would contribute to overall codebase consistency. However, I'm just a random individual and don't really feel in a position to strongly push for my approach.

@natikgadzhi
Copy link
Contributor Author

However, I'm just a random individual and don't really feel in a position to strongly push for my approach.

Big same here.

This commit sets up `.swift-format` rule to omit returns.
@natikgadzhi natikgadzhi force-pushed the internal/swift-format/omit-returns branch from 40ab696 to aea4ba6 Compare September 8, 2023 18:23
@natikgadzhi
Copy link
Contributor Author

I've rebased this PR so we can review this with ordered imports and other improvements merged in.

It's growing on me, to be honest. Should I perhaps start a topic in swift forums to pitch this and get more feedback from the community, @ahoppen?

@ahoppen
Copy link
Member

ahoppen commented Sep 8, 2023

I’ll gather some feedback on this internally and come back to you in the next week-ish. I’m also very hesitant to merge this as-is.

@ahoppen
Copy link
Member

ahoppen commented Sep 27, 2023

I just had another conversation about this and we don’t want this change for now. If there was an official Swift API design guideline to follow regarding when to omit return, we should definitely adopt it in swift-syntax but for now we believe that enforcing the omission of return in general makes the code less readable.

@ahoppen ahoppen closed this Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants