---
name: swift-best-practices
description: Apply Guard, Value Types, Immutability, and Naming conventions in Swift. Use when writing idiomatic Swift with guard, value types, immutability, or naming conventions—not Swift concurrency/actor-isolation, persistence, linting, or release tooling.
metadata:
  triggers:
    files:
    - '**/*.swift'
    keywords:
    - guard
    - let
    - struct
    - final
    - swift idiomatic
    - swift naming
    - swift best practice
    - swift conventions
    - value type
    - immutability swift
    - guard let
---
# Swift Best Practices

## **Priority: P0 (CRITICAL)**

## Implementation Guidelines

### Control Flow (Guard over If)

- **Guard for Early Exit**: Use **`guard let`** over **nested if** statements for better readability and to unwrap optionals early.
- **Nested Checks**: Use **`guard`** for **precondition** checks at top of function to reduce nested depth.
- **Switch Exhaustiveness**: Always handle all cases; use **`@unknown default`** for freezing enums (enums from frameworks).
- **if-case**: Use **`if case .success(let value) = result`** for simple enum pattern matching.

### Value Types & Immutability

- **Prefer Structs**: **Default to struct** for **value semantics** and thread safety. Use `class` only when reference identity or **inheritance** required.
- **Immutability**: Always **default to let** for all properties and constants. Use `var` only when change required.
- **Modifiers**: Use **`final`** for all classes that not intended to subclassed to improve performance (static dispatch).
- **Static Dispatch**: Favor methods in structs and `final` classes.

### Naming & Style

- **Clear Intent**: Prefix booleans with **`is, has, or can`**. Example: **`isValid`**, `hasErrors`, **`canEdit`**. Makes boolean state clear.
- **API Guidelines**: Follow official **Swift API Design Guidelines**. Use **`camelCase`** for **clear names** and `PascalCase` for types.
- **Protocols**: Name protocols with **`-able`**, `-ible`, or `-ing` suffixes (e.g., `Codable`, `Identifiable`).
- **Opaque Types**: Use **`some View`** or `some Collection` for return types where underlying type internal.

### Collection Performance

- **Sequence API**: Use **`compactMap`**, **`filter`**, and **`reduce`** instead of explicit **for-where** loops for data transformations.
- **Lazy Collections**: Use **`.lazy`** for large sequences when result consumed partially.
- **Dictionaries**: Use **`default`** values in dictionary access to avoid double optional unwrapping.

## Anti-Patterns

- **No Pyramid of Doom**: Use **`guard`** for early exits.
- **No force unwrap**: Never use **`!`** on optionals. Use **`??`** (nil-coalescing) or **`if let`**.
- **No global var**: Avoid mutable global state. Use **Singletons** (accessed via `static let shared`) or DI.

## References

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


---
Source attribution: HoangNguyen0403
Original project: https://github.com/HoangNguyen0403/agent-skills-standard/blob/c60b42c11db73f29817031ad92476b42feb0ab11/skills/swift/swift-best-practices/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Hoang Nguyen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: HoangNguyen0403
Original project: https://github.com/HoangNguyen0403/agent-skills-standard/blob/c60b42c11db73f29817031ad92476b42feb0ab11/skills/swift/swift-best-practices/SKILL.md
Attribution basis: Source repository record

Fields of work: Development & Engineering, Product Management
Roles: Backend Developer, Frontend Developer, Full-Stack Developer, Mobile Developer, Product Manager
Specialties: Backend, Cross Platform, Mobile
Tools: Tool not specified
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: bd5f0b8a891fe572929d565cfc7b5358bd35f97752a1750298de24eda101e140
