background

About State Machines and Mushrooms

Has it ever happen to you, going to sleep at a reasonable time, with the perfect set up, ready to embrace Morpheus' world. As you close your eyes, confident to have a quick switch to the sleep mode, images start hunting you.

Your brain casts, one after another, pictures of something that has been impressed to you during the day. No matter how much you try to remove them, the main processor of your body keeps pushing them, more and more intrusive, and your dream of falling asleep quickly fades away...

I experience what I just described each time I go mushrooms picking. It's an activity that I love, it connects me with the nature and relaxes me. Each time I go, I know I will have to deal with this side effect. I guess it is related to the high concentration level one needs to detect our little fellows, hidden underneath leaves, behind a tree, camouflaged and hard to find.

As soon as I close my eyes, I see mushrooms everywhere 🍄 🍄 👀 ... ...and I can't sleep.

What's the link with State Machines then? Well, I kind of experienced the same effect when I recently stepped into the wild world of State Machine. In my last post, I described the creation of a file uploader using browser APIs. To implement it in an efficient and bullet proof way, we decided to go for a state machines approach. Stately, with XState (XState React Documentation), offers a very nice framework to use state machines in a React project, both in a local state context or in a global state (as a context). The documentation is rich and there is a library of existing State Machines that other developers publish, giving a nice source of inspiration and guide.

The learning curve is not so smooth, though. It is a wild world, as I already said, and it needs a switch of context and a different mindset. I was lucky enough that ICFM AG supported me by providing the mentoring of 📐Simon Bächler, a real expert in XState, working for ti&m. The moment you have the "click", and you understand how everything suddenly makes sense, the mushroom effect shows itself.

I see state machines everywhere! I can't think of a new feature without designing it with the help of state machines. I don't want to list all the benefits of this approach, it is well documented online. Mine wants to be a warning to the other developers willing to enter the State Machine forest. It is a nice journey, but it will affect your sleep.

Don't do my mistake.