Architecture Policy
Type: Policy. Principles for organizing code, assigning responsibilities, and working with dependencies.
This policy sets out a common approach to organizing code: how to assign responsibilities to different parts of a project and arrange their interaction. Each project's architecture documentation describes its particular structure.
The principles help explain who owns a behavior, what it depends on, and what an alteration will affect. Their application takes account of the task, the application's complexity, and decisions already made in the project.
Module responsibilities
A module brings together code that serves one clear purpose. That purpose explains its name, its place in the project, and its connections to other modules.
For example, calculating a price, saving preferences, and displaying a form are different tasks. Separating them allows storage or presentation to change while the calculation stays the same. In a small component, a few local functions may provide this separation; separate layers and directories become useful as the need arises.
Finding a place for code starts with understanding its role. Existing architecture documentation and nearby modules help identify a suitable home. If several materially different options remain after examining them, discussing the intent with the project owner helps settle the decision before changing code.
Dependencies
Domain rules remain independent of how data enters the application, is stored, and is displayed. A change of database, HTTP client, or UI framework can then affect the corresponding integration while core calculations stay the same.
An interaction with an external system can be expressed through a small interface that describes the application's needs. An implementation connects that interface to a particular technology. Such an interface is useful where the interaction has a distinct responsibility; the number of wrappers alone says little about architectural quality.
In an application organized into layers, the roles might look like this:
| Role | Responsibility | Examples |
|---|---|---|
| Domain logic | Concepts, calculations, and rules of the task | Price calculation, checking domain conditions |
| Application scenarios | Sequences of actions and use of external capabilities | Loading data, saving a result |
| Integrations | Work with particular technologies and conversion of external data | HTTP, filesystem, storage |
| Presentation | Displaying data and handling user interaction | Form, table, loading state |
| Composition and startup | Selecting implementations, resolving settings, and connecting parts | Entry point, preparing dependencies |
This table illustrates a division of responsibilities. Layer names, directories, and separate modules follow the project's structure. A simple component or library may use fewer parts.
For example, in a project with domain, application, infrastructure, presentation, and bootstrap layers, domain rules stay in domain, scenarios use them through application, and concrete integrations are connected when the application is assembled. Presentation calls the scenarios while details of the external API remain in the integration. Shared pure utilities have their own technical role.
Settings and external data
Defaults and pure configuration can be used across an application when reading them is deterministic and performs no external operations. Domain types help express the meaning of such settings.
For example, if a project uses src/config.ts for static configuration, that module may contain defaults and resource limits. Reading environment variables, files, and network resources stays in application setup code; the resolved values are passed explicitly to consumers.
Types from external SDKs and transport formats are best converted near the integration. Internal modules can then use stable application concepts and depend less on changes made by the provider.
Contract sources
Each contract has a primary source that defines its exact form: a signature, data format, or set of values. Other representations rely on that source. Documentation explains meaning and usage, while tests check observable behavior.
| Subject | Source of the exact form | How documentation helps |
|---|---|---|
| Domain models and rules | Types, value models, and validation functions | Explains domain meaning |
| Scenarios, external capabilities, and errors | Interfaces and input/output types | Describes a scenario and its result |
| Component properties and events | Property declaration or exported component type | Shows usage and interaction |
| Presentation model API | Exported factory or model interface | Explains state, actions, and lifetime |
| Page URLs and HTTP behavior | Route handler and its inputs | Explains parameters, statuses, and examples |
| External API | Provider's versioned schema | Identifies the version in use and how to update it |
| Request proxying | Handler and integration module | Explains allowed requests and response forwarding |
| Preference storage | Storage interface, format consumer, and adapter | Describes keys, versions, validation, and recovery |
| npm commands | scripts in package.json |
Explains how to run commands and what they do |
| Dependency composition | Application composition code | Shows implementation selection and data flow |
A contract change starts at its primary source. Consumers, tests, generated representations, and documentation are updated with it so that all parts describe the same behavior. Generated declarations are reproduced from the source contract.
For persisted preferences, it helps to distinguish three tasks: the application describes the storage capability, the consumer defines its data key, version, and validation, and the adapter handles serialization and environment details. This separation supports recovery from damaged data and allows storage technology to change independently of data meaning.
UI applications
In a user interface, it is especially useful to distinguish domain rules, user scenarios, and display state. They change for different reasons: a calculation rule follows the product's task, while an open panel or selected row follows the current interaction.
Domain calculations remain ordinary code that can be called and checked without a UI framework, DOM, or network client. An application scenario coordinates the work and returns a result. Presentation displays that result and handles loading, error, empty, selected, and expanded states.
HTTP, WebSocket, storage, and external SDK details are concentrated in integrations. Converting data and errors near the request lets a component work with a meaningful scenario result. For example, the component displays that no record was found, while the integration describes which HTTP status corresponds to that outcome.
Components and presentation models
A component combines markup, local state, and actions related to its presentation. For a reusable UI component, a clear API of properties, callbacks, and markup snippets reduces coupling to a particular page or domain.
Local state stays close to the component while that keeps the code readable. If a component coordinates several actions, asynchronous operations, or repeated reactive behavior, a presentation model can collect that work in one place. An extracted model retains its presentation role even without markup.
Pure calculations, parsing, and normalization can form a separate computation module. This extraction helps when it makes behavior clearer and independently testable. A simple calculation can stay near its use.
User features
A coherent user scenario helps identify a module's components, state, and local data transformations. Search, for example, may combine query input, a result list, and record selection.
A page assembles several such features and coordinates their interaction. A shared API allows them to connect through the page or an application scenario while each module retains its own internal structure.
Shared code emerges when there is a clear common task and real consumers. Reusing a component does not by itself make it independent of the domain: a product selection form retains knowledge of products even when used on several pages.
The abstraction policy discusses shared code in more detail.
State ownership
Mutable state has a clear owner that creates it, provides actions for changing it, and manages the lifetime of associated resources. Keeping it close to its use usually makes these connections clearer: in a component, a related scenario, a page, or the whole application.
Shared state emerges when independent consumers actually need it. An API with selection or reload actions helps keep data consistent. The location and lifetime of that state remain explicit, including when one instance is shared across the application.
Values entirely determined by other data are naturally expressed as calculations. This preserves a single source for a value and removes the need to synchronize several mutable copies.
Asynchronous operations
An asynchronous operation's model reflects its actual states and valid transitions. For example:
type AsyncState<T> =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'ready'; value: T }
| { status: 'error'; error: AppError };
The specific form depends on the scenario. Explicit states help explain which data is available during loading or an error and avoid accidental contradictions between independent flags.
A request has an owner for its result. When requests compete, cancellation or checking whether a response is still current helps retain the result of the user's latest action.
External effects and resources
A user action explicitly starts its associated scenario. Calculations express dependencies between values, while external effects connect state to the network, DOM, storage, and other systems.
A timer, subscription, event listener or Worker lives with the owner of the operation. Resources must be released when the corresponding lifecycle ends. Considering creation and release together makes behavior easier to understand when a page reopens, an action is canceled or a component is destroyed.
The application of these principles in Svelte 5 and SvelteKit is described in Languages and platforms.
File names and placement
A file's name and location help explain its role before reading the implementation. Existing project conventions provide guidance for new code; the naming policy explains the general principles for choosing names.
A directory brings together code with a clear shared purpose. Names such as utils, helpers, or shared are useful only alongside a clear description of their contents. Framework directories such as src/lib and src/routes already have a purpose in its conventions.
A new directory or a file move is useful when it expresses a real change in responsibility. That reason becomes part of the change description and architecture documentation. An existing directory alone does not explain which behavior belongs there.
Architecture changes
Project architecture documentation describes the system's current structure. An ADR can preserve a decision's history: its context, the options considered, and the reasons for the choice. The documentation policy explains these document roles in more detail.
Moving code is justified when it clarifies responsibility or corrects a mismatch with the project's current structure. A shorter import or directory symmetry can be convenient, but the code's role explains the main reason for the change.
Small, sequential changes allow moves, renaming, and behavioral changes to be assessed separately. Observable application properties, external operation order, and public interfaces are preserved unless the task explicitly changes them. The change policy describes this approach in more detail.
Significant interfaces include exported module APIs, component properties and handlers, URLs, HTTP statuses, storage formats, Worker messages, environment variables and startup commands. The process for changing them is described in Contract sources.
Several questions help assess a decision:
- What task does this code perform, and who owns it?
- Where does the state live, and how long does it live?
- Which dependencies does this work need?
- What will change if an external system or UI framework is replaced?
- Which observable properties confirm the result?