The Itinera bookJava edition

Part 1: Why Itinera, Chapter 1

But why Itinera?

What you want to do

Fair question. You already know how to write a checkout, so why would you hand its flow to a framework? Let's not argue about it in the abstract. Let's look at code you have probably written yourself, and see what it costs.

You run a small online shop. When a customer checks out, the shop reserves the goods, charges the card, ships the parcel and tells the customer how it went. You want to answer simple questions about any order: what the shop tried, in what order, what it did when something failed, and who was told.

This chapter looks at a checkout written the usual way, in plain Java, by a competent developer, and asks it those questions. No Itinera yet: just the usual code, and four orders to try it on.

The code

The checkout is split the way most code bases split it: a module for payments, one for shipping, one for the checkout itself, and small ones for the log and the audit trail. Here they are sections of one file, marked by comments, so that the whole program fits on the page; in a real project each would be a file of its own, or a package. The bank, the warehouse, the carriers and the customer messages are faked inside the program, so it prints the same thing every time it runs. Errors travel the way they usually do in Java: thrown as exceptions, and caught by whichever method up the call stack has a try around the call. The bank's refusal, Declined, is a checked exception, so every method it may leave says so with throws Declined.

The checkout reserves, charges and ships, then tells the customer how it went:

Java, from the program ch01_checkout
/** Reserves, charges and ships an order, and tells the customer how it went. */
String checkout(Order order) throws Declined {
  Log.info("checkout " + order.order());
  try {
    String tracking = reserveChargeAndShip(order);
    messages.tell(order.customer(), "order " + order.order() + " is on its way: " + tracking);
    return tracking;
  } catch (Exception error) {
    Log.warn("checkout " + order.order() + " failed: " + error.getMessage());
    messages.tell(order.customer(), "order " + order.order() + " could not be completed");
    throw error;
  }
}

private String reserveChargeAndShip(Order order) throws Declined {
  warehouse.reserve(order.order());
  long amount = order.price() - order.discount().orElse(0L);
  Payments.takePayment(bank, order.card(), amount);
  return Shipping.ship(main, backup, order.order(), order.weight())
      .orElseThrow(() -> new IllegalStateException("no carrier could take it"));
}

Taking the payment charges the card. When the bank refuses it because the card has expired, it asks the bank for the new card and charges that one. It also records every successful charge in the audit trail:

Java, from the program ch01_checkout
/**
 * Charges the card. An expired card is renewed with the bank and charged once more. Every
 * successful charge is audited.
 */
static String takePayment(Bank bank, String card, long amount) throws Declined {
  String receipt;
  try {
    receipt = bank.charge(card, amount);
  } catch (Declined declined) {
    if (declined.why != Why.EXPIRED_CARD) throw declined;
    Log.info("card " + card + " expired, renewing it");
    receipt = bank.charge(bank.renewedCard(card), amount);
  }
  Audit.record("charged " + amount + ", receipt " + receipt);
  return receipt;
}

Shipping hands the parcel to the main carrier, and to a backup carrier when the main one cannot take it. It returns the tracking number, or an empty Optional when neither carrier takes the parcel:

Java, from the program ch01_checkout
/**
 * Ships through the main carrier, or through the backup carrier when the main one cannot take the
 * parcel. Returns the tracking number, or nothing when neither can take it.
 */
static Optional<String> ship(Carrier main, Carrier backup, String order, long weight) {
  try {
    return Optional.of(main.ship(order, weight));
  } catch (IllegalStateException refused) {
    Log.warn(
        main.name
            + " cannot take "
            + order
            + ": "
            + refused.getMessage()
            + "; trying "
            + backup.name);
    try {
      return Optional.of(backup.ship(order, weight));
    } catch (IllegalStateException alsoRefused) {
      return Optional.empty();
    }
  }
}

The program sets up the fakes and checks out four orders. Each card ends with the year it expires, and the bank, set up for the year 2026 with a limit of 1,000, refuses a card that expired before this year, and any charge above its limit. The main carrier, Rapid, takes parcels up to 20 kg; the backup carrier, Steady, up to 50 kg. So the orders' own data decides what happens to them: Ann's goes through without trouble. Ben's card expired in 2024, and the bank's new card for him expires in 2029. Cat's parcel weighs 30 kg, too heavy for the main carrier. Dan's card has expired too, and his order of 1,200 is above the bank's limit, so the bank refuses the new card as well.

Java, from the program ch01_checkout
public static void main(String[] args) {
  var shop =
      new Shop(
          new Bank(2026, 1000),
          new Warehouse(List.of()),
          new Carrier("Rapid", "T", 20),
          new Carrier("Steady", "B", 50),
          new Messages());
  var orders =
      List.of(
          new Order("A-1", "Ann", "ann-card-2028", 40, Optional.of(5L), 2),
          new Order("A-2", "Ben", "ben-card-2024", 25, Optional.empty(), 1),
          new Order("A-3", "Cat", "cat-card-2027", 30, Optional.empty(), 30),
          new Order("A-4", "Dan", "dan-card-2025", 1200, Optional.empty(), 3));
  for (var order : orders) {
    try {
      System.out.println(order.order() + ": shipped, " + shop.checkout(order));
    } catch (Exception error) {
      System.out.println(order.order() + ": failed, " + error.getMessage());
    }
    System.out.println();
  }
}

It prints:

What ch01_checkout prints, recorded in ch01_checkout.txt
INFO checkout A-1
AUDIT charged 35, receipt R-ann-card-2028-35
to Ann: order A-1 is on its way: T-A-1
A-1: shipped, T-A-1

INFO checkout A-2
INFO card ben-card-2024 expired, renewing it
AUDIT charged 25, receipt R-ben-card-2029-25
to Ben: order A-2 is on its way: T-A-2
A-2: shipped, T-A-2

INFO checkout A-3
AUDIT charged 30, receipt R-cat-card-2027-30
WARN Rapid cannot take A-3: 30 kg is over its 20 kg limit; trying Steady
to Cat: order A-3 is on its way: B-A-3
A-3: shipped, B-A-3

INFO checkout A-4
INFO card dan-card-2025 expired, renewing it
WARN checkout A-4 failed: payment declined
to Dan: order A-4 could not be completed
A-4: failed, payment declined

How it works

Every piece of this code is reasonable. We would have written it this way too. Now try to answer some questions about it.

  1. In what order does a checkout do its work? This one is easy: reserveChargeAndShip says it in a few lines. But each of those lines hides decisions of its own, and nothing in the checkout shows them.
  2. What happens to an order when the card is declined twice? The checkout does not say. You follow the charge into the payments module. There, the catch in takePayment looks at why the bank refused: a card declined because it has expired is renewed with the bank and charged once more. If the bank refuses that second charge, nothing in takePayment catches it. The Declined thrown by the second bank.charge(...) leaves takePayment, passes through reserveChargeAndShip, which names it only in its throws, and lands in the catch of checkout, which logs a warning, tells the customer and throws it again for whoever called. A card refused for any other reason is never tried again: the throw declined at the top of the catch sends the refusal on at once. To learn this you read three methods in two modules, and the bank's Why, its list of reasons for refusing a charge. The throws Declined on takePayment and reserveChargeAndShip says only that some refusal may leave them, not which one: nothing in their signatures tells the first refusal, which takePayment catches, from the second, which passes through. Dan's order shows the answer, but its log does not say that it was the new card the bank refused.
  3. What happens when the main carrier cannot take the parcel? The shipping module tries the backup carrier. If the backup cannot take the parcel either, its error is thrown away: the inner catch turns it into an empty Optional, and the reason goes with it. The log shows only the main carrier's trouble, and the checkout reports "no carrier could take it", without saying why.
  4. Is every successful charge audited? Only the payments module tells you. The audit happens at the end of the payment method, after the renewal. Reading the checkout, you would never know that charges are audited at all.
  5. When is the customer told, and what? At the end of the checkout, whether the order went through or not. On a failure, the customer is told only that the order could not be completed. Why it failed is in the log, where each module writes what its author thought worth writing, in its own way: the checkout logs failures, the payments module logs renewals, the shipping module logs the switch to the backup carrier.

The code mixes two kinds of decision. Business rules decide what the shop does: an order is charged its price less any discount, goods are reserved before they are shipped, a parcel goes to a carrier. Flow control decides what happens around that work: renew the card and try once more, fall back to another carrier, give up, audit, tell the customer. Logging is woven through both. Every flow decision lives inside a business method, so the only way to find it is to read that method, and the one it calls, and the one after.

That is the answer to "why Itinera?": the hardest part of understanding a system like this is knowing what is going on. Answering one question means reading every module, and a decision is easy to miss: the audit tucked into the payment method, the backup carrier's error dropped, the second refusal that travels up without a word. Changing a decision is harder still, because you first have to find every place that makes it.

The shop is small, and you can still read all of it. A system that has grown for years, across teams and repositories, has the same problem with no one left who has read everything, and agents changing it faster than anyone can follow. The Itinera site tells that larger story on Why Itinera.

The next chapter writes the same checkout with Itinera, where every flow decision has one place to live, and answers each of these questions by reading one declaration.

Outcomes

We have shown how flow decisions hide inside business code, so that answering a simple question about one order means reading every module it passes through.

The usual way With Itinera
Each method usually decides, inside its own code, what happens when something fails, so the flow is spread across every module it calls. The flow is declared in one workflow, and chapter 2 answers every question of this chapter from it.
Each module often logs what its author thought worth logging, in its own way, so the log rarely tells the whole story. One reporter attached to the workflow writes the log of every journey, in one format.