The loopLesson 14 of 23

Can your Model say something untrue?

Find a state that makes no sense, then make it impossible to write.

Here is an order. The Model has a field for each thing that can be true of it: placing, receipt and error. Two of the buttons stand in for the shop answering, because nothing in the course can call a shop yet.

Predict

Read update in Order.ts, then press three buttons in your head, in this order: Place order, It failed, It worked.

What does the screen show at the end?

Check your answer

"Paid. Receipt #1042" and, under it, "Card declined."

Try it, then open the last row in the log. The Model holds a receipt and an error at once. No line is wrong: each handler set the fields it knew about. The Model had room for a state that can't exist, and three clicks found it.

Try it

  1. Three fields, each with two possibilities, make eight combinations. How many of the eight are real states of an order?

  2. Describe those states and nothing else. UiSchema.TaggedEnum made your messages. Use it for the Model too, and pass it as model: OrderModel:

    const OrderModel = UiSchema.TaggedEnum({
      Idle: {},
      Placing: {},
      Paid: { receipt: Schema.String },
      Failed: { reason: Schema.String }
    })

    In which of the four could you find a receipt?

  3. init becomes OrderModel.Idle(). A handler now moves to a state with $.set, for example $.set(OrderModel.Placing()). An answer only counts while an order is being placed, so write ClickedItWorked as $.model._tag === "Placing" ? $.set(OrderModel.Paid({ receipt: "#1042" })) : $.model, and ClickedItFailed the same way.

  4. Draw one branch per state: OrderModel.match(model, { Idle: () => ..., Placing: () => ..., Paid: ({ receipt }) => ..., Failed: ({ reason }) => ... }). Press Run and repeat the three clicks. Before you do: where would the view get an error from while the order is Paid?

About steps 1 and 4

Four: nothing yet, placing, paid, failed. The other four are nonsense, such as paid and failed together, or still placing with a receipt.

In step 4 it has nowhere to get one. Paid carries a receipt and nothing else. The view can't show both, and update can't store both, because there is no way to write it down.

What you found

The Model with three fields could say untrue things, so every handler and every line of the view had to be careful. It only takes one that isn't.

The second Model has exactly as many values as the order has states. A receipt exists only inside Paid, a reason only inside Failed. The check you would have written is gone, because what it checked for can't be built. And match has no default branch, so a state added later is an error until every view handles it. This is lesson 8 at work: a Schema describes data, so describe only data that can be true.

Those two buttons are still a fake. Something real has to call the shop, wait and come back with an answer. But update is pure. So how does anything happen?

14
Principle 14

Make impossible states impossible

Model the states a thing can be in, not a bag of flags. If the Model can't hold a contradiction, no code has to check for one.

ResultStarting

No messages yet

Use the example above. Each message that reaches update shows here, with what it changed.