nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.63k stars 623 forks source link

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

Open bobby-fi opened 3 months ago

bobby-fi commented 3 months ago

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 {
    ...
}
nicklockwood commented 3 months ago

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.