Protect changes and answer questions
The mug shop in Keep the facts behind the numbers records accepted orders as events and uses them to calculate available stock.
An event is a fact the application has accepted. That makes the boundary around it important: a customer cannot declare that the shop accepted an order simply by sending an OrderPlaced event.
Consider a customer asking for four mugs when only three remain. Asking for an order and having an accepted order are different things. The application needs a way to hear the request, check it, and refuse it without consuming stock.
Ask for a change with a command
Section titled “Ask for a change with a command”A command expresses an intention to change the application. PlaceOrder carries the information needed to request an order. For one mug, that information looks like this:
PlaceOrder { product = "ceramic mug", quantity = 1}A decision evaluates that command against the current state and the shop’s policy. The quantity must be positive, and enough mugs must be available. With three in stock, an order for one passes and an order for four fails.
Acceptance records OrderPlaced. Rejection leaves stock unchanged and records no OrderPlaced event. The command carries the intention; the event records the accepted fact.
NeoHaskell supplies the execution path: obtain the relevant state, evaluate the decision, and append the accepted events. The developer writes the conditions that express the shop’s promise. A website and an AI tool can submit the same command and use that same decision.
Make business decisions pure
Section titled “Make business decisions pure”NeoHaskell puts pure code at the center of application development. The business logic describes what should happen from the information it receives. A decision checks the command and current state, then describes acceptance or rejection. It cannot open a database connection, send a confirmation email, or call a payment provider.
For the shop’s stock rule, the same order and available stock produce the same acceptance or rejection. Testing whether four mugs can be ordered from a stock of three does not create an order in a database. It calculates the decision’s answer.
That boundary changes everyday development:
- Test thousands of cases without external consequences. Generate combinations of stock and requested quantities, including zero, negative values, and the last available mug. Check the decisions without a database, network connection, or email account.
- Reproduce failures. Keep the inputs that exposed a broken stock rule and run them again. Investigating that rule does not depend on a provider being available or a test database having the right contents.
- Refactor against observable behavior. Give the old and new stock rules the same cases and compare their decisions. Changes written by hand or by an AI agent can be checked against the shop’s promises.
- Change infrastructure without rewriting the policy. The stock rule contains no SQL or email-provider calls. Replacing storage or an email service leaves the conditions for accepting an order intact.
- Understand and reuse the logic. Read the inputs and the resulting decision without tracing hidden writes through other services. Extract a calculation or reuse a rule without carrying a database connection along with it.
Pure application logic is the normal way to build in NeoHaskell. The developer writes the rules and calculations; the language supplies the machinery that loads state and records accepted changes. Testing the business behavior does not require running that machinery around every rule.
How does the application do useful work?
Section titled “How does the application do useful work?”An accepted order still needs a confirmation email. Integrations perform effects outside the application logic. They package that work separately, so the shop’s decisions do not depend on the provider that performs it.
An integration worker knows how to do its task. An email worker receives the recipient and message and sends them. It does not decide whether an order is allowed, how much stock remains, or which customers should receive a confirmation. Those choices belong to the application’s pure logic.
The application can request that work after OrderPlaced has been recorded. If the email provider is unavailable, the order remains accepted. Changing how the email is delivered changes the integration, not the stock policy. The integration guides cover how to configure and run these workers.
Give each screen the answer it needs
Section titled “Give each screen the answer it needs”Once the order is accepted, different people need different answers. The next shopper needs available stock. The customer needs confirmation. Staff need to know how many orders await their attention.
A query maintains useful information derived from application state and exposes an answer to a caller. For the shop that started with three mugs and no orders, accepting an order for one produces these answers:
Opening stock: 3 mugs
Reading the stock answer does not place another order. It also does not require scanning the complete event history for each page visit: NeoHaskell updates query state as changes arrive, and the screen reads that maintained answer.
The same facts can support different questions. A customer display and a staff report can have different shapes without needing competing accounts of what happened. A new report can use the retained history to calculate another answer.
A displayed answer can lag behind a new change. That is why the stock shown on a screen is not permission to place an order: the command’s decision checks the relevant state when the request arrives.
Separating changes from questions is called CQRS. Commands and decisions control what may happen; queries organize what people need to know. Event Sourcing supplies the history from which the application derives its state.