Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What is a good way to define "state", as in state variable or state machine, to a new (previously non) programmer? What are some good ways to explain why this concept is useful for writing software? Is the concept of state explicitly taught in beginning programming courses or books commonly?

share|improve this question

4 Answers 4

up vote 4 down vote accepted

From Wikipedia:

State commonly refers to either the present condition of a system or entity...

That's pretty much what it means in a computing context: the data that defines the condition of some object or system.

The meaning of 'state' isn't specific to programming. There are examples of 'state' literally everywhere you look. The television is off. The coffee is hot. The sneakers are a) blue; b) smelly; c) not put away; d) quite worn out. 'state' in a programming sense is just the same as 'state' in a non-programming sense, except we apply it to the objects that we deal with: variables, objects, threads, databases, file systems, etc. If you have to explain it, explain it in terms that are familiar -- there's no need to make it into a foreign, technical concept.

A clock is a good example of a simple system that has some state: a clock can be running or not running, and it indicates a time which may or may not correspond to the actual time of day. It's pretty easy to see how those things are like variables and how changing the state of the clock might impact other things.

Compare that to something stateless, like a statement: "The door is red." The statement itself doesn't have state. It may or may not be true that the door is red, but the statement itself doesn't contain any state. Painting the door can change the truth of the statement, but the statement itself never changes. Similarly, a number like 42 doesn't have any state: 42 is 42 and there's nothing you can change about it.

Variables have state, values don't.

share|improve this answer
    
And how is the wikipedia or dictionary definition useful to a beginning programmer? –  hotpaw2 May 25 '12 at 2:40
    
@hotpaw2 The meaning of 'state' isn't specific to programming. There are examples of 'state' literally everywhere you look. The television is off. The coffee is hot. The sneakers are a) blue; b) smelly; c) not put away; d) quite worn out. 'state' in a programming sense is just the same as 'state' in a non-programming sense, except we apply it to the objects that we deal with: variables, objects, threads, databases, file systems, etc. –  Caleb May 25 '12 at 2:49
    
So you would explain it to a programming student by using concrete examples? Seems like a good idea. Will it transfer effectively to them understanding state in their programs? –  hotpaw2 May 25 '12 at 2:54
2  
@hotpaw2 Moved my comment up to the answer and expanded a bit. As for understanding, I think it's best to not focus too much on 'state' in the abstract with a beginner. Most people need concrete examples of something before they can really recognize a more general pattern. –  Caleb May 25 '12 at 3:27

"state" is needed, when you need to remember something. Functions (not "methods" or whatever) ideally only depend on their inputs. If you ask a question where the answer can change over time, even if the inputs are the same, you need something to remember that difference in the answer, correct? Otherwise, you are unable to give a different answer because no other factors have changed (let's assume the answer does not depend on time directly).

That "thing/information" that you need to remember is called "state".

(it is not requiered that this "state" changes over time, it's basically just a way to say "remembering/knowing something")

share|improve this answer
    
I think this is the best answer, if rephrased to be intelligible to a nonprogrammer. Say: all activities handle things and/or information. This happens in three ways: 1) any activity may take or use or read something: its input; 2) it may produce or write something: its output; 3) while in progress it may hold, keep, memorize, store something: its state. For most computer programs, all input, output and state consists of information, while for other types of activities (say, cooking) physical objects are usually involved as well. –  reinierpost May 25 '12 at 13:02

You can use the analogy of a traffic light which can be in one of three states: [GREEN], [YELLOW] or [RED]. It must only be in one state at a time. The traffic light is the actual state machine being modelled. It will normally transition its state from [GREEN to YELLOW], then [YELLOW to RED], then [RED to GREEN].

enter image description here

An alternative state transition (which is in effect in some countries like UK, has two transitions of [RED to YELLOW, then YELLOW to GREEN] in place of the [RED to GREEN] transition.

share|improve this answer

State is information your program manipulates to accomplish some task. It is data or information that gets changed or manipulated throughout the runtime of a program. The "state" of a program at a given time refers to a snapshot of all the data the program is currently looking at or analyzing to get to the next step in it's execution.

My university started with Scheme, so we started programming with a state-less model. We then switched to C and were very explicit about what state is, why it's useful, and why it makes things potentially very complicated. So some universities certainly talk about state, but not necessarily all of them

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.