In one of my recent articles titled Working with AGENTS.md: What I Learned, I talked about how we can use the AGENTS.md file to set up guidelines and conventions specific to our project.
Although AGENTS.md is a great way to communicate project level rules and conventions, sometimes we want to provide more direction to the AI agent. This is particularly useful when we have already decided how we want to architect our application and how different pieces of the application should work together.
One way to accomplish this is through scaffolding. Instead of explaining our entire architecture to the AI agent through prompts, we can simply provide the structure of our application and let the agent fill in the missing pieces.
In this article, I will demonstrate how we can use scaffolding along with specifications to guide AI agents when building a SwiftUI application.
I think of scaffolding as providing a wireframe, an outline, or a skeleton for an application. This can include creating folders, files, defining types, and even adding function signatures with comments describing what each function is supposed to do.
At this point, we are not concerned with the actual implementation. We are simply laying out the foundation of our application and deciding how different pieces will fit together.
Below, you can see a very simple example of scaffolding.
struct Resource<T: Codable> {
let url: URL
let modelType: T
}
struct HTTPClient {
/*
func fetch<T>(_ resource: Resource<T>) async throws -> T {
} */
}
There is no hard and fast rule for how detailed your scaffolding needs to be. Personally, I tend to go a little deeper as this gives the AI agent a better understanding of how I want to architect my application.
Below, you can see my scaffolding of the ProductStore.
@Observable
class ProductStore {
// private(set) var products: [Product] = []
let httpClient: HTTPClient
init(httpClient: HTTPClient) {
self.httpClient = httpClient
}
// This function will use httpClient to perform
// a request to load all products.
func loadProducts() async throws {
}
}
Notice that although we have not implemented the loadProducts function, we have already decided that the ProductStore will use an instance of HTTPClient to load the products. We are communicating our architectural decisions directly through the code.
And finally, here is the scaffolding of the ProductListScreen, which is responsible for displaying all the products on the screen.
struct ProductListScreen: View {
// Access ProductStore from Environment
var body: some View {
/*
- When products are loading, show progress view
- When an error occurs, show error view
- If no products are found, show empty view
- When products are loaded, show
ProductListView(products: products)
*/
EmptyView()
}
}
As you can see, the scaffolding also provides some guidance on how the screen should behave under different conditions.
Instead of leaving all these decisions up to the AI agent, we are providing it with a structure to follow. The agent can now focus on implementing the functionality within the architecture we have already established.
Writing specifications is a vast topic and deserves its own article or perhaps even a series of articles. For this particular example, I have created a basic specification file called DisplayProducts.spec.md and placed it under the Specifications folder.
The purpose of a specification is to communicate the business requirements. It is generally a good idea to avoid unnecessary technical and implementation details in the specification document since these requirements often come from product managers or domain experts.
Specification documents can incorporate ubiquitous language, which is a shared vocabulary used consistently by developers, domain experts, and stakeholders across conversations, documentation, and source code within a particular business domain.
Below is an example of a simple specification document.
# Display Products
- As a user, I should be able to view all the products returned from the following URL:
https://fakestoreapi.com/products
- When products are being loaded, a loading indicator should be displayed.
- When an error occurs, a user friendly error message should be displayed on the screen.
- As a user, I should be able to sort products by name.
- Product prices should be displayed based on the user's locale.
- Product ratings should be displayed using stars.
Notice that the specification does not mention anything about SwiftUI, architectural patterns, networking frameworks, or how the data should be managed.
The specification describes what the application should do, not how it should do it. We will communicate our architectural decisions separately through project scaffolding.
You might be wondering if we still need a specification document when we are already scaffolding our application.
Specifications and scaffolding serve different purposes. Specifications communicate the business requirements, while scaffolding communicates our architectural decisions.
For smaller applications, we can certainly communicate the business requirements through comments in our scaffolded files. However, as the application grows, it becomes beneficial to keep the business requirements separate from the implementation. This also allows product managers and domain experts to review the requirements without having to understand the code.
Think of specifications as telling the AI agent what to build and scaffolding as providing guidance on how to build it within our architecture.
Once you are happy with the specification document and the scaffolding, you can ask your AI agent to implement the feature. Although this process can be automated using GitHub Actions, for this particular example, we will simply ask the agent to implement the specification manually.
Implement the specification called "DisplayProducts".
The agent will read the DisplayProducts specification document and start implementing the feature using the architectural structure we have already established through scaffolding.
Below, you can see the implementations of HTTPClient, ProductStore, and ProductListScreen generated by the AI agent based on our scaffolding.
struct Resource<T: Decodable> {
let url: URL
}
struct HTTPClient {
func fetch<T>(_ resource: Resource<T>) async throws -> T {
let (data, response) = try await URLSession.shared.data(from: resource.url)
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
throw HTTPClientError.invalidResponse
}
do {
return try JSONDecoder().decode(T.self, from: data)
} catch {
throw HTTPClientError.invalidData
}
}
}
enum HTTPClientError: Error {
case invalidResponse
case invalidData
}
@Observable
@MainActor
final class ProductStore {
private(set) var products: [Product] = []
private(set) var isLoading = false
private(set) var errorMessage: LocalizedStringResource?
private let httpClient: HTTPClient
init(httpClient: HTTPClient) {
self.httpClient = httpClient
}
func loadProducts() async {
isLoading = true
errorMessage = nil
defer { isLoading = false }
guard let url = URL(string: "https://fakestoreapi.com/products") else {
errorMessage = "We couldn't load the products. Please try again."
return
}
do {
products = try await httpClient.fetch(Resource<[Product]>(url: url))
} catch {
errorMessage = "We couldn't load the products. Please try again."
}
}
}
struct ProductListScreen: View {
@Environment(ProductStore.self) private var store
var body: some View {
NavigationStack {
ProductContentView(
products: store.products,
isLoading: store.isLoading,
errorMessage: store.errorMessage,
retry: { await store.loadProducts() }
)
.navigationTitle("Products")
}
.task {
guard store.products.isEmpty else { return }
await store.loadProducts()
}
}
}
As you can see, the AI agent followed the overall architecture of our application. The ProductStore is responsible for loading and managing the products, the HTTPClient is responsible for making network requests, and the ProductListScreen accesses the store from the environment.
One interesting thing to notice is that the AI agent also made a few changes to our original scaffolding. It removed the modelType property from the Resource struct, changed the generic constraint from Codable to Decodable, and removed the throws keyword from the loadProducts function.
Whether you agree with these changes or not is a separate discussion. The important thing to understand is that providing scaffolding does not necessarily mean that the AI agent will follow everything exactly as you have defined it.
This is why reviewing the code generated by the AI agent is still an important part of the development process.
One of the reasons I like scaffolding is that it allows me to think about the architecture of my application before handing over the implementation to an AI agent.
How much scaffolding you need really depends on how much control you want over the implementation. Sometimes, creating a few files and folders is sufficient. Other times, you may want to go a little deeper and define types, function signatures, dependencies, and even the responsibilities of individual functions.
Scaffolding does not replace AGENTS.md or specification documents. All three can work together to provide better direction to the AI agent.
Of course, we still need to review the code generated by the AI agent. As you saw in this article, the agent followed the overall structure but also made some changes to our original scaffolding.
Just because AI agents are capable of building an entire application does not mean we have to delegate all our architectural decisions to them. Personally, I enjoy the process of designing and architecting applications, and scaffolding allows me to continue doing that while taking advantage of AI agents.
Become an AzamSharp School member and get access to more than 250 hours of practical courses covering SwiftUI, SwiftData, testing, architecture, AI, machine learning, and more.
Your membership also includes access to AzamSharp books, live workshops, office hours, and new content added regularly.