Updated a section about early exits · github/swift-style-guide@47d4070 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 47d4070

Browse files
committed
Updated a section about early exits
1 parent 49239db commit 47d4070

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

README.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,27 @@ It becomes easier to reason about code. Had you used `var` while still making th
3434

3535
Accordingly, whenever you see a `var` identifier being used, assume that it will change and ask yourself why.
3636

37-
#### Use `guard` statements to exit early
37+
### Return and break early
3838

39-
If you have to meet certain criteria to continue execution, prefer `guard` over `if`
39+
When you have to meet certain criteria to continue execution, try to exit early. So, instead of this:
4040

41-
So, use this:
4241
```swift
43-
guard let foo = foo else {
42+
if n.isNumber {
43+
// Use n here
44+
} else {
4445
return
4546
}
46-
// Use unwrapped `foo` value in here
4747
```
48-
Instead of this:
48+
49+
use this:
4950
```swift
50-
if let foo = foo {
51-
// Use unwrapped `foo` value in here
52-
} else {
51+
guard n.isNumber else {
5352
return
5453
}
54+
// Use n here
5555
```
5656

57-
_Rationale:_ Intention and criteria are clearly visible, leading to lower chance of programmer error
57+
You can also do it with `if` statement, but using `guard` is prefered, because `guard` statement without `return`, `break` or `continue` produces a compile-time error, so exit is guaranteed.
5858

5959
#### Avoid Using Force-Unwrapping of Optionals
6060

@@ -94,7 +94,7 @@ So, write these:
9494

9595
```swift
9696
var myGreatProperty: Int {
97-
return 4
97+
return 4
9898
}
9999

100100
subscript(index: Int) -> T {
@@ -106,9 +106,9 @@ subscript(index: Int) -> T {
106106

107107
```swift
108108
var myGreatProperty: Int {
109-
get {
110-
return 4
111-
}
109+
get {
110+
return 4
111+
}
112112
}
113113

114114
subscript(index: Int) -> T {
@@ -134,7 +134,7 @@ However, definitions within those can leave access control implicit, where appro
134134

135135
```swift
136136
internal struct TheFez {
137-
var owner: Person = Joshaber()
137+
var owner: Person = Joshaber()
138138
}
139139
```
140140

@@ -169,27 +169,27 @@ When accessing properties or methods on `self`, leave the reference to `self` im
169169

170170
```swift
171171
private class History {
172-
var events: [Event]
172+
var events: [Event]
173173

174-
func rewrite() {
175-
events = []
176-
}
174+
func rewrite() {
175+
events = []
176+
}
177177
}
178178
```
179179

180180
Only include the explicit keyword when required by the language—for example, in a closure, or when parameter names conflict:
181181

182182
```swift
183183
extension History {
184-
init(events: [Event]) {
185-
self.events = events
186-
}
187-
188-
var whenVictorious: () -> () {
189-
return {
190-
self.rewrite()
191-
}
192-
}
184+
init(events: [Event]) {
185+
self.events = events
186+
}
187+
188+
var whenVictorious: () -> () {
189+
return {
190+
self.rewrite()
191+
}
192+
}
193193
}
194194
```
195195

@@ -264,21 +264,21 @@ Methods of parameterized types can omit type parameters on the receiving type wh
264264

265265
```swift
266266
struct Composite<T> {
267-
268-
func compose(other: Composite<T>) -> Composite<T> {
269-
return Composite<T>(self, other)
270-
}
267+
268+
func compose(other: Composite<T>) -> Composite<T> {
269+
return Composite<T>(self, other)
270+
}
271271
}
272272
```
273273

274274
could be rendered as:
275275

276276
```swift
277277
struct Composite<T> {
278-
279-
func compose(other: Composite) -> Composite {
280-
return Composite(self, other)
281-
}
278+
279+
func compose(other: Composite) -> Composite {
280+
return Composite(self, other)
281+
}
282282
}
283283
```
284284

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.