Imperative programming describes computation through commands changing program state. Assignment updates a variable, sequencing orders commands and loops repeat updates until a condition changes. To sum a list, an imperative program can maintain an accumulator and add each next element into it. Correctness can be expressed through a loop invariant: after processing a prefix, the accumulator equals that prefix's sum. State changes can be local or observable as a side effect. The paradigm concerns the description of computation, rather than requiring a particular processor architecture.
Organize computation around handling events emitted by input sources or changing state. A handler may update a model and then refresh derived views. The event source, subscription mechanism and handler logic are separate responsibilities; registering code without subscribing to its producer does not guarantee delivery.
A handler invoked again before a previous invocation has completed, for example through a synchronous notification caused by its own display update. A guarded refresh can suppress unintended recursive commits. Such a flag does not replace the need for a coherent state invariant and an explicit rule deciding which edit is authoritative.
A while loop tests a Boolean condition before each iteration and repeats its body while that condition is true. A loop invariant is established initially and preserved by every iteration; a loop variant decreases in a well-founded order to prove algorithm termination. The invariant together with the false exit guard establishes the postcondition.
A side effect is an observable state change accompanying evaluation beyond the returned value. Writing a file, changing a shared variable or modifying an argument object are examples. A function can return the same numeric answer on repeated calls while producing different observable behaviour by appending to a log each time. Replacing the second call with a cached result then changes the program's behaviour. A pure function avoids such observable effects and returns its result from its inputs alone; languages can still provide explicit mechanisms for sequencing necessary effects.
// Target: computer-science.bigb

Articles by others on the same topic (1)