Make the Plan a Value

22 points by jackdk 20 hours ago on lobsters | 4 comments

mayonnaise | 6 hours ago

Even in strict languages, the decoupling benefits are great enough that the extra memory use is often worth it.

If I got it right, Gary Bernhardt made this famous for the Ruby world some 10+ years ago through Functional Core Imperative Shell in his Destroy All Software series and his Boundaries talk.

One challenge I have found sometimes when trying to apply this is performance optimizations requiring IO to decide whether a plan goes one way or another. You can front load the IO, but then you make every plan pay a high fixed cost.

The answer might be breaking out into multiple smaller plans, which is awkward, or making your plan interpreter smarter, but a big point of FCIS was making the imperative shell dumb and exercisable with simple happy path testing.

It might be certain domains lend themselves better to FCIS than others.

[OP] jackdk | an hour ago

Isn't the idea of the executable plan the opposite? You have an imperative core (or subsystem) and you aim to isolate it by creating a functional shell for it?

Your point about needing a side-effect to decide which way a plan should go maps pretty cleanly onto whether a plan type needs to be a powerful structure like a monad or whether a simpler structure like a list of steps is enough. You might find you can still build a plan by describing the computation graph statically (so you'd have nodes for should_optimise? as well as nodes that are essentially control structures within your plan).

andyc | 3 hours ago

To untangle things, I resolved to make one of my favourite programming moves, “make the plan a value” or “decide what to do, and then do it”. This move decouples two parts of a program by introducing an intermediate data structure (a “plan”, or a value of a “plan type”) describing what is to be done before any action is taken.

FWIW in most instances I think of this as "the Interpreter pattern" from the GoF Design Patterns book

Although now when I look on the web, people writing about the interpreter pattern talk about parsing, which IMO is unnecessary for the interpreter pattern. I often express it like something like:

class Migrate:
  def __init__(foo, bar)
  def Run()

class Test:
  def __init__(s)
  def Run()

class OptionalWork:
  def __init__(i, j, k)
  def Run()

And then a "plan" can is list of objects with a Run() method (which is a value). And the harness can do error handling and logging, etc.

And yes the harness can be parameterized too by --dry-run, so that is useful too.

If you want, you can make a "composite" value obeying the Run() interface, which also contains other Runnables. You might run them on another thread, or on another machine, etc.

It's trivial to add an Retry wrapper too, and then if and for (but then you're getting a bit crazy)

Just like a real interpreter. No parsing involved!

[OP] jackdk | an hour ago

I was going to write a couple of sentences about the Interpreter Pattern, but it turned into too much of a tangent. I read that pattern as a way to provide a scripting-type interface to the programmer in a world where embeddable languages like Lua hadn't yet taken over. The backend of an interpreter will look very similar to an executable plan but I see it as a slightly different purpose: the plan is about providing a narrow waist between two parts of a program, rather than between the programmer and the compiled program.