# Swift Best Practices

> Standards for Guard, Value Types, Immutability, and Naming

- Skill: `fierzone/swift-best-practices` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add fierzone/swift-best-practices`
- Raw SKILL.md: https://api.skillmd.com/api/skills/fierzone/swift-best-practices/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: fierzone (https://skillmd.com/u/fierzone)
- Updated: 2026-09-10
- Page: https://skillmd.com/skills/fierzone/swift-best-practices

---


# Swift Best Practices

## **Priority: P0**

## Implementation Guidelines

### Control Flow

- **Guard for Early Exit**: Use `guard` over nested `if` for better readability.
- **for-where**: Use `for item in items where condition` instead of nested `if`.
- **Switch Exhaustiveness**: Always handle all cases; use `@unknown default` for enums.

### Value Types & Immutability

- **Prefer Structs**: Use `struct` by default; classes only for reference semantics or inheritance.
- **Immutability**: Use `let` over `var` whenever possible.
- **Copy-on-Write**: Leverage Swift's built-in COW for collections.

### Naming & Style

- **Clear Intent**: `isEnabled`, `hasData`, `canSubmit` for Booleans.
- **Avoid Abbreviations**: `userRepository` not `userRepo`.
- **Type Inference**: Let compiler infer types unless clarity requires annotation.

## Anti-Patterns

- **Nested If**: `**No Pyramid of Doom**: Use guard.`
- **Mutable Globals**: `**No var at Global Scope**: Use constants or dependency injection.`
- **Force Try**: `**No try!**: Handle errors properly.`

## References

- [Guard Patterns & Immutability](references/implementation.md)

