Hacker News new | ask | show | jobs
by jonasvp 3034 days ago
I distinctly remember the first time I looked at a overly long and complicated model class and thought: "OMG, this is a state machine!" - a shortish refactor later and it was much simpler to read and debug.

However, now I'm at a loss on how to teach the junior programmers at my company how to recognize which patterns scream "turn me into a state machine!" Several booleans triggering certain code paths in several methods is a pretty sure sign. Are there more?

2 comments

The word "protocol" is a big red flag that you should be using a state machine.

The words "event loop" probably mean you need a state machine.

If "time" is an input variable, you probably have a state machine.

I've also found the presence of a mode variable a good indicator that a FSM should at least be considered.
Your second and third points are why I love having state machines in the games I develop.
If you have a "status" field in your object, chances are you can represent it as a state machine