-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathsample.swift
111 lines (89 loc) · 2.68 KB
/
sample.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Don't include generated header comments
// MARK: Types and naming
/// Types begin with a capital letter.
struct User {
let name: String
/// if the first letter of an acronym is lowercase, the entire thing should
/// be lowercase.
let json: Any
/// if the first letter of an acronym is uppercase, the entire thing should
/// be uppercase.
static func decode(from json: JSON) -> Self {
Self(json: json)
}
}
/// Use () for void arguments and Void for void return types.
let closure: () -> Void = {
// Do nothing
}
/// When using classes, default to marking them as final.
final class MyClass {
// Empty class
}
/// Use typealias when closures are referenced in multiple places.
typealias CoolClosure = (Int) -> Bool
/// Use aliased parameter names when function parameters are ambiguous.
func yTown(some: Int, withCallback callback: CoolClosure) -> Bool {
callback(some)
}
/// It's OK to use $ variable references if the closure is very short and
/// readability is maintained.
let cool = yTown(5) { $0 == 6 }
/// Use full variable names when closures are more complex.
let cool = yTown(5) { foo in
max(foo, 0)
// …
}
// Strongify weak references in async closures
APIClient.getAwesomeness { [weak self] result in
guard let self else {
return
}
self.stopLoadingSpinner()
self.show(result)
}
/// Use if-let to check for not `nil` (even if using an implicitly unwrapped variable from an API).
func someUnauditedAPI(thing: String?) {
if let thing {
print(thing)
}
}
/// When the type is known you can let the compiler infer.
let response: Response = .success(NSData())
func doSomeWork() -> Response {
let data = Data("", .utf8)
return .success(data)
}
switch response {
case .success(let data):
print("The response returned successfully \(data)")
case .failure(let error):
print("An error occurred: \(error)")
}
// MARK: Organization
/// Group methods into specific extensions for each level of access control.
private extension MyClass {
func doSomethingPrivate() {
// Do something
}
}
// MARK: Breaking up long lines
// One expression to evaluate and short or no return
guard let singleTest = somethingFailable() else {
return
}
guard statementThatShouldBeTrue else {
return
}
// If a guard clause requires multiple lines, chop down, then start `else` new line
// In this case, always chop down else clause.
guard
let oneItem = somethingFailable(),
let secondItem = somethingFailable2()
else {
return
}
// If the return in else is long, move to next line
guard let something = somethingFailable() else {
return someFunctionThatDoesSomethingInManyWordsOrLines()
}