Agent-skills-standard swift-best-practices
Apply Guard, Value Types, Immutability, and Naming conventions in Swift. Use when writing idiomatic Swift using guard, value types, immutability, or naming conventions. (triggers: **/*.swift, guard, let, struct, final)
install
source · Clone the upstream repo
git clone https://github.com/HoangNguyen0403/agent-skills-standard
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/HoangNguyen0403/agent-skills-standard "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/swift/swift-best-practices" ~/.claude/skills/hoangnguyen0403-agent-skills-standard-swift-best-practices && rm -rf "$T"
manifest:
skills/swift/swift-best-practices/SKILL.mdsource content
Swift Best Practices
Priority: P0
Implementation Guidelines
Control Flow (Guard over If)
- Guard for Early Exit: Use
over nested if statements for better readability and to unwrap optionals early.guard let - Nested Checks: Use
for precondition checks at top of function to reduce nested depth.guard - Switch Exhaustiveness: Always handle all cases; use
for freezing enums (enums from frameworks).@unknown default - if-case: Use
for simple enum pattern matching.if case .success(let value) = result
Value Types & Immutability
- Prefer Structs: Default to struct for value semantics and thread safety. Use
only when reference identity or inheritance required.class - Immutability: Always default to let for all properties and constants. Use
only when change required.var - Modifiers: Use
for all classes that not intended to subclassed to improve performance (static dispatch).final - Static Dispatch: Favor methods in structs and
classes.final
Naming & Style
- Clear Intent: Prefix booleans with
. Example:is, has, or can
,isValid
,hasErrors
. Makes boolean state clear.canEdit - API Guidelines: Follow official Swift API Design Guidelines. Use
for clear names andcamelCase
for types.PascalCase - Protocols: Name protocols with
,-able
, or-ible
suffixes (e.g.,-ing
,Codable
).Identifiable - Opaque Types: Use
orsome View
for return types where underlying type internal.some Collection
Collection Performance
- Sequence API: Use
,compactMap
, andfilter
instead of explicit for-where loops for data transformations.reduce - Lazy Collections: Use
for large sequences when result consumed partially..lazy - Dictionaries: Use
values in dictionary access to avoid double optional unwrapping.default
Anti-Patterns
- No Pyramid of Doom: Use
for early exits.guard - No force unwrap: Never use
on optionals. Use!
(nil-coalescing) or??
.if let - No global var: Avoid mutable global state. Use Singletons (accessed via
) or DI.static let shared