swift-best-practices
Installation
SKILL.md
Swift Best Practices
Priority: P0 (CRITICAL)
Implementation Guidelines
Control Flow (Guard over If)
- Guard for Early Exit: Use
guard letover nested if statements for better readability and to unwrap optionals early. - Nested Checks: Use
guardfor precondition checks at top of function to reduce nested depth. - Switch Exhaustiveness: Always handle all cases; use
@unknown defaultfor freezing enums (enums from frameworks). - if-case: Use
if case .success(let value) = resultfor simple enum pattern matching.
Value Types & Immutability
- Prefer Structs: Default to struct for value semantics and thread safety. Use
classonly when reference identity or inheritance required. - Immutability: Always default to let for all properties and constants. Use
varonly when change required. - Modifiers: Use
finalfor all classes that not intended to subclassed to improve performance (static dispatch). - Static Dispatch: Favor methods in structs and
finalclasses.