Documentation

State

Discussion of state in Dataflow context.

State

Understanding states is the key to predictable program behaviors.

Dataflow context is not "pure". Everytime it executes, something might have changed, and the next time it executes we may have different results. On a larger scale, files and data may be created/removed, but on a smaller scale, graph variables may cause behavior change of execution.

In below example, we are incrementing the value of a module variable.

Declare: MyNumber

Get MyNumber
>| Add 1
>| Set MyNumber

When MyNumber is first created, it's assigned default value 0. The first time this snippet runs, MyNumber becomes 1. What about the second time it runs?

Once a variable is declared, it will only be created once, so its value won't reset. The second time Get MyNumber is executed it will get 1. So the final value after setting will be 2. This example illustrates how states can alter behavior of programs.

State is the current stored value of something that persists between executions and can affect future behavior. In other words, State is any data that a program remembers over time, so that what happens next can depend on what happened before.

In this example:

  • MyNumber is state
  • Its value changes over time (0 → 1 → 2)
  • Each run behaves differently because the state has changed

Usage

States are highly useful in dataflow context, and we have two examples below.

User Shopping Cart

In an e-commerce website, the "Shopping Cart" page may depend on current items - if it's empty, we don't show "Proceed to Checkout".

Empty Cart State

Your Cart

Your cart is empty.

Filled Cart State

Your Cart
Item A $10
Item B $15
Total: $25

App/Game Input Behavior

In a graphical application, when user clicks on a button, it can alter state of things, e.g. zoom level of a map.

Remarks

Usually states are not directly modified in the dataflow context directly but may get involved in a subgraph that's used by the dataflow context.

(Screenshot)