⚠️State Machine Workshop Imminent and Boolean Complexity
Hey folks,
This the last email I send you about my online State Machine workshop on Wednesday. If you're interested in attending, you can get your ticket here.
However, it is not the last time I'll write about state machines. The more research I do to get prepared for the workshop, the more ways I realize how state machines are the best solution to a variety of common problems. One of those problems is the complexity of managing multiple booleans in the same scope.
A single boolean, let's call it alpha
, is not very difficult to manage. It's either true
or false
. Two states. alpha
and !alpha
.
Add a second boolean, beta
. Now you have four states. alpha && beta
, alpha && !beta
, !alpha && beta
, and finally !alpha && !beta
. That's twice the complexity of the first scenario.
Add a third boolean, gamma
. Now you eight states! alpha && beta && gamma
, alpha && !beta && gamma
, alpha && beta && !gamma
, alpha && !beta && !gamma
, !alpha && beta && gamma
, !alpha && !beta && gamma
, !alpha && beta && !gamma
, and finally !alpha && !beta && !gamma
.
Add a fourth boolean... I kid. I won't do that. But for the record, it's sixteen states. Boolean complexity increases at a rate of 2^n
where n
is the number of booleans. It quickly becomes unmanageable. Imagine if we had 10 booleans, that would be 1024 possible states.
But are all of those states actually possible?
Replace alpha
, beta
, gamma
with names like isLoading
, hasError
, hasMetadata
, etc. We have been taught to write programs with variables like this as if somehow they make the complexity better. Sure they make it easier to read (and I encourage variable names you can read), but they don't reduce the complexity of our programs.
What if we wrote our programs with only the possible states that it could be in? What if it was impossible for our programs to enter impossible states?
We can. With state machines. ...And with enums and switches which is messier and more error prone but can be done. 😅 Trust me, state machines are better.
I hope you'll consider joining me in the workshop as I teach you how to do this. If you can't make it, no pressure. I'll be putting out more material soon that covers this as well. For now, I want you to think about how you might be able to reduce boolean complexity in your applications. Our apps and the brains of your fellow coders will thank you for the effort.
Thanks for letting me share some words with you. Hope your week gets off to a great start.
-Kyle