TwistingTwists

TwistingTwists

Finite State machine - when to use and when not to?

What are use cases when Finite state machines should not be used?

  1. MUST NOT USE in THESE CASES
  • Must not use because ______

  1. MAY USE or MAY NOT use
  • Pro of using FSM are : (a) ____ (b) ______ (c) _____
  • Cons of using FSM are : (a) ____ (b) ______ (c) _____

  1. MUST USE in THESE cases
  • because ____

This is a general discussion to gauge where are FSM’s useful. I am new to this concept and trying to understand if I should refractor some of existing code to use FSM’s.

Most Liked

al2o3cr

al2o3cr

You’ll get significantly better responses, IMO, if you ask about your specific use-case versus sifting through mountains of speculation. That said, here’s a small pile of speculation…

Must not use:

  • things where the space of states is infinite. You wouldn’t use an FSM for “current account balance”

Pros of using:

  • FSMs provide a shared vocabulary for talking about complicated code. Snazzier libraries will even let you run the documentation!

Cons of using:

  • using an FSM with the wrong “kind” of implementation can be painful. For instance, using something compile-time like :gen_statem or Finitomata (linked above) but with states/transitions defined at runtime
  • an FSM is good at enforcing business rules in software like “do X then Y”, but that can cause friction at a company where the real-world process is “do X then Y, unless it’s Tuesday then do Z first instead and then Y and then X”

“Must use”:

  • It depends on what you define as “an FSM”: is a “draft” boolean that starts out false and is deliberately set to true later an FSM? It has two states and a transition between them. :thinking:
tfwright

tfwright

I think any time you have to model data with 3 more more exclusive states, you are writing a FSM. So I take this question to essentially be one of implementation, and specifically whether to use libraries or not.

I used FSM libraries heavily in Rails, the most recent legacy app I rewrote was just using timestamps to determine states which was very messy, difficult to reason about, and bug prone. Converting to proper/explicit state machines was a no brainer for me. I think there was briefly a trend against FSMs for whatever reason, I remember reading this article at some point. It skims the Rails history topic of observers which essentially was part of the perennial struggle with the type of callback hell that could affect Rails apps. But again, I don’t think there is any avoiding state machines in most apps of any significant complexity.

I feel like I end up writing a version of this on every other thread I reply on, but one of the things I deeply appreciate Phoenix and Ecto’s design in particular is that, in forcing you to be a bit more explicit in how you handle this kind of logic, and arming you with the context/schema separation off the bat, these kinds of issues I faced constantly as a Rails dev are much weaker threats. So far I have just been using context functions with pattern matching to control transition logic, as @sasajuric advises in the disclaimer of the top of his fsm library, which is I found when I first went looking for a way to handle this logic in Phoenix apps. So far that has been working well for me, although of course as others have mentioned above it can get a bit sticky when there is a lot of conditional logic around transitions. But in my experience libraries can only help so much with that kind of complexity since it is unique to each application. Basically I find the situation comparable to the factory pattern, where the small benefit in terms of reduced boilerplate and enforced consistency that you get from a 3rd party lib is not worth the simplicity of just sticking with POCFs (plain old context functions).

Where Next?

Popular in Discussions Top

jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
New
blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
100phlecs
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement