opencode-skills-collection 3.0.49 → 3.0.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundled-skills/.antigravity-install-manifest.json +27 -1
- package/bundled-skills/ab-test-setup/SKILL.md +14 -0
- package/bundled-skills/apple-notes-search/SKILL.md +122 -0
- package/bundled-skills/astropy/references/coordinates.md +9 -0
- package/bundled-skills/astropy/references/cosmology.md +8 -0
- package/bundled-skills/astropy/references/fits.md +8 -0
- package/bundled-skills/astropy/references/tables.md +8 -0
- package/bundled-skills/astropy/references/time.md +7 -0
- package/bundled-skills/astropy/references/units.md +9 -0
- package/bundled-skills/astropy/references/wcs_and_other_modules.md +9 -0
- package/bundled-skills/browser-extension-builder/SKILL.md +1 -1
- package/bundled-skills/ckw-design/SKILL.md +129 -0
- package/bundled-skills/deterministic-design/SKILL.md +56 -0
- package/bundled-skills/docs/integrations/jetski-cortex.md +3 -3
- package/bundled-skills/docs/integrations/jetski-gemini-loader/README.md +1 -1
- package/bundled-skills/docs/maintainers/repo-growth-seo.md +3 -3
- package/bundled-skills/docs/maintainers/skills-update-guide.md +1 -1
- package/bundled-skills/docs/users/bundles.md +1 -1
- package/bundled-skills/docs/users/claude-code-skills.md +1 -1
- package/bundled-skills/docs/users/gemini-cli-skills.md +1 -1
- package/bundled-skills/docs/users/getting-started.md +1 -1
- package/bundled-skills/docs/users/kiro-integration.md +1 -1
- package/bundled-skills/docs/users/usage.md +4 -4
- package/bundled-skills/docs/users/visual-guide.md +4 -4
- package/bundled-skills/lint-and-validate/SKILL.md +3 -1
- package/bundled-skills/lookdev/SKILL.md +229 -0
- package/bundled-skills/lookdev-auto/SKILL.md +102 -0
- package/bundled-skills/macos-screen-recorder/SKILL.md +59 -0
- package/bundled-skills/pr-merge-champion/SKILL.md +116 -0
- package/bundled-skills/programmatic-seo/SKILL.md +11 -0
- package/bundled-skills/schema-markup/SKILL.md +11 -0
- package/bundled-skills/screenstudio-alt/SKILL.md +91 -0
- package/bundled-skills/super-code/SKILL.md +209 -0
- package/bundled-skills/super-code/bash/SKILL.md +292 -0
- package/bundled-skills/super-code/c/SKILL.md +263 -0
- package/bundled-skills/super-code/cpp/SKILL.md +271 -0
- package/bundled-skills/super-code/csharp/SKILL.md +276 -0
- package/bundled-skills/super-code/dart/SKILL.md +327 -0
- package/bundled-skills/super-code/elixir/SKILL.md +366 -0
- package/bundled-skills/super-code/go/SKILL.md +234 -0
- package/bundled-skills/super-code/java/SKILL.md +230 -0
- package/bundled-skills/super-code/kotlin/SKILL.md +281 -0
- package/bundled-skills/super-code/php/SKILL.md +316 -0
- package/bundled-skills/super-code/python/SKILL.md +315 -0
- package/bundled-skills/super-code/ruby/SKILL.md +306 -0
- package/bundled-skills/super-code/rust/SKILL.md +289 -0
- package/bundled-skills/super-code/scala/SKILL.md +302 -0
- package/bundled-skills/super-code/swift/SKILL.md +299 -0
- package/bundled-skills/super-code/typescript/SKILL.md +286 -0
- package/bundled-skills/web-media-getter/SKILL.md +119 -0
- package/bundled-skills/youtube-seo-optimizer/SKILL.md +9 -9
- package/package.json +1 -1
- package/skills_index.json +574 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: scala
|
|
3
|
+
description: "Language-specific super-code guidelines for scala."
|
|
4
|
+
risk: safe
|
|
5
|
+
source: community
|
|
6
|
+
date_added: "2026-06-16"
|
|
7
|
+
---
|
|
8
|
+
# Scala: Idiomatic Efficiency Reference
|
|
9
|
+
|
|
10
|
+
## Table of Contents
|
|
11
|
+
1. [Collections & Functional Transforms](#collections)
|
|
12
|
+
2. [Pattern Matching](#patterns)
|
|
13
|
+
3. [Case Classes & ADTs](#case-classes)
|
|
14
|
+
4. [Option & Error Handling](#option)
|
|
15
|
+
5. [Implicits & Given/Using](#implicits)
|
|
16
|
+
6. [Concurrency](#concurrency)
|
|
17
|
+
7. [Anti-patterns specific to Scala](#antipatterns)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 1. Collections & Functional Transforms {#collections}
|
|
22
|
+
|
|
23
|
+
```scala
|
|
24
|
+
// ❌ Imperative accumulation
|
|
25
|
+
val result = new ArrayBuffer[String]()
|
|
26
|
+
for (item <- items) {
|
|
27
|
+
if (item.isActive) result += item.name.toUpperCase
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// ✅
|
|
31
|
+
val result = items.filter(_.isActive).map(_.name.toUpperCase)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```scala
|
|
35
|
+
// ❌ Manual grouping
|
|
36
|
+
val grouped = mutable.Map[String, List[Item]]()
|
|
37
|
+
for (item <- items) {
|
|
38
|
+
grouped(item.category) = grouped.getOrElse(item.category, Nil) :+ item
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ✅
|
|
42
|
+
val grouped = items.groupBy(_.category)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```scala
|
|
46
|
+
// ❌ Manual fold when sum/product works
|
|
47
|
+
var total = 0
|
|
48
|
+
for (o <- orders) total += o.amount
|
|
49
|
+
|
|
50
|
+
// ✅
|
|
51
|
+
val total = orders.map(_.amount).sum
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```scala
|
|
55
|
+
// ❌ Using head on potentially empty collection
|
|
56
|
+
val first = items.head // throws on empty
|
|
57
|
+
|
|
58
|
+
// ✅
|
|
59
|
+
val first = items.headOption // returns Option[T]
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```scala
|
|
63
|
+
// ❌ Chaining filter + head for find
|
|
64
|
+
val found = items.filter(_.id == targetId).head
|
|
65
|
+
|
|
66
|
+
// ✅
|
|
67
|
+
val found = items.find(_.id == targetId) // returns Option[T]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**Use `view` for lazy evaluation on large collections to avoid intermediate allocations.**
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## 2. Pattern Matching {#patterns}
|
|
75
|
+
|
|
76
|
+
```scala
|
|
77
|
+
// ❌ if-else chain for type dispatch
|
|
78
|
+
if (shape.isInstanceOf[Circle]) {
|
|
79
|
+
val c = shape.asInstanceOf[Circle]
|
|
80
|
+
c.radius * c.radius * Math.PI
|
|
81
|
+
} else if (shape.isInstanceOf[Rect]) { ... }
|
|
82
|
+
|
|
83
|
+
// ✅
|
|
84
|
+
shape match {
|
|
85
|
+
case Circle(r) => r * r * Math.PI
|
|
86
|
+
case Rect(w, h) => w * h
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
```scala
|
|
91
|
+
// ❌ Nested match with identical fallthrough
|
|
92
|
+
x match {
|
|
93
|
+
case 1 => "low"
|
|
94
|
+
case 2 => "low"
|
|
95
|
+
case 3 => "mid"
|
|
96
|
+
case _ => "high"
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ✅
|
|
100
|
+
x match {
|
|
101
|
+
case 1 | 2 => "low"
|
|
102
|
+
case 3 => "mid"
|
|
103
|
+
case _ => "high"
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
```scala
|
|
108
|
+
// ❌ Match to extract then use
|
|
109
|
+
val result = opt match {
|
|
110
|
+
case Some(x) => x.toString
|
|
111
|
+
case None => "N/A"
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ✅
|
|
115
|
+
val result = opt.map(_.toString).getOrElse("N/A")
|
|
116
|
+
// or:
|
|
117
|
+
val result = opt.fold("N/A")(_.toString)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## 3. Case Classes & ADTs {#case-classes}
|
|
123
|
+
|
|
124
|
+
```scala
|
|
125
|
+
// ❌ Regular class for data
|
|
126
|
+
class User(val name: String, val age: Int) {
|
|
127
|
+
override def equals(obj: Any): Boolean = ...
|
|
128
|
+
override def hashCode(): Int = ...
|
|
129
|
+
override def toString: String = ...
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ✅
|
|
133
|
+
case class User(name: String, age: Int)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
```scala
|
|
137
|
+
// ❌ Sealed trait with unrelated case objects
|
|
138
|
+
sealed trait Result
|
|
139
|
+
case class Success(value: Int) extends Result
|
|
140
|
+
case class Failure(error: String) extends Result
|
|
141
|
+
case object Unknown extends Result // what does "Unknown" mean?
|
|
142
|
+
|
|
143
|
+
// ✅ — each variant should carry the data it represents
|
|
144
|
+
sealed trait Result[+A]
|
|
145
|
+
case class Success[A] (value: A) extends Result[A]
|
|
146
|
+
case class Failure(error: Throwable) extends Result[Nothing]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
```scala
|
|
150
|
+
// ❌ (Scala 3) Verbose enum
|
|
151
|
+
sealed trait Color
|
|
152
|
+
object Color {
|
|
153
|
+
case object Red extends Color
|
|
154
|
+
case object Green extends Color
|
|
155
|
+
case object Blue extends Color
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ✅ (Scala 3)
|
|
159
|
+
enum Color { case Red, Green, Blue }
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## 4. Option & Error Handling {#option}
|
|
165
|
+
|
|
166
|
+
```scala
|
|
167
|
+
// ❌ Null checks
|
|
168
|
+
val name: String = if (user != null) user.name else "Unknown"
|
|
169
|
+
|
|
170
|
+
// ✅
|
|
171
|
+
val name = Option(user).map(_.name).getOrElse("Unknown")
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
```scala
|
|
175
|
+
// ❌ .get on Option (defeats the purpose)
|
|
176
|
+
val name = userOpt.get // throws if None
|
|
177
|
+
|
|
178
|
+
// ✅
|
|
179
|
+
val name = userOpt.getOrElse("default")
|
|
180
|
+
// or: userOpt.map(process).getOrElse(fallback)
|
|
181
|
+
// or: userOpt match { case Some(u) => ... case None => ... }
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
```scala
|
|
185
|
+
// ❌ Try with .get
|
|
186
|
+
val result = Try(parse(input)).get // throws on failure
|
|
187
|
+
|
|
188
|
+
// ✅
|
|
189
|
+
val result = Try(parse(input)) match {
|
|
190
|
+
case Success(v) => v
|
|
191
|
+
case Failure(e) => handleError(e)
|
|
192
|
+
}
|
|
193
|
+
// or: Try(parse(input)).getOrElse(default)
|
|
194
|
+
// or: Try(parse(input)).toEither
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
```scala
|
|
198
|
+
// ❌ Using exceptions for expected failures
|
|
199
|
+
def findUser(id: String): User = {
|
|
200
|
+
val user = db.query(id)
|
|
201
|
+
if (user == null) throw new NotFoundException(id)
|
|
202
|
+
user
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ✅ — Option for absence, Either for expected errors
|
|
206
|
+
def findUser(id: String): Option[User] = db.query(id)
|
|
207
|
+
// or:
|
|
208
|
+
def findUser(id: String): Either[AppError, User]
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## 5. Implicits & Given/Using {#implicits}
|
|
214
|
+
|
|
215
|
+
```scala
|
|
216
|
+
// ❌ (Scala 2) Implicit conversion that hides bugs
|
|
217
|
+
implicit def stringToInt(s: String): Int = s.toInt
|
|
218
|
+
|
|
219
|
+
// ✅ — extension methods instead of implicit conversions
|
|
220
|
+
extension (s: String)
|
|
221
|
+
def toIntSafe: Option[Int] = s.toIntOption
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
```scala
|
|
225
|
+
// ❌ (Scala 2) Implicit parameter with broad type
|
|
226
|
+
def query(sql: String)(implicit conn: Connection): ResultSet
|
|
227
|
+
|
|
228
|
+
// ✅ (Scala 3)
|
|
229
|
+
def query(sql: String)(using conn: Connection): ResultSet
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
```scala
|
|
233
|
+
// ❌ Importing implicits from everywhere
|
|
234
|
+
import com.lib.implicits._
|
|
235
|
+
|
|
236
|
+
// ✅ — import only what you need
|
|
237
|
+
import com.lib.given
|
|
238
|
+
// or specific: import com.lib.{given ExecutionContext}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## 6. Concurrency {#concurrency}
|
|
244
|
+
|
|
245
|
+
```scala
|
|
246
|
+
// ❌ Thread.sleep in production code
|
|
247
|
+
Thread.sleep(5000)
|
|
248
|
+
|
|
249
|
+
// ✅ — use scheduler / timer abstraction
|
|
250
|
+
import scala.concurrent.duration._
|
|
251
|
+
system.scheduler.scheduleOnce(5.seconds)(doWork())
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
```scala
|
|
255
|
+
// ❌ Blocking inside Future
|
|
256
|
+
Future {
|
|
257
|
+
val result = blockingHttpCall() // starves thread pool
|
|
258
|
+
process(result)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// ✅
|
|
262
|
+
Future {
|
|
263
|
+
blocking { val result = blockingHttpCall() }
|
|
264
|
+
// or use a dedicated blocking ExecutionContext
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
```scala
|
|
269
|
+
// ❌ Awaiting futures in a loop
|
|
270
|
+
for (f <- futures) Await.result(f, Duration.Inf)
|
|
271
|
+
|
|
272
|
+
// ✅
|
|
273
|
+
val all = Future.sequence(futures)
|
|
274
|
+
all.map(results => process(results))
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
**Prefer `Future.sequence`/`Future.traverse` over manual await loops.**
|
|
278
|
+
|
|
279
|
+
---
|
|
280
|
+
|
|
281
|
+
## 7. Anti-patterns specific to Scala {#antipatterns}
|
|
282
|
+
|
|
283
|
+
| Anti-pattern | Preferred |
|
|
284
|
+
|---|---|
|
|
285
|
+
| `.get` on Option/Try | `.getOrElse` / pattern match |
|
|
286
|
+
| `null` | `Option` |
|
|
287
|
+
| `isInstanceOf` + `asInstanceOf` | pattern matching |
|
|
288
|
+
| Implicit conversions (Scala 2) | extension methods (Scala 3) |
|
|
289
|
+
| `var` for accumulation | `val` + functional transforms |
|
|
290
|
+
| `return` keyword | last expression is the return value |
|
|
291
|
+
| Mutable collections by default | immutable collections |
|
|
292
|
+
| `Any` / `AnyRef` parameters | generics with type bounds |
|
|
293
|
+
| Deeply nested `for` comprehensions | break into named values |
|
|
294
|
+
| Tuple instead of case class | case class for anything with semantic meaning |
|
|
295
|
+
| `Await.result` in production | compose with `map`/`flatMap` |
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
## Limitations
|
|
301
|
+
- These are language-specific guidelines and do not cover overall architectural decisions.
|
|
302
|
+
- Over-compression might reduce readability; apply judgement.
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: swift
|
|
3
|
+
description: "Language-specific super-code guidelines for swift."
|
|
4
|
+
risk: safe
|
|
5
|
+
source: community
|
|
6
|
+
date_added: "2026-06-16"
|
|
7
|
+
---
|
|
8
|
+
# Swift: Idiomatic Efficiency Reference
|
|
9
|
+
|
|
10
|
+
## Table of Contents
|
|
11
|
+
1. [Optionals](#optionals)
|
|
12
|
+
2. [Collections & Functional Transforms](#collections)
|
|
13
|
+
3. [Value vs Reference Types](#value-types)
|
|
14
|
+
4. [Error Handling](#errors)
|
|
15
|
+
5. [Concurrency](#concurrency)
|
|
16
|
+
6. [Protocol-Oriented Design](#protocols)
|
|
17
|
+
7. [Anti-patterns specific to Swift](#antipatterns)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 1. Optionals {#optionals}
|
|
22
|
+
|
|
23
|
+
```swift
|
|
24
|
+
// ❌ Force unwrap
|
|
25
|
+
let name = user.name!
|
|
26
|
+
|
|
27
|
+
// ✅ — guard or if-let
|
|
28
|
+
guard let name = user.name else { return }
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```swift
|
|
32
|
+
// ❌ Nested if-let pyramid
|
|
33
|
+
if let user = fetchUser() {
|
|
34
|
+
if let address = user.address {
|
|
35
|
+
if let city = address.city {
|
|
36
|
+
display(city)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ✅ — chained optional binding
|
|
42
|
+
if let city = fetchUser()?.address?.city {
|
|
43
|
+
display(city)
|
|
44
|
+
}
|
|
45
|
+
// or guard-let for early exit
|
|
46
|
+
guard let city = fetchUser()?.address?.city else { return }
|
|
47
|
+
display(city)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```swift
|
|
51
|
+
// ❌ Ternary for default
|
|
52
|
+
let name = user.name != nil ? user.name! : "Unknown"
|
|
53
|
+
|
|
54
|
+
// ✅
|
|
55
|
+
let name = user.name ?? "Unknown"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```swift
|
|
59
|
+
// ❌ Optional map when if-let is clearer for side effects
|
|
60
|
+
user.name.map { display($0) }
|
|
61
|
+
|
|
62
|
+
// ✅ — map for transforms, if-let for side effects
|
|
63
|
+
let upper = user.name.map { $0.uppercased() }
|
|
64
|
+
if let name = user.name { display(name) }
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## 2. Collections & Functional Transforms {#collections}
|
|
70
|
+
|
|
71
|
+
```swift
|
|
72
|
+
// ❌ Imperative filter + map
|
|
73
|
+
var result: [String] = []
|
|
74
|
+
for item in items {
|
|
75
|
+
if item.isActive { result.append(item.name.uppercased()) }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ✅
|
|
79
|
+
let result = items
|
|
80
|
+
.filter(\.isActive)
|
|
81
|
+
.map { $0.name.uppercased() }
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```swift
|
|
85
|
+
// ❌ Manual dictionary construction
|
|
86
|
+
var dict: [String: User] = [:]
|
|
87
|
+
for user in users { dict[user.id] = user }
|
|
88
|
+
|
|
89
|
+
// ✅
|
|
90
|
+
let dict = Dictionary(uniqueKeysWithValues: users.map { ($0.id, $0) })
|
|
91
|
+
// or with possible duplicates:
|
|
92
|
+
let dict = Dictionary(grouping: users, by: \.department)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```swift
|
|
96
|
+
// ❌ Checking isEmpty then accessing first
|
|
97
|
+
if !items.isEmpty { process(items[0]) }
|
|
98
|
+
|
|
99
|
+
// ✅
|
|
100
|
+
if let first = items.first { process(first) }
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```swift
|
|
104
|
+
// ❌ Index-based loop
|
|
105
|
+
for i in 0..<items.count { process(items[i]) }
|
|
106
|
+
|
|
107
|
+
// ✅
|
|
108
|
+
for item in items { process(item) }
|
|
109
|
+
// with index:
|
|
110
|
+
for (i, item) in items.enumerated() { process(i, item) }
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**Use key paths (`\.isActive`) as closure shorthand where supported.**
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## 3. Value vs Reference Types {#value-types}
|
|
118
|
+
|
|
119
|
+
```swift
|
|
120
|
+
// ❌ Class for plain data (reference semantics where value semantics suffice)
|
|
121
|
+
class Point {
|
|
122
|
+
var x: Double
|
|
123
|
+
var y: Double
|
|
124
|
+
init(x: Double, y: Double) { self.x = x; self.y = y }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// ✅
|
|
128
|
+
struct Point { var x, y: Double }
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
```swift
|
|
132
|
+
// ❌ Large struct copied repeatedly (performance hit)
|
|
133
|
+
struct HugeData { var buffer: [UInt8] /* thousands of elements */ }
|
|
134
|
+
func process(_ data: HugeData) { ... } // copies entire buffer
|
|
135
|
+
|
|
136
|
+
// ✅ — use class or pass inout for mutation
|
|
137
|
+
func process(_ data: inout HugeData) { ... }
|
|
138
|
+
// or use copy-on-write wrapper for large value types
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
**Default to `struct`. Use `class` when you need identity, inheritance, or reference semantics.**
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## 4. Error Handling {#errors}
|
|
146
|
+
|
|
147
|
+
```swift
|
|
148
|
+
// ❌ Using optionals to mask errors
|
|
149
|
+
func parse(_ input: String) -> Data? { ... } // caller doesn't know why it failed
|
|
150
|
+
|
|
151
|
+
// ✅
|
|
152
|
+
func parse(_ input: String) throws -> Data { ... }
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
```swift
|
|
156
|
+
// ❌ try! in production code
|
|
157
|
+
let data = try! JSONDecoder().decode(User.self, from: jsonData)
|
|
158
|
+
|
|
159
|
+
// ✅
|
|
160
|
+
do {
|
|
161
|
+
let data = try JSONDecoder().decode(User.self, from: jsonData)
|
|
162
|
+
} catch {
|
|
163
|
+
logger.error("decode failed: \(error)")
|
|
164
|
+
throw AppError.decodingFailed(underlying: error)
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
```swift
|
|
169
|
+
// ❌ Generic Error type
|
|
170
|
+
enum AppError: Error { case generic(String) }
|
|
171
|
+
|
|
172
|
+
// ✅ — specific, actionable error cases
|
|
173
|
+
enum AppError: Error {
|
|
174
|
+
case networkUnreachable
|
|
175
|
+
case invalidInput(field: String, reason: String)
|
|
176
|
+
case unauthorized
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
```swift
|
|
181
|
+
// ❌ Catching all errors and ignoring
|
|
182
|
+
do { try riskyOperation() } catch { }
|
|
183
|
+
|
|
184
|
+
// ✅
|
|
185
|
+
do {
|
|
186
|
+
try riskyOperation()
|
|
187
|
+
} catch let error as NetworkError {
|
|
188
|
+
handleNetworkError(error)
|
|
189
|
+
} catch {
|
|
190
|
+
throw error // rethrow unknown
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
## 5. Concurrency {#concurrency}
|
|
197
|
+
|
|
198
|
+
```swift
|
|
199
|
+
// ❌ Callback-based async (pyramid of doom)
|
|
200
|
+
fetchUser { user in
|
|
201
|
+
fetchPosts(for: user) { posts in
|
|
202
|
+
fetchComments(for: posts.first!) { comments in
|
|
203
|
+
display(comments)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ✅ (Swift 5.5+)
|
|
209
|
+
let user = try await fetchUser()
|
|
210
|
+
let posts = try await fetchPosts(for: user)
|
|
211
|
+
let comments = try await fetchComments(for: posts[0])
|
|
212
|
+
display(comments)
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
```swift
|
|
216
|
+
// ❌ Sequential awaits for independent work
|
|
217
|
+
let a = try await fetchA()
|
|
218
|
+
let b = try await fetchB()
|
|
219
|
+
|
|
220
|
+
// ✅
|
|
221
|
+
async let a = fetchA()
|
|
222
|
+
async let b = fetchB()
|
|
223
|
+
let (resultA, resultB) = try await (a, b)
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
```swift
|
|
227
|
+
// ❌ DispatchQueue.main.async for UI updates in async context
|
|
228
|
+
DispatchQueue.main.async { label.text = result }
|
|
229
|
+
|
|
230
|
+
// ✅
|
|
231
|
+
await MainActor.run { label.text = result }
|
|
232
|
+
// or mark the function/class @MainActor
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
**Use `actor` for mutable shared state instead of manual locks/queues.**
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## 6. Protocol-Oriented Design {#protocols}
|
|
240
|
+
|
|
241
|
+
```swift
|
|
242
|
+
// ❌ Deep class inheritance hierarchy
|
|
243
|
+
class Animal { ... }
|
|
244
|
+
class Dog: Animal { ... }
|
|
245
|
+
class GuideDog: Dog { ... }
|
|
246
|
+
|
|
247
|
+
// ✅ — protocols + composition
|
|
248
|
+
protocol Animal { var name: String { get } }
|
|
249
|
+
protocol Trainable { func train() }
|
|
250
|
+
struct Dog: Animal, Trainable { ... }
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
```swift
|
|
254
|
+
// ❌ Protocol with default implementations for everything
|
|
255
|
+
protocol Renderable {
|
|
256
|
+
func render()
|
|
257
|
+
}
|
|
258
|
+
extension Renderable {
|
|
259
|
+
func render() { /* default */ }
|
|
260
|
+
}
|
|
261
|
+
// Every conformer uses default — protocol serves no purpose
|
|
262
|
+
|
|
263
|
+
// ✅ — only default implementations that provide genuine shared logic
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
```swift
|
|
267
|
+
// ❌ Associated type when generic parameter suffices
|
|
268
|
+
protocol Container {
|
|
269
|
+
associatedtype Element
|
|
270
|
+
func get() -> Element
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// ✅ — use `some` or generic parameter for simple cases
|
|
274
|
+
func process(_ item: some Equatable) { ... }
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
---
|
|
278
|
+
|
|
279
|
+
## 7. Anti-patterns specific to Swift {#antipatterns}
|
|
280
|
+
|
|
281
|
+
| Anti-pattern | Preferred |
|
|
282
|
+
|---|---|
|
|
283
|
+
| Force unwrap `!` in production code | `guard let` / `if let` / `??` |
|
|
284
|
+
| `try!` outside tests | `do/catch` |
|
|
285
|
+
| `class` for plain data | `struct` |
|
|
286
|
+
| Deep inheritance hierarchies | protocol composition |
|
|
287
|
+
| `@objc` when pure Swift works | native Swift types |
|
|
288
|
+
| `NSArray` / `NSDictionary` | `Array` / `Dictionary` |
|
|
289
|
+
| `DispatchQueue` in async/await code | `actor` / `MainActor` |
|
|
290
|
+
| Implicitly unwrapped optionals as fields | regular optionals or non-optional with init |
|
|
291
|
+
| `Any` / `AnyObject` everywhere | generics with protocol constraints |
|
|
292
|
+
| Massive `switch` over string values | enum with raw values |
|
|
293
|
+
| Singleton pattern (global mutable state) | dependency injection |
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
## Limitations
|
|
298
|
+
- These are language-specific guidelines and do not cover overall architectural decisions.
|
|
299
|
+
- Over-compression might reduce readability; apply judgement.
|