DEV Community


Discussion on: Explain a State Machine Like I'm Five

louy2 profile image
Yufan Lou

Are you hungry? When you are hungry, what do you do? You eat some bread, maybe drink some milk, right? Then are you full? Yes, you are? Good. But you don't feel hungry and full at the same time, right? Yeah. The feelings you have, you feel hungry or full, but not both at the same time, we can call them states. S-t-a-t-e, states. The things you do to change your feelings, like eating or drinking, we can call them transitions. But if you don't eat, you will become hungry again, right? But what are you doing when you are not eating? Does doing nothing also change how you feel? Yes? No, because you are not really doing nothing. Your stomach absorbs the food you eat. So only doing something changes how you feel, right? Doing nothing doesn't change how you feel. We call this only transitions can change the state. See, you are a little state machine! Yes, yes, you are not a machine, you are a child, not a robot. But we want to make robots which work like human, right? So they can help you do the housework, right? We describe how human do housework with this tool called state machine, just like you draw a house with the crayons. This machine can't work yet, like you can't live in your house on the paper. You can? Can you invite me into the house then? No? That's a pity, but don't worry. There are people who can look at your house, and build one on the ground that we can visit together. They are called engineers. There are also people who can look at the state machine, and build a robot that helps you do some housework! They are also called engineers. Do you want to become an engineer? No? What do you want to do? You like cooking? ...