▲ | grumpyprole 7 months ago | |||||||||||||||||||||||||||||||
A state machine makes the actual program state first class and easy to reason about. One does not even need mutable state to model one. Whereas you appear to be advocating mutable objects. The state space then becomes a combinatorial explosion of all the hidden mutable state "encapsulated" inside the objects. Object oriented programming is not the only way and often leads to a poor domain model. Some OOP evangelists even model a bank account with a mutable balance field and methods for making deposits. This is a absolutely not a faithful model of the domain (ledgers have been used for hundreds/thousands of years). In summary, yes a state machine can absolutely be a good domain model. | ||||||||||||||||||||||||||||||||
▲ | bvrmn 7 months ago | parent | next [-] | |||||||||||||||||||||||||||||||
It's interesting to know about what state machines you talk. From my experience most of the time it's an entity with state property with finger countable cardinality. And state is assumed to be changed directly. And it's not easy to reason because author only heard about state machines and state transitions are spread over all code base. | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
▲ | majormajor 7 months ago | parent | prev [-] | |||||||||||||||||||||||||||||||
I don't follow the connection you're making. State machines often are implemented with mutable objects. And one does not need mutable objects to make "modules-> objects with the transitions being method calls". Every method call could return a fresh, immutable object, nothing requires mutation there. I'd see a method like: `TransitionTo(newState)` as a major smell compared to an explicit `TransistionToNewState` and I think OOO can be helpful (hardly required, of course) in that one neat way of streamlining usage of your code is that if you're implementing objects then the object for something in "State A" might not even have "TransitionToStateC" if that's not a valid operation. (No, you don't HAVE to write state machine code that allows you to ask for invalid operations, but it's a common pattern I've seen in real code and in online discussion/blogs/stack overflow.) | ||||||||||||||||||||||||||||||||
|