liveBook · Manning

Imperative programming

As explained in Functional Design and Architecture: Examples in Haskell

A programming paradigm in which programs specify ordered operations that change data or produce side effects.

Imperative programming organizes software as ordered instructions that change data or produce side effects. It emphasizes how execution flows toward a result rather than only describing the result. section 1.4.1

Common building blocks and design practices include:

  • Loops for repeated actions, procedures for named instruction sequences, eager computations, mutable data structures, and pointers. section 1.4.1
  • Modularity separates behavioral responsibilities, while opaque data types hide unstable implementations behind stable public interfaces. section 1.4.1
  • Destructive updates, side effects, and risky type conversions can target low-level characteristics such as cache efficiency or reduced slow-memory access. section 1.4.1

Temperature monitoring scenario

A thermometer is read once per second. Conditional branches register normal readings, log measurements, and raise alarms when temperatures cross bounds; registration converts Celsius to Kelvin and stores the timestamp, value, and device. The same requirement can be reshaped as streams: one stream yields readings, while a monitor stores, validates, logs, alarms, and yields results. section 3.1.4 section 3.1.4

Listing 3.3 Temperature sensor readings scenario
Scenario: readThermometer                               #1
Description: monitor outside thermometer temperature
Given: outside thermometer @therm
Invocation: once a second
  
  Read @therm,                                          #2
    Result: @reading(@time, @temp, @therm)
  
  If @temp < -10C Then                                  #3
    register @reading                                   #4
    Log @reading                                        #5
    Raise alarm "Temperature is too low"                #6
 
  Else If @temp > 50C Then                              #7
    register @reading 
    Log @reading 
    Raise alarm "Temperature is too high"
  
  Else register @reading                                #8
  
  Return @reading                                       #9

Read the annotated scenario to trace invocation, branching, registration, logging, alarms, and return flow. section 3.1.4

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains control flow, mutation, side effects, and modular design, but stops short of a comprehensive catalog of imperative patterns.
Examples
Uses a temperature-monitoring scenario with sequential reads, conditional branches, database storage, logging, alarms, and stream-based reformulation.
What you can do after
Evaluate how a requirement maps to sequential instructions, mutable operations, side effects, and alternative stream-based structures.
How it connects
Connects imperative programming with object-oriented programming, functional programming, modularity, polymorphism, opaque pointers, and side effects.

Also explained in

Compare how 14 books explain imperative programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.