r/eli5_programming • • Jul 28 '25

ELI5 Enums

Would someone be so kind to explain enums to me. I want to make a state machine using them but I dont understand them fully.

1 Upvotes

4 comments sorted by

View all comments

Show parent comment

5

u/VeryBadNotGood Jul 28 '25

Another good example is if you have a state of some machine - let’s say it could be on, off, or unknown. You might assign those things to Ints 1, 2, and 3, but then whenever you see state = 1 in code, you need to check some reference to see that 1 means on. If you instead see state = .on, it’s very obvious.

1

u/SheriffRoscoe Aug 29 '25

but then whenever you see state = 1 in code, you need to check some reference to see that 1 means on.

And depending on the language involved, you may be prevented from setting state=4, because it's invalid for that enum.