Rule Swift

Guard Early Return

Use guard for preconditions and early exits to reduce nesting

swiftreadabilitypatternsmacosios
CLAUDE.md

Use guard statements for precondition checks and early exits. This keeps the happy path at the lowest indentation level.

// Good — flat, readable
func processOrder(_ order: Order?) {
    guard let order else { return }
    guard order.isValid else {
        log("Invalid order: \(order.id)")
        return
    }
    submitOrder(order)
}

// Bad — nested pyramid
func processOrder(_ order: Order?) {
    if let order = order {
        if order.isValid {
            submitOrder(order)
        }
    }
}

Reserve if let for branches where both the nil and non-nil paths have meaningful work.

Copy this block into your CLAUDE.md or agent config file to enforce it in your workflow.

get crystl