At ALL cost try not to added memory to individual states. Let me give you an example.
switch (state)
{
case STATE1:
do something
case STATE2:
if (somememory == TRUE)
do something true
else (do something false)
}
This a very simplistic view, but in most cases STATE2 should be decomposed into two states. you should try to eliminate the guard at all costs. The advantage of this is not apparent until you try to extend or change the state machine. If every state is essentially orthogonal then extending the state machine is easy. If the some states are dependent on some stored value or as I like to say the state is overloaded to mean something different depending on some condition then extending the state machine can be problematic. Basically, I am suggesting that memoryless state machine are in generally far more maintainable than "extended" state machines.
Each state should mean exactly one thing and one thing only. There are obviously exceptions to this case. It is not uncommon for this exception to manifest as some sort of time interval. For example:
Timer fires -
If (timeout > time expired)
do something...perhaps change state
else
reset timer...no state change
I simply suggest that in the back of your mind that you consider the trade offs and limit the coupling between the qualitative and quantitative aspects of your state machine to increase flexibly and maintainable.
If there is one thing that I have learned since entering industry it is that "things' will change. The best thing that you can for yourself and the development of the project is to plan for this ahead of time and make sure changes are as painless as possible. There might be an upfront cost associated with this, but in the long run, it will pay off.
No comments:
Post a Comment