
SwiftUI View Refactor
FreeStreamline and standardize your SwiftUI views.
Free · Opens the source repo
What SwiftUI View Refactor does
The SwiftUI View Refactor skill provides a structured approach to organizing and refactoring SwiftUI views, focusing on best practices for layout, dependency injection, and observation management. This skill is aimed at developers looking to enhance the maintainability and readability of their SwiftUI code by adhering to established Model-View (MV) patterns. By applying these guidelines, developers can ensure that their views are lightweight and composable, making it easier to manage state and dependencies.
The core guidelines emphasize a specific order for view components, promoting a clear hierarchy that begins with environment settings and ends with helper functions. This ordering not only improves code readability but also aids in understanding the flow of data and state within the application. Additionally, the skill encourages developers to split large views into smaller subviews, which can significantly reduce complexity and improve reusability across the application.
Another key aspect of the skill is its focus on view model handling. It advises against introducing view models unless necessary and encourages the use of non-optional view models when they do exist. This helps maintain a clean and efficient architecture, ensuring that dependencies are managed correctly and that the view logic remains straightforward. The skill also addresses the proper use of observation, guiding developers on how to effectively utilize SwiftUI's state management features without introducing unnecessary complexity.
Overall, this skill is a valuable resource for SwiftUI developers aiming to refine their code structure and adhere to best practices, ultimately leading to more maintainable and scalable applications.
When to use it
Use this skill when refactoring existing SwiftUI views or when establishing a new codebase to ensure adherence to best practices.
When not to use it
This skill may not be suitable for simple projects where the overhead of strict patterns is unnecessary or for developers who prefer a more flexible approach to view structuring.
What you can build with it
Refactoring a Large View
When faced with a large SwiftUI view, this skill guides you in breaking it down into smaller, manageable subviews, enhancing clarity and reusability.
Establishing a New Codebase
For new projects, apply this skill to set a strong foundation by adhering to MV patterns and ensuring a clean architecture from the start.
Improving Existing Code
Use this skill to review and refactor existing SwiftUI views, aligning them with best practices for better maintainability and performance.
How to install SwiftUI View Refactor
View source1. Install with the skills CLI
npx skills add steipete/agent-scripts/swiftui-view-refactor --agent claude-code2. Or install it manually
Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.
Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs
Inside SKILL.md
Written by steipeteSwiftUI View Refactor
Attribution: copied from @Dimillian’s Dimillian/Skills (2025-12-31).
Overview
Apply a consistent structure and dependency pattern to SwiftUI views, with a focus on ordering, Model-View (MV) patterns, careful view model handling, and correct Observation usage.
Core Guidelines
1) View ordering (top → bottom)
- Environment
private/publiclet@State/ other stored properties- computed
var(non-view) initbody- computed view builders / other view helpers
- helper / async functions
2) Prefer MV (Model-View) patterns
- Default to MV: Views are lightweight state expressions; models/services own business logic.
- Favor
@State,@Environment,@Query, andtask/onChangefor orchestration. - Inject services and shared models via
@Environment; keep views small and composable. - Split large views into subviews rather than introducing a view model.
3) Split large bodies and view properties
- If
bodygrows beyond a screen or has multiple logical sections, split it into smaller subviews. - Extract large computed view properties (
var header: some View { ... }) into dedicatedViewtypes when they carry state or complex branching. - It's fine to keep related subviews as computed view properties in the same file; extract to a standalone
Viewstruct only when it structurally makes sense or when reuse is intended. - Prefer passing small inputs (data, bindings, callbacks) over reusing the entire parent view state.
Example (extracting a section):
var body: some View {
VStack(alignment: .leading, spacing: 16) {
HeaderSection(title: title, isPinned: isPinned)
DetailsSection(details: details)
ActionsSection(onSave: onSave, onCancel: onCancel)
}
}
Example (long body → shorter body + computed views in the same file):
var body: some View {
List {
header
filters
results
footer
}
}
private var header: some View {
VStack(alignment: .leading, spacing: 6) {
Text(title).font(.title2)
Text(subtitle).font(.subheadline)
}
}
private var filters: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(filterOptions, id: \.self) { option in
FilterChip(option: option, isSelected: option == selectedFilter)
.onTapGesture { selectedFilter = option }
}
}
}
}
Example (extracting a complex computed view):
private var header: some View {
HeaderSection(title: title, subtitle: subtitle, status: status)
}
private struct HeaderSection: View {
let title: String
let subtitle: String?
let status: Status
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text(title).font(.headline)
if let subtitle { Text(subtitle).font(.subheadline) }
StatusBadge(status: status)
}
}
}
4) View model handling (only if already present)
- Do not introduce a view model unless the request or existing code clearly calls for one.
- If a view model exists, make it non-optional when possible.
- Pass dependencies to the view via
init, then pass them into the view model in the view'sinit. - Avoid
bootstrapIfNeededpatterns.
Example (Observation-based):
@State private var viewModel: SomeViewModel
init(dependency: Dependency) {
_viewModel = State(initialValue: SomeViewModel(dependency: dependency))
}
5) Observation usage
- For
@Observablereference types, store them as@Statein the root view. - Pass observables down explicitly as needed; avoid optional state unless required.
Workflow
- Reorder the view to match the ordering rules.
- Favor MV: move lightweight orchestration into the view using
@State,@Environment,@Query,task, andonChange. - If a view model exists, replace optional view models with a non-optional
@Stateview model initialized ininitby passing dependencies from the view. - Confirm Observation usage:
@Statefor root@Observableview models, no redundant wrappers. - Keep behavior intact: do not change layout or business logic unless requested.
Notes
- Prefer small, explicit helpers over large conditional blocks.
- Keep computed view builders below
bodyand non-view computed vars aboveinit. - For MV-first guidance and rationale, see
references/mv-patterns.md.
Frequently asked questions about SwiftUI View Refactor
Similar skills
React Composition Patterns
Streamline your React component architecture with proven patterns.
Pester Should Migration
Easily convert Pester v5 assertions to v6 syntax.
Radix to Base UI Migration
Seamlessly migrate React components from Radix UI to Base UI.
Migrate Next.js to Vinext
Seamlessly transition your Next.js projects to Vinext.
WinUI 3 Migration Guide
Streamline your UWP to WinUI 3 migration process.
Refactor
Enhance code maintainability without altering behavior.
