-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Enforce explicit self #321
Comments
I'd love this, even if it's not enabled by default. |
Yeah I'm assuming people would cherry-pick it into their SwiftLint config file only if they wanted it enforced in their project. Unfortunately I'm not yet familiar enough with SwiftLint myself so I'm hoping for someone experienced to at least chime in about how difficult it'd be to add. (Also I'm studying for exams atm, so this time right here officially counts as procrastination.) |
We also have this in our style guide and would love to see this in swiftlint. I'll look into an approach for this today. |
I'd love to see this as an opt-in rule. |
Are opt-in rules already supported by SwiftLint? |
No they're not. That's something that's come up multiple times before and On Wed, Jan 6, 2016 at 08:26 Honza Dvorsky notifications@github.com wrote:
|
Understood. Does that mean that this issue will be blocked until opt-in rules are implemented? I'd be happy to use the rule from a feature branch in the meantime if so. |
It definitely would be. We wouldn't be able to ship this on by default. |
It would be trivial to introduce opt-in rules.
|
Just to throw in some ideas for the configurable elements:
I'd personally enforce EDIT: Maybe the |
@AliSoftware, what advantages do you see with the rule modes vs. the current approach of FWIW, I don't like the idea of separating properties vs. method calls in their enforcement of |
@scottrhoyt i don't think If you don't list the If you do list the But what if the convention on your team is to NOT use self, and so you want SwiftLint to generate a warning if there is a self whereas it wasn't needed? With |
Isn't that a completely different rule? I understand they both touch self,
|
@czechboy0 But then we'll have two conflicting rules, which seems strange especially whereas they treat the exact same problem. So their implementation will probably be the same, just a And also with 2 conflicting rules instead of one configurable unified rule, what if the user opt in for both rules, |
Yes they'd conflict if you put them both in, but I don't really see that as Maybe we're just over engineering this. I think a completely simple, naive
|
WFM! :) |
Is there any update on this? I have tried looking into the source code, but can't figure out how to implement this rule. Any ideas? |
Let me know if I've missed any test cases 👍 |
@IanKeen - looks amazing! BTW would it be possible to add an option not to apply this rule on variables with a I don't think I'm alone who prefixes private instance variables with underscore, which lets you easily identify them in the code - for those I don't mind omitting the explicit self since local scope variables are not prefixed with an underscore so any bugs resulting from shadowing are unlikely. Example:
|
@charlieMonroe Interesting. I would use |
🤔 I'm inclined to agree with @3lvis - this seems a counter intuitive practise to what adding But this wouldn't be hard to add if people want it 😄 |
The whole rationale behind the explicit This may be my old habits from ObjC, though:
You can potentially address As they say - old habits die hard... Sure, I could fork this project and from what I've looked at the code, it's a one-line change, but I thought perhaps more people would benefit from this. |
Yeah it is a simple addition. I've got a few cases to cater for which I'm On Friday, October 7, 2016, charlieMonroe notifications@github.com wrote:
|
Thanks, Ian! |
Yeah, what some of you have described is not general enough to make it part of the rule for everyone in my opinion 👎 |
If anything, making an exception for underscored members should be either a rule configuration option or not supported at all. |
One thing to keep in mind is that Swift and SwiftLint can be used on Server Side Swift and non Cocoa environments. Following Cocoa standards, like underscore prefixes (which was short of a hack to hide private properties and methods) should be discouraged by default. |
@eneko - I honestly do not see why. When I come to someone else's code and see an identifier starting with an underscore, I immediately know that it's a private instance member. Otherwise you need to look it up, mainly when one doesn't use explicit self. E.g. in git diffs, I find this very useful. I personally think that it improves readability of the code and it visibly separates private members from public. But I fully understand that you have a different opinion on this matter - code style is often something very personal and can be a source of endless fights. Nevertheless, I don't think that "should be discouraged by default" is absolutely true - is there any particular reason on your mind? If such a coding style makes sense to one and helps him more easily understand the code, why not? Since the identifiers are not public, the API doesn't get exported, so I don't see any harm - I personally only see benefits. |
Languages and frameworks over the years develop habits. Some are good and some are bad. Just because we were used to do things like that in Objective-C does not mean we should follow the same practices in Swift (even if the platform we are developing on is the same). Using prefixes or suffixes to indicate the visibility of methods or properties is a bad idea because:
In summary, I does not belong to SwiftLint to encourage or support bad programming practices, even if those rules were optional. |
@eneko Using underscore prefixes on names to denote privacy was not invented by Apple and is not even remotely exclusive to Objective-C or Cocoa. It's a not-uncommon practice in Javascript, Python, Ruby, C, C++, C#, Perl, Java... It's also used regularly by the Swift stdlib itself. See, e.g. https://github.com/apple/swift/blob/master/stdlib/public/core/ArrayBody.swift#L21 |
A lot of stuff isn't. A lot of bugs could have been prevented by explicit self in my codebase. Just because it's not checked by the compiler doesn't make it a wrong practice. When you forget to add the
I'm sorry, could you elaborate?
In one post, you mention that "Swift and SwiftLint can be used on Server Side Swift and non Cocoa environments" and then you mention an Xcode feature (that works about 20% of the time in my experience). IDE is not (and shouldn't be) part of the language nor code style. Not everyone is using Xcode (Linux) and in many cases, you look into diffs on Github/elsewhere for code review.
So you'd prefer
This dates waaaay before properties. I've started with ObjC a few years before ObjC 2.0. |
Let's please keep it civil in here. SwiftLint's role is to help make projects a bit more stylistically consistent, rather than enforce one philosophy over another. Since there's clearly some differences of opinion, we should strive to make SwiftLint accommodate both these cases and move on. I think this can reasonably be done by making an "explicit self" rule configurable on whether or not underscore-prefixed members should be exempt. |
Any progress on this? I'd love to force |
Is it possible to do this in a class extension that's in a different file? Does SwiftLint work file-by-file, or can it handle things across files? |
How's this looking? My team has agreed to omit the implied |
I would also like to see an option to only enforce To illustrate, I would like to enforce: class Unicorn {
init(foo: String, goat: String) {
self.foo = foo
bar = goat
}
} To be: class Unicorn {
init(foo: String, goat: String) {
self.foo = foo
self.bar = goat
}
} Continued in #2436. |
This was built in #2379. |
Due to the Swift evolution proposal 009 being rejected, but still having a pretty big following, I think it'd be very valuable for certain teams to be able to enforce explicit self (like in Objective-C). Here's the reasoning for not changing the language from the core team and here's the proposal itself, where you can find reasons why the proposed change makes sense for some teams.
The gist of the decision was that explicit self makes sense for a certain group of people and those should use a lint tool (suggested by the core Swift team) instead of the language being changed. Thus I think SwiftLint supporting this rule would help it to be used by all those devs.
The text was updated successfully, but these errors were encountered: