Skip to content

existential-birds/beagle / plugins/beagle-ios/skills/swiftui-code-review

swiftui-code-review

Verified

Reviews SwiftUI code for view composition, state management, performance, and accessibility. Use when reviewing .swift files containing SwiftUI views, property wrappers (@State, @Binding, @Observable), or UI code.

FileKindSize
SKILL.mdskill md2.8 KB
references/accessibility.mdmarkdown3.3 KB
references/performance.mdmarkdown3.0 KB
references/state-management.mdmarkdown2.7 KB
references/view-composition.mdmarkdown2.2 KB

references/performance.md

View on GitHub
# Performance

## Lazy Stacks

Use LazyVStack/LazyHStack for scrolling content with many items.

```swift
// BAD - all 1000 items rendered immediately
ScrollView {
    VStack {
        ForEach(items) { ItemView(item: $0) }
    }
}

// GOOD - only visible items rendered
ScrollView {
    LazyVStack {
        ForEach(items) { ItemView(item: $0) }
    }
}
```

## AnyView Avoidance

AnyView defeats SwiftUI's type-based diffing.

```swift
// BAD - SwiftUI can't diff
func makeView(type: ViewType) -> some View {
    switch type {
    case .a: return AnyView(ViewA())
    case .b: return AnyView(ViewB())
    }
}

// GOOD - preserves type information
@ViewBuilder
func makeView(type: ViewType) -> some View {
    switch type {
    case .a: ViewA()
    case .b: ViewB()
    }
}
```

## ForEach Identity

Use stable Identifiable IDs, never array indices.

```swift
// BAD - index changes when array changes
ForEach(items.indices, id: \.self) { index in
    ItemView(item: items[index])
}

// GOOD - stable ID from Identifiable
ForEach(items) { item in
    ItemView(item: item)
}

// BAD - dynamic range without id
@State var count = 5
List(0..<count) { i in Text("Row \(i)") }  // Crashes!

// GOOD - dynamic range with id
List(0..<count, id: \.self) { i in Text("Row \(i)") }
```

## Equatable Views

For complex views, implement Equatable to optimize diffing.

```swift
struct CalendarView: View, Equatable {
    let events: [Event]
    let selectedDate: Date

    static func == (lhs: Self, rhs: Self) -> Bool {
        lhs.events.count == rhs.events.count &&
        lhs.selectedDate == rhs.selectedDate
    }

    var body: some View { /* expensive */ }
}

// Usage
ParentView()
    .equatable()
```

## View Body Efficiency

Avoid expensive operations in view body.

```swift
// BAD - formatter created on every rebuild
var body: some View {
    let formatter = DateFormatter()
    formatter.dateStyle = .long
    return Text(formatter.string(from: date))
}

// GOOD - cached formatter
private static let formatter: DateFormatter = {
    let f = DateFormatter()
    f.dateStyle = .long
    return f
}()
```

## Expensive Visual Effects

Blur, shadow, and mask cause offscreen rendering.

```swift
// CAUTION - expensive in lists
ForEach(items) { item in
    ItemView(item: item)
        .blur(radius: 5)      // Expensive
        .shadow(radius: 10)   // Expensive
}
```

## Critical Anti-Patterns

| Pattern | Issue |
|---------|-------|
| VStack in ScrollView with 100+ items | All items rendered at once |
| AnyView | Defeats type-based diffing |
| ForEach with array index as id | View recreation on array change |
| .id() modifier inside List | Prevents List optimization |
| DateFormatter in view body | Recreated on every rebuild |

## Review Questions

1. Should this VStack/HStack be Lazy for scrolling performance?
2. Is AnyView used? Can @ViewBuilder replace it?
3. Does ForEach use stable Identifiable IDs?
4. Are expensive computations cached outside view body?
5. Are visual effects (blur, shadow) used sparingly in lists?
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 197kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 197kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 197kUpdated Claude CodeCodexVerifiedInstall