Live data from Hacker News

State Machines in Ruby: An Introduction

blog.appsignal.com

1–10 of 42 posts

Re: State Machines in Ruby: An Introduction

#3
I've seen a few large Rails codebases that included a state machine library like mentioned in the article. Every single one was worse because of it. It pushes the code down a path where hidden hooks run when magic methods are called. At this point in my career I'm just done with that type of "clever" code.

The article starts with examples that just define the state machine by hand. This is a much better approach and scales to larger code much better. You can grep "def start!" and just read what the method does. A state machine DSL is really not providing much value and eventually just gets in the way.

Re: State Machines in Ruby: An Introduction

#4
Counterpoint: State machines in Ruby should be used sparingly, and only when necessary, because they start out quick and easy, but almost always grow into things that are big, ugly, and difficult to refactor or remove.

Here are somebody's notes from a Railsconf talk that made this point, plus a link to the video:

https://gist.github.com/benoittgt/05521e272e13c7b7c9c89eb4a9...

But the short version is the takeaway quote:

> State machines are magnetic : they attract more states, transitions, events, callbacks and they accumulate callback logic where our bugs hide

Re: State Machines in Ruby: An Introduction

#7
post #3

I've seen a few large Rails codebases that included a state machine library like mentioned in the article. Every single one was worse because of it. It pushes the code down a path where hidden hooks run when magic methods are called. At this point in my career I'm just done with that type of "clever" code. The article starts with examples that just define the state machine by hand. This is a much better approach and…

Funny, I have spent a ton of time around Rails code where I dearly wished we had a state machine, because the alternative was an unorganized cluster of home-grown "state transition" glue without any consistent way of handling it, with all the weird-ass edge cases and split-brain BS that come with it.

Re: State Machines in Ruby: An Introduction

#8
I worked at a business (in ruby) where the entire core logic of the app was a state machine, as an idea the thought was good, a state machine modeled order flows pretty well, but in practice it was very slow and the amount of side effects that had to be considered as the business grew just compounded this.

It got to the point where this state machine approach was a real bottleneck and though I left before it was resolved last I heard they had to put real engineering time in to move off of it.

Be very careful when deciding to go this route.

Post reply on HN