State Machines and Agile Software Development

Thursday 02 October 2003 at 21:02

State Machines part three

I've recently finished reading Agile Software Development cover to cover. I learned a ton. I can't recommend this book strongly enough. This goes on my list of must-haves along side Design Patterns , and Refactoring , and Effective Java .

Bob Martin Introduces the state pattern on page 419:

Finite state automata are among the most useful abstractions in the software arsenal. They provide a simple and elegant way to explore and define the behavior of a complex system. They also provide a powerful implementation strategy that is easy to understand and easy to modify. I use them at all levels of a system, from controlling the high-level GUI to the lowest-level communication protocols. They are almost universally applicable.

That dispelled any doubts I had about all those workflows . And it gave me insight about what made that Tango outline effective. Despite some tendency toward self-doubt, I trust my intuition when it gets insistent. But confirmation by external authorities gives me a warm, fuzzy feeling inside. :-)

Just as I was settling a little too deeply into my self-satisfaction, Bob mentioned his state machine compiler, available for free at Object Mentor . It humbled me. Create a state transition table and let the compiler pop out a bunch of state machine gears and cogs. What an excellent example of meta-programming (about which I'll have more to say). Moreover, a state transition table is an excellent way to think about the state pattern. The compiler allows me to work with the state machine at a conceptual level without having to get lost in the state pattern minutia. This is the kind of trick I just know is going to change the way I approach problems. This is leverage.

By the way, Larry, who recommended Agile Software Development to me, has just commented about using state charts to design user interfaces .

Update: Thought I should include an example of the SMC config file. This is the state transition table for a turnstyle from the SMC distribution. I think you can see at a glance how it spares you the state machine minutia.

Context Turnstylecontext     // The Name Of The Context Class
Fsmname Turnstyle            // The Name Of The Fsm To Create
Initial Locked               // The Name Of The Initial State
                             // For C++ Output
Pragma Header  Tscontext.H   // The Header File Name For The Context Class
{
    Locked
    {
        Coin     Unlocked    Unlock
        Pass     Locked      Alarm
    }
    Unlocked
    {
	    Coin    Unlocked    Thankyou
	    Pass    Locked      Lock
    }
}