Tailoring Our Architectural Guardrails to a New Project
Published by Manuel Rivero on 22/11/2026
Introduction.
In a previous post, we described the minimal architectural guardrails we were using for katas in our deliberate practice sessions and as a starting point on a client project.
-
Domain code should not depend on infrastructure.
-
Production code should not have circular dependencies.
-
Production code should not depend on tests.
After three months working on a new client project, where we’re turning a PHP and JavaScript prototype into a React and TypeScript app with a Java backend, we’ve added several rules to the original three.
Some came from the architecture we chose for the project, while others came from problems we repeatedly found in code generated by coding agents.
In this post, we’ll describe the rules we’re currently enforcing in the backend and frontend, and explain why we introduced them.
Our backend rules.
The backend is written in Java. We use a ports-and-adapters architecture and organize the code around business contexts, with separate models for reads and writes within each context.
We are using ArchUnit to enforce our design rules. We currently have six rules.
1. Domain classes depend only on domain code and Java SE.
We enforce this by restricting domain dependencies to other domain classes and the Java standard library, with the validation exception explained in the next rule.
2. Our shared validations may use Apache Commons Validate.
Classes in com.dreamplace.cde.domain.shared.validation may also use org.apache.commons.lang3.Validate. We wrap calls to Validate in the shared validation package so we can translate its exceptions into our own domain exceptions. This lets us distinguish violations of preconditions, postconditions, and invariants in the domain code.
3. Domain packages must not have cyclic dependencies.
The first-level packages inside the domain must not form dependency cycles because we want to be able to change those packages independently.
4. Infrastructure packages must not have cyclic dependencies.
The first-level infrastructure packages have their own cycle check. A cycle between infrastructure packages wouldn’t be caught by the domain package check, so we check them separately.
5. Top-level application packages must not have cyclic dependencies.
We also check the first-level application packages for cycles.
For example, configuration can depend on infrastructure to wire up adapters. If infrastructure also starts depending on configuration, this check catches the cycle.
For all three cycle checks, we group each first-level package and its subpackages into one slice. A cycle confined to a single slice is outside the scope of these checks.
6. Domain source files must not import infrastructure.
We also check the Java source files for imports from infrastructure into the domain.
This may seem redundant given the first rule, but, as we explained in a previous post, ArchUnit’s bytecode analysis can miss dependencies that are visible in the source. Compile-time constants, for instance, can be inlined, so the resulting bytecode may no longer contain the dependency we want to reject. The import is still in the Java file, so we look for it there.
Our frontend rules.
In this project, the frontend is more complex than the backend, so we’ve ended up with more architectural rules for it.
We use React for the views and Reffects, which was inspired by ClojureScript’s re-frame framework, to handle events and update application state[1].
Reffects promotes a functional, event-driven, unidirectional architecture that separates state management, UI rendering, business logic, and side effects. It represents side effects as data instead of executing them inside event handlers. This keeps event handlers pure and separates business logic from the code that performs side effects, making the logic easier to test independently.
For React applications, Reffects Store provides state management integration, allowing components to receive the state they need through props.
This lets us keep the views dumb: they render what they receive through props and call callbacks in response to UI actions.
With the logic outside the views, we can test most of the application’s behavior just below the UI, a strategy known as subcutaneous testing.
That leaves us with only two things to test in the views:
- The view renders what we expect for the props it receives.
- UI actions translate into dispatching domain events we’ve registered in Reffects.
These are the architectural rules we have in the frontend:
1. Keeping views independent of Reffects.
Views must not access application state or selectors, subscribe to the store, dispatch events, import Reffects APIs, or use event constants.
2. Keeping subscribed components consistent.
Each Subscribed*.tsx component must assign its typedSubscribe boundary to a named Subscribed… constant and default-export that constant. This gives subscribed components a consistent structure.
3. No Reffects hooks.
We didn’t like the decision to add useSelector to Reffects. It allows a presentational component to subscribe directly to application state, making it depend on Reffects instead of receiving the state through props. That’s why our production code can’t import useSelector from reffects-store.
4. Improving Reffects with types.
We wrote adapters to make better use of TypeScript’s type checking when using Reffects. In the original JavaScript API, paths into application state are specified as strings, so it’s possible to reference paths that don’t exist. Our typed store boundaries let the TypeScript compiler catch these mistakes, while our event registration adapter prevents duplicate event handler registrations, another problem we could encounter with the JavaScript version.
Production code uses those boundaries for state, store, and subscribe. The only exceptions are the adapter modules themselves, which use the raw APIs. Event handlers go through our checked registration adapter, so production code can’t import registerEventHandler directly from reffects.
5. Keeping React hooks out of the application’s UI.
Thanks to Reffects, we don’t need React hooks in pages, shared components, the application shell, or application entry components. We have a rule that prevents their use there.
6. Keeping asynchronous work out of views.
We want to keep views as dumb as possible, so we ban await expressions in them. This catches await expressions introduced directly in views, for example when waiting for a request to complete.
7. Keeping infrastructure out of the UI.
Application UI and page routing must not depend directly on files in src/app/infrastructure/.
8. Keeping the original dependency checks.
The frontend production dependency graph must contain no cycles, and files under src/ must not depend on files under tests/.
Here, cycle detection works at source-file level, whereas the backend rules described above work at package level.
The rule against depending on tests is the same one we started with. Test builders belong in tests. We still don’t want an agent to use one to instantiate an object in production code, however convenient it may seem.
9. Rules for tests.
The tests have a few rules of their own.
-
Tests using our shared component render helpers must render a subscribed component instead of a view directly.
-
Tests must build application state with test builders, rather than importing
createDefaultAppState. -
Tests must use the
typedStorewrapper instead of the raw Reffects store. -
Tests must not use
Reflection.setto modify values.
When we use the shared render helpers, going through the subscribed component lets us exercise the wiring as well as the rendering.
We use test data builders for two reasons. They save us from repeating the structure of the same objects across tests. They also let each test specify just the values that matter for that case and leave the rest to the builder.
We also enforce a convention to make tests easier to read: top-level test-helper function declarations must appear after all the top-level describe blocks. This lets us read the test descriptions before getting into the helper implementations.
10. Avoiding ReturnType<typeof …>.
We saw coding agents using ReturnType<typeof …> as a shortcut to avoid naming types explicitly. We found that this made the code harder to read, so we decided to ban it in both production code and tests and require named types or aliases instead.
11. Rules for composing pages.
-
Keep
PageRoutingstatic. It maps routes to page definitions. Feature decisions stay in the features, so we can find them alongside the behavior they control. In review, we check that new entries contain only composition metadata. -
Resolve the route in
SubscribedApp. This is where we read the current route and look up itsPageDefinition.Appreceives the definition and renders the active page insideSubscribedAppShell. This keeps the store wiring in the subscribed component, uses our typed Reffects adapters, and passes the page definition toAppthrough props. -
Keep shared state in the shell. The shell owns routing, authentication, shared hotel context, footer data, and global UI. These concerns belong to the application across pages.
-
Let each page own its data. Each page feature is responsible for fetching its payload, interpreting it, and handling the request lifecycle. When it needs its own state, it gets a namespace in
AppState. This gives us one place to look when we need to change how a page loads. Placeholder pages only get a namespace when they need one. We test the request flow to verify that each page handles its own requests.
Complementing the architectural rules with ESLint
In the previous post, we said we preferred architecture tests for design rules because we found them easier to read. However, we also use ESLint in the frontend. We didn’t see a reason to write architecture tests for rules that ESLint already supports and that we could enable with a few lines of configuration.
We started with the recommended TypeScript rules and the Vite configuration for React Refresh, then added three rules of our own for TypeScript and TSX files:
-
We require braces around control flow bodies. That includes an
ifwith a single statement. -
We report unused variables and parameters. A name starting with
_is allowed to go unused, so we can keep a parameter required by a callback signature and show that we don’t use it. -
We allow at most one statement per line. Put two declarations on the same line and ESLint will ask you to split them.
Conclusions.
Over three months, we went from three basic rules to a larger set of checks adapted to the architecture and conventions of our project.
We didn’t introduce all these rules for the same reasons. Some protect architectural boundaries, like keeping infrastructure out of the domain. Others support the way we test the application, like requiring test data builders so each test can show just the values that matter. Some enforce our opinion on how Reffects should be used. And several came from seeing coding agents repeatedly introduce patterns we didn’t want in the code.
These rules are tailored to our project. Your mileage may vary.
Acknowledgements.
Thank you to Antonio de la Torre for giving me feedback about several drafts of this post.
References.
-
Our Architectural Guardrails for AI-Generated Code, Manuel Rivero.
-
A Flash from the Past: Why We Built Reffects, Manuel Rivero.
-
Cleaning Up AI Slop with the Help of AI, Piece by Piece, Manuel Rivero.
Notes.
[1] For the story behind Reffects, see A Flash from the Past: Why We Built Reffects. The Reffects README describes the framework and links to its packages and examples.