Use `guard` when a condition must hold for the rest of the function. Its `else` block must exit the current scope: ```swift func displayName(for user: User?) -> String { guard let user else { return "Anonymous" } guard !user.name.isEmpty else { return "Unnamed" } return user.name } ``` This keeps the happy path unindented and makes precondition failures explicit. Prefer it to deeply nested `if let` statements; use `if let` when the optional value is needed only in a small branch.
Scanned 9/5/2026
Install to Claude Code
npx -y skills add HoangNguyen0403/agent-skills-standard --skill swift-best-practices --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Swift Best Practices?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-swift-best-practices-agent-skills-standard)More formats (shields.io, HTML) on the badges page.
Use `guard` when a condition must hold for the rest of the function. Its `else` block must exit the current scope:
```swift
func displayName(for user: User?) -> String {
guard let user else { return "Anonymous" }
guard !user.name.isEmpty else { return "Unnamed" }
return user.name
}
```
This keeps the happy path unindented and makes precondition failures explicit. Prefer it to deeply nested `if let` statements; use `if let` when the optional value is needed only in a small branch.
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!