I don't want to debate the one versus the other. There's big long threads that do that already. I'm pretty confident that the implicit self'ers are in the majority, but swiftformat options like "--self insert" make me think there's some small group of explicit'ers. I was curious how many (I recognize that is a pretty ad hoc polling location). Which do you do?
- Implicit When Possible
- Explicit Always
Karl
(👑🦆)
2
My preference is:
Explicit in initialisers, or when accessing generically-named data (e.g. count, length) from within a large, complicated function. Otherwise implicit.
4 Likes
Explicit self everywhere where required, or in some rare cases for disambiguation and in every init. Other than that I rely on the implicit self.
2 Likes
[In case you didn't know] You can do a poll in discourse using the 'Build Poll' option, that way people can answer your question without leaving comments for one or the other.
wbrian
(W. Brian Gourlie)
6
I use implicit self where possible, but only because AppCode orange-squigglies unneeded self qualifiers (I should probably figure out how to disable that lint).
On the other hand, I'm not sure how I feel about implicit return. I wish it was similar to rust where the last expression in a given block is implicitly returned, although optional semicolons in Swift conflict with that desire.
tera
7
implicit everywhere including escaping closures!
("explicit use of 'self' to make capture semantics explicit" doesn't make it "explicit"! what makes capture semantics explicit is capturing self in the closure capture list.)
mickeyl
(Dr. Mickey Lauer)
8
Explicit everywhere. Made a habit thanks to decades of Python programming.
3 Likes
Explicit everywhere. Started with implicit everywhere thinking it would help call out where self was actually required and help identify retain cycles, but in practice found that readability, especially in pull requests, was greatly improved with explicit self.
11 Likes
Agreed. I especially find it super confusing when people call a method using implicit self when they're inside the same type. For example:
struct Bar {
func foo() {
baz() // should be self.baz() for clarity
}
// .....
func baz() { /* ... */ }
}
1 Like
It depends on the method. I find that things like formIndex(after:) read just fine with implicit self, but things like sorted() really want explicit self.
Heaney
13
I like to be explicit where I can in my own personal projects, but when I’m working on larger code bases with multiple people, the project convention usually seems to be implicit where you can.
Being implicit keeps things lightweight, but I like the extra readability explicitly using self can give.
I’m enjoying seeing what others are posting on this!
1 Like