As the lead of a SwiftUI app with several slow screens, which redraw rules would you enforce in code review, and when would you profile instead of guessing?
answer
- defaults versus measurement
- body composes, never computes
- read where displayed
- real device, Release, SwiftUI template
basics
~20 sEnforce a few mechanism-based defaults: no computation in body, volatile values read where displayed, @Observable models, real view splits, and justified .equatable(). Profile on a real device with the SwiftUI instrument whenever users report hitches or a change claims a speed-up.
solid answer
~40 sI'd enforce a short list a reviewer can check from a diff: `body` composes but never computes; ticking values are read in the smallest view that shows them, with parents passing the `@Observable` object; new models use `@Observable`; extraction means real `View` structs, not computed properties; `.equatable()` only with an `==` covering everything the body uses and a measured gain; no `_printChanges()` merged. Those are defaults, not proof. I profile when users report hitches or hangs, before and after any optimization, and whenever rules conflict, using the SwiftUI template in Instruments on a real device with a Release build, then `_printChanges()` to explain the hot views.
go deeper
Recall the basic habits: keep heavy work out of body, keep views small around values that change, and test speed on a real device.
Explain the mechanism behind each rule, such as per-body dependency tracking and why computed properties do not split updates.
Show how you would find the slow view with the SwiftUI instrument, explain it with _printChanges, and prove a fix with before and after numbers.
Choose which rules apply everywhere and which only on measured hot screens, who owns performance of key screens, and when measurement overrides a rule.
## Two kinds of decisions A lead looking after a SwiftUI app with slow screens has to decide two things: which habits the team follows **by default** in every pull request, and when to stop following rules of thumb and **measure**. Rules keep the common cases cheap without anyone profiling; measurement settles the cases where the rules conflict or do not obviously apply. Getting the balance wrong either way costs: no rules means every screen degrades a little, and too many rules produce code full of indirection that was never shown to help. ## Rules worth enforcing in review These are cheap to check by reading a diff, and each is tied to how SwiftUI updates views: 1. **`body` composes, it does not compute.** No formatter construction, sorting, filtering, decoding or image work in `body`; derived data is stored and refreshed when its inputs change, formatting uses format styles. 2. **Read volatile values where they are displayed.** A property that changes often (a price, a timer, a progress value) is read in the smallest view that shows it. Parents pass the `@Observable` object or a binding, not the value. 3. **New models use `@Observable`.** On an iOS 17 floor, per-property tracking is the default; a new `ObservableObject` needs a reason. 4. **Split along update boundaries, not line counts.** A computed property or `@ViewBuilder` function does not narrow updates; a separate `View` struct does. 5. **Do not create fresh values in a parent's body just to pass them down**, such as `Date()` or a newly built array, unless the child really needs a new value each time. 6. **`.equatable()` needs justification.** The `==` must cover everything the body uses, including what closures capture, and the PR shows the measured gain. 7. **No `_printChanges()` in merged code.** It is an underscored debugging aid; keep it behind `#if DEBUG` while diagnosing and delete it after. Lazy containers for long collections belong on this list too; when to choose them is covered with lists and lazy stacks. ## What the rules cannot see Each rule is a proxy. None of them tells you whether a screen is actually slow, which view costs the most, or whether a refactor helped. A diff also cannot show runtime dependencies: whether a parent reads a property is sometimes three helper calls away. That is where measurement comes in. ## When to profile instead of guessing - **A user-visible problem is reported**: a hitch while scrolling, a tap that lags, a hang. - **Before and after an optimization**, so the PR carries numbers, not intuition. - **When rules conflict**, such as splitting for performance against keeping a view readable. - **Before adopting an exotic fix**, such as `.equatable()` or caching layers. How to profile, according to Apple's documentation: | Step | Detail | |---|---| | Device | a real device; Apple's article on scrollable stacks says never to profile performance in the iOS simulator | | Build | the Profile action, normally a Release configuration, because unoptimized code distorts timing | | Template | the SwiftUI template in Instruments; Apple's performance guidance names long view body updates and frequently occurring SwiftUI updates as what to analyze | | Read | which view types have the most body evaluations and the longest ones, then confirm the cause with `_printChanges()` | `Self._printChanges()` and the instrument complement each other: the first explains **why** one view ran, the second shows **how often and how long** across a whole interaction. ## The tradeoffs a lead owns - **Rules versus readability.** Every rule above adds a little structure. Enforce the first four everywhere; reserve the rest for screens with measured problems. - **Automation.** Most of these rules are judgment calls a linter cannot check well; a short review checklist plus a performance pass on the heaviest screens before release usually beats a long rule file. - **Ownership.** Name an owner for the few screens that matter most (the main feed, checkout) and keep their traces from the last release to compare against. - **Budgets.** How many milliseconds a frame may take and how to track it across releases is a system-design decision; the rules here are how a SwiftUI team stays inside whatever budget is set. A good answer to this question names a short set of rules tied to mechanisms, says plainly that rules are defaults, and makes profiling on a device the tiebreaker.
- Why not require .equatable() on every list row as a blanket rule?Because it only helps when the body is expensive and the parent hands over new but meaningless values, and it hurts whenever `==` skips something the body uses: rows show stale data or run stale closures. It needs a measured reason and a reviewed `==`, not a blanket rule.
- What do you look for first in a SwiftUI trace of a slow scroll?Which view types have the most body evaluations and which have the longest ones during the scroll. A high count points at update scope, too many views depending on a changing value; a long single evaluation points at work inside `body`. Then `_printChanges()` on that view explains why it ran.
- profiling in the simulator is good enough for SwiftUI
- every view should be Equatable to be safe
- rules alone guarantee a fast screen without measurement
- _printChanges() output is a substitute for timing data
- splitting every stack into its own view is always faster