8000 Merge pull request #4 from github/jspahrsummers-patch-1 · smashcoding/swift-style-guide@cdae9ae · GitHub
[go: up one dir, main page]

Skip to content

Commit cdae9ae

Browse files
committed
Merge pull request github#4 from github/jspahrsummers-patch-1
Only explicitly refer to `self` when required
2 parents a5d2a34 + 15613d4 commit cdae9ae

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,40 @@ However, definitions within those can leave access control implicit, where appro
4747

4848
```swift
4949
internal struct TheFez {
50-
var owner: Person = Joshaber()
50+
var owner: Person = Joshaber()
5151
}
5252
```
5353

5454
_Rationale:_ It's rarely appropriate for top-level definitions to be specifically `internal`, and being explicit ensures that careful thought goes into that decision. Within a definition, reusing the same access control specifier is just duplicative, and the default is usually reasonable.
55+
56+
#### Only explicitly refer to `self` when required
57+
58+
When accessing properties or methods on `self`, leave the reference to `self` implicit by default:
59+
60+
```swift
61+
private class History {
62+
var events: [Event]
63+
64+
func rewrite() {
65+
events = []
66+
}
67+
}
68+
```
69+
70+
Only include the explicit keyword when required by the language—for example, in a closure, or when parameter names conflict:
71+
72+
```swift
73+
extension History {
74+
init(events: [Event]) {
75+
self.events = events
76+
}
77+
78+
var whenVictorious: () -> () {
79+
return {
80+
self.rewrite()
81+
}
82+
}
83+
}
84+
```
85+
86+
_Rationale:_ This makes the capturing semantics of `self` stand out more in closures, and avoids verbosity elsewhere.

0 commit comments

Comments
 (0)
0