Notes on programming state

A computer program stores data in variables, which represent storage locations in the computer’s memory. The contents of these memory locations, at any given point in the program’s execution, is called the program’s state.

Imperative programming is a programming paradigm (way of designing a programming language) that describes computation in terms of the program state and statements that change the program state. In contrast, in declarative programming languages the program describes the desired results, and doesn’t specify changes to the state directly.

A more specialized definition of state is used in some computer programs that operate serially (sequentially) on streams of data, such as parsers, firewalls, communication protocols and encryption programs. Serial programs operate on the incoming data characters or packets sequentially, one at a time. In some of these programs, information about previous data characters or packets received is stored in variables and used to affect the processing of the current character or packet. This is called a “stateful protocol” and the data carried over from the previous processing cycle is called the “state”. In others, the program has no information about the previous data stream and starts “fresh” with each data input; this is called a “stateless protocol”.

Stateless protocol

In computing, a stateless protocol is a communications protocol that treats each request as an independent transaction that is unrelated to any previous request so that the communication consists of independent pairs of request and response. A stateless protocol does not require the server to retain session information or status about each communications partner for the duration of multiple requests. In contrast, a protocol that requires keeping of the internal state on the server is known as a stateful protocol.

Examples of stateless protocols include the Internet Protocol (IP), which is the foundation for the Internet, and the Hypertext Transfer Protocol (HTTP), which is the foundation of data communication for the World Wide Web.

The stateless design simplifies the server design because there is no need to dynamically allocate storage to deal with conversations in progress. If a client session dies in mid-transaction, no part of the system needs to be responsible for cleaning up the present state of the server. A disadvantage of statelessness is that it may be necessary to include additional information in every request, and this extra information will need to be interpreted by the server.

Examples

An example of a stateless protocol is HTTP,[1] meaning that each request message can be understood in isolation.

Contrast this with a traditional FTP server that conducts an interactive session with the user. During the session, a user is provided a means to be authenticated and set various variables (working directory, transfer mode), all stored on the server as part of the user’s state.
Stacking of stateless and stateful protocol layersEdit

There can be complex interactions between stateful and stateless protocols among different protocol layers. For example, HTTP is an example of a stateless protocol layered on top of TCP, a stateful protocol, which is layered on top of IP, another stateless protocol, which is routed on a network that employs BGP, another stateful protocol, to direct the IP packets riding on the network.

This stacking of layers continues even above HTTP. As a work-around for the lack of a session layer in HTTP, HTTP servers implement various session management methods,[2] typically utilizing a unique identifier in a cookie or parameter that allows the server to track requests originating from the same client, and effectively creating a stateful protocol on top of HTTP.

“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”)

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.

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.