Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 49239db

Browse files
committed
Added a section about guard statement
1 parent b71dd2b commit 49239db

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ 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
38+
39+
If you have to meet certain criteria to continue execution, prefer `guard` over `if`
40+
41+
So, use this:
42+
```swift
43+
guard let foo = foo else {
44+
return
45+
}
46+
// Use unwrapped `foo` value in here
47+
```
48+
Instead of this:
49+
```swift
50+
if let foo = foo {
51+
// Use unwrapped `foo` value in here
52+
} else {
53+
return
54+
}
55+
```
56+
57+
_Rationale:_ Intention and criteria are clearly visible, leading to lower chance of programmer error
58+
3759
#### Avoid Using Force-Unwrapping of Optionals
3860

3961
If you have an identifier `foo` of type `FooType?` or `FooType!`, don't force-unwrap it to get to the underlying value (`foo!`) if possible.

0 commit comments

Comments
 (0)