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

Rule idea: computed vars over functions with 0 arguments #1653

Open
bobby-fi opened this issue Mar 22, 2024 · 1 comment
Open

Rule idea: computed vars over functions with 0 arguments #1653

bobby-fi opened this issue Mar 22, 2024 · 1 comment

Comments

@bobby-fi
Copy link

have generally always preferred computed variables over functions with 0 arguments, would be awesome to be able to enforce that:

don't prefer:

func foo() -> Bar {
    ...
}

prefer:

var foo: Bar {
    ...
}
@bobby-fi bobby-fi changed the title Enhancement idea: computed vars over functions with 0 arguments Rule idea: computed vars over functions with 0 arguments Mar 22, 2024
@nicklockwood
Copy link
Owner

Unfortunately this isn't really feasible due to the need to change all the call sites. SwiftFormat only operates on a per-file basis, so it wouldn't know if a call to foo() in another file referred to the same function and needs to have the parens removed.

Also, even if it were possible, I'm not sure it's a good idea. If foo() is just retrieving a value then it should be a property, but what about something like this method:

func popLast() -> Element? {  }

Here it's actually mutating the collection and returning the result. It wouldn't make sense to change that to a property.

I suppose SwiftFormat could try to detect if the method name is a verb or noun and infer from that. Might work better as a SwiftLint rule in that case though.

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

No branches or pull requests

2 participants