[Tutor] What is a "state variable"?

Alan Gauld alan.gauld at btinternet.com
Sat Nov 13 02:07:24 CET 2010


"Richard D. Moores" <rdmoores at gmail.com> wrote

>> For example if I say that the state of any object can be 
>> represented
>> by a vector whose values correspond to the collected set of 
>> variables
>> of the object, does that make sense?
>
> No. Not the vector idea. Sorry.

A vector is just a fancy mathematical name for an array of values.
In the case of state the values are those of the set of relevant
variables that control the behaviour of the program.

In the case of an object it is usually the set of attributes of the
object plus any local/environment variables. (An object can be
thought of as a container in the same way as a list or dictionary)

So the state variables of a class are those that affect the
functioning of the class.

As an example we might have a simple traffic light that cycles
through the 3 states (green, amber red) at a set time interval.
The only state variable is likely to be one holding the current
state name. But we could make it more complex by having
the traffic light interval controlled by traffic density based on
a count of the number of cars passing during a green phase.
Now we have two factors controlling the state change - the
old value and the counter - two state variables. We then might
introduce different intervals per state change - so we get a
set of 4 timer values - now 6 state variables. And so on.

We might also have some other variables that are purely data
such as the location of the light, or the date of installation.
But these may have no effect on the function of the light.
These are attributes but they are not state variables - they
don't affect the state. Of course, in practice they may affect
the state of other operations, like a scheduled cleaning
operation - in which case they become state variables but
in a different state vector since the cleaning state machine
is separate from the lighting state machine.

State machines are very important in software engineering,
particularly for safety critical systems as they are one of the
few specification and design techniques in CS that are both
mathematically rigorous, have provably correct implementations
and are practical to build.(usually by building a dispatch table
of states and their events)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list