The Itinera bookKotlin edition

Part 5: Designing with Itinera, Chapter 39

Deciding where a decision belongs

What you want to do

The shop will keep gaining rules, and for each new one you want to know where it belongs: in a step, in a rescue step, in the workflow or in a policy.

The code

Every decision the shop has made so far has its place. The workflow class, as chapter 37's program has it, names the steps, their policies, the rescue step and the retry budget in its annotations. Its methods are the role implementations of chapter 19 and chapter 23, the ID generator of chapter 11, the input adapter of chapter 21, and the providers of chapter 12, chapter 13, chapter 22 and chapter 35:

Kotlin, from the program ch37_listing
/**
 * The shop's workflow: it looks the order up, and finishes an order already completed. Otherwise it
 * works out what the order costs, then charges it, repairing a charge that fails and auditing one
 * that succeeds, then ships, and shipping may be tried once more. Once a journey has ended, the
 * customer is told, through the channel the shop configures.
 */
@Workflow(name = "orders", reporters = [Log::class, Desk::class], policies = [Notify::class])
@WithStep(Lookup::class, name = "lookup", policies = [AlreadyCompleted::class])
@WithStep(Price::class, name = "price")
@WithStep(
    Charge::class,
    name = "charge",
    rescue = RenewCard::class,
    policies = [Audit::class, Outage::class],
)
@WithStep(Ship::class, name = "ship", retryBudget = 1, policies = [FailedShipping::class])
class Orders(private val channel: String) : ItineraWorkflow, Messages, Refunds {
    override fun tell(customer: String, message: String) {
        println("$channel to $customer: $message")
    }

    override fun refund(receipt: String, amount: Long) {
        println("the bank gives $amount back on receipt $receipt")
    }

    /** The bank, as the payment client of the journey. */
    @Provider(key = "payments") fun bank(): Provided<Payments> = provide(Bank(thisYear = 2026))

    /** The bank's card registry in Europe, for the charge's rescue step only. */
    @Provider(steps = ["charge_rescue"])
    fun cards(): Provided<Cards> = provide(cardRegistry("eu"), "eu")

    /** A carrier for every attempt of a step: the main carrier first, the backup carrier after. */
    @Provider(perAttempt = true, key = "carrier")
    fun carriers(attempt: Attempt): Provided<Carrier> =
        if (attempt.isFirst) provide(MainCarrier()) else provide(BackupCarrier())

    /**
     * Gives the shipping the weight of the order's parcel, as the warehouse describes it. Every
     * other input of the shipping is read from the data bag.
     */
    @InputAdapter(steps = ["ship"])
    fun parcels(@Key key: String, @FromWorkflow parcel: Parcel): Supplied =
        if (key == "weight") supply(parcel.weight) else fromDataBag()

    /** Names each journey after its order. */
    @IdGenerator
    fun orderNumber(data: DataBag): String =
        data.read<String>("order").orNull() ?: error("an order without a number")

    /** The order book, which has the orders completed before today. */
    @Provider(key = "orderBook") fun ledger(): Provided<OrderBook> = provide(Ledger(listOf("A-13")))
}

What it names is as earlier chapters show it: the lookup, AlreadyCompleted and OrderBook in chapter 22, the pricing in chapter 20, the charge in chapter 33, RenewCard, Payments, Cards and cardRegistry in chapter 35, Audit and Outage in chapter 34, the shipping in chapter 17, Carrier in chapter 12, with the carriers in chapter 13, Notify and Messages in chapter 19, FailedShipping and Refunds in chapter 23, Parcel in chapter 21, the log in chapter 33, chapter 35 and chapter 36, and the desk in chapter 11. Nothing runs in this chapter, so nothing is printed.

Decision Where it lives Why there
Whether the order was already completed the lookup step reading the order book is the business's work
Ending the journey of an order already completed the alreadyCompleted policy it decides what happens after the lookup's outcome
The amount, less any discount the price step it is a rule of the business
Whether the card can be charged the charge step it is what the business does with the order
Renewing an expired card the rescue step, charge_rescue it repairs why the charge failed, so the charge can run once more
Giving up a charge that ended in the bank's error the outage policy it decides what happens after an error the charge didn't handle
Writing every charge down the audit policy it reacts to an outcome, and changes nothing in the charge
A gift card has nothing to ship the ship step, which skips only the step knows its domain well enough
Shipping tried twice, with another carrier the second time the workflow: a retry budget, and a provider for every attempt how often to try, and with what, is the plan, not the rule
A parcel over the shop's limit the ship step fails it, and the failedShipping policy gives it up before a retry the limit is the business's; giving up early is a decision after an outcome
Refunding a shipping given up the failedShipping policy, through a role it reacts to a step given up, and acts on the outside world
Telling the customer the notify policy, through a role it reacts to how the journey ended
The weight out of the warehouse's parcel the workflow's input adapter it changes the form of a step's data, and decides nothing

How it works

Ask these questions about a new decision, in this order. The first answer that fits says where it belongs.

flowchart TD
  accTitle: Where a decision belongs
  accDescr: A decision that repairs why a step failed goes in a rescue step. Otherwise, a decision about what the business does goes in a step. Otherwise, how often a step is tried, and which services it gets, goes in the workflow. Anything else, deciding what happens after an outcome or acting on the outside world after one, goes in a policy's hook.
  Q1{Does it repair why a step failed?} -->|yes| R[A rescue step]
  Q1 -->|no| Q2{Is it what the business does?}
  Q2 -->|yes| S[A step]
  Q2 -->|no| Q3{Is it how often to try, or with what?}
  Q3 -->|yes| W[The workflow]
  Q3 -->|no| P[A policy's hook]
Diagram: Where a decision belongs

The diagram asks three questions in turn, with four answers:

  1. A decision that repairs why a step failed, so that the step can run once more, belongs in a rescue step.
  2. Otherwise, a decision about what the business does, such as pricing, charging or shipping, belongs in a step.
  3. Otherwise, how often a step is tried, and which services it gets, belongs in the workflow: in its annotations, such as retryBudget, and in its providers.
  4. Otherwise, the decision says what happens after an outcome, or acts on the outside world after one, and belongs in a policy's hook.

Every decision in the table answers one of these questions; the adapter, which decides nothing, only changes the form of a step's data. The ID generator and the role implementations decide nothing about the flow either. Each place holds one kind of decision, and the journey records each one where it was taken: a step's outcome, a rescue step's repair, a retry the workflow allows, and the hook a policy ran.

But a rule put in the wrong place often still runs, and nothing in the shop's orders says so.

Outcomes

We have shown how to place a new decision of the shop.

The usual way With Itinera
Where a new rule goes is often decided by whoever writes it, next to the code they happen to be reading. The workflow's declaration shows where each of the shop's decisions lives, one kind of decision to each place.