Software Architecture is more important right now than ever.
It's what ensures our code can scale.
Code with a poor foundation makes delivering new features more complicated. Whether a human is programming or an agent is.
And it also affects our perception of agentic programming.
Let's look at an example.
Agentic programming with poor architecture
Given the code structure:
βΊ ecommerce/
βββ src/
β βββ controllers/
β β βββ ProductController # 2847 lines
β β βββ CartController # 1923 lines
β β βββ OrderController # 3156 lines
β β βββ UserController # 1542 lines
β β βββ PaymentController # 2234 lines
β β βββ ShippingController # 1876 lines
β βββ models/
β β βββ Product # Just DB schema
β β βββ Cart
β β βββ Order
β β βββ User
β β βββ Payment
β βββ routes/
β β βββ index
β βββ utils/
β β βββ helpers # Random functions
β βββ index
βββ Makefile
When giving the agent the prompt:
Add suggested products to the user's cart
The most likely change it will make is:
βΊ ecommerce/
βββ src/
β βββ controllers/
β β βββ ProductController # 2847 lines
- β β βββ CartController # 1923 lines
+ β β βββ CartController # 2387 lines (+464)
β β βββ OrderController # 3156 lines
β β βββ UserController # 1542 lines
β β¦
βββ Makefile
Basically what the agent does is respect the architecture we have. In this case, an MVC architecture.
And that's not badβdepending on the context, it can be a good thing:
- When we're building a new application and want to explore.
- When we're fixing a bug as quickly as possible.
But it does have quite a few disadvantages if we have an application that requires maintenance over time:
- The agent has consumed a lot of context to perform this task since it keeps loading those massive files.
- The more features we add, the slower the agents will run.
- The probability of hallucination increases and, therefore, the risk that it won't do what we expect.
If you've only tried agentic programming with this type of code, your opinion on programming with AI is most likely: "It works well, but you have to be very careful and review everything it does."
Agentic programming with good architecture
Now let's see what happens with a structure based on Hexagonal Architecture and DDD:
βΊ ecommerce/
βββ src/
β βββ app/
β β βββ api/
β β βββ cart/
β β βββ orders/
β β βββ products/
β βββ contexts/
β βββ ecommerce/
β β βββ cart/
β β β βββ application/
β β β βββ domain/
β β β βββ infrastructure/
β β βββ orders/
β β βββ products/
β βββ backoffice/
β βββ analytics/
β βββ inventory/
βββ tests/
β βββ app/
β β βββ api/
β β βββ cart/
β β βββ orders/
β β βββ products/
β βββ contexts/
β βββ ecommerce/
β β βββ cart/
β β βββ orders/
β β βββ products/
β βββ backoffice/
β βββ analytics/
β βββ inventory/
βββ Makefile
When applying the same prompt, the result would be:
βΊ ecommerce/
βββ src/
β βββ app/
β β βββ api/
β β βββ cart/
β β βββ orders/
- β β βββ products/
+ β β βββ products/
+ β β βββ suggested-products/ # +18 lines
β βββ contexts/
β βββ ecommerce/
β β βββ cart/
β β βββ orders/
- β β βββ products/
+ β β βββ products/
+ β β βββ suggested-products/
+ β β βββ application/
+ β β β βββ ProductSuggester # +45 lines
+ β β βββ domain/
+ β β β βββ SuggestedProduct # +23 lines
+ β β βββ infrastructure/ # +38 lines
β βββ backoffice/
β βββ analytics/
β βββ inventory/
βββ tests/
β βββ app/
β β βββ api/
β β βββ cart/
β β βββ orders/
- β β βββ products/
+ β β βββ products/
+ β β βββ suggested-products/ # +24 lines
β βββ contexts/
β βββ ecommerce/
β β βββ cart/
β β βββ orders/
- β β βββ products/
+ β β βββ products/
+ β β βββ suggested-products/ # +52 lines
β βββ backoffice/
β βββ analytics/
β βββ inventory/
βββ Makefile
Here we see that, just like before, it has respected the existing architecture.
But there's a big difference: it's much more likely that the prompt has worked perfectly on the first try:
- It has to touch more files, but much smaller and more focused ones. Therefore, the agent performs better.
- By having small and well-named files, the agent can navigate the code better and find where to make changes without loading everything into context.
- By having a separation by modules with small and atomic aggregates, it simply had to create a new one replicating the existing structure.
- By having a good test base, it created tests for the new use case, verifying that it works correctly.
- By having domain events, those suggestions can be calculated asynchronously without having to modify the product code. All this thanks to respecting the OCP principle of SOLID.
Additionally, the created code probably looks very similar to what anyone on the team would have written.
What AI is doing is making best practices shine much brighter. This is where agentic programming excels.
Conclusion
Agentic programming doesn't come to replace best practices, but to amplify them.
Everything that affects humans when scaling and maintaining code affects AI agents in the same way.
If your code already has good architecture, agents will help you scale it. If it doesn't, there has never been a better time to implement it.
