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 describes how a program proceeds: it gives step-by-step instructions that change data or produce side effects. The focus is on the sequence of operations rather than only the final result. section 1.4.1

Common building blocks and design concerns include:

  • Loops, mutable data structures, pointers, procedures, and eager computations. section 1.4.1
  • Modularity and opaque data types, which separate a stable public interface from a private implementation. section 1.4.1

Imperative code remains common in system software and is also present under the hood of object-oriented programming. Managing state and side effects can make parallel and distributed programming difficult. section 1.4 section 1.5

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

This thermometer scenario reads a value, branches on temperature bounds, and registers readings, logs problems, or raises alarms. section 3.1.4

A stream-based alternative represents thermometer readings as a stream and lets a monitor react to each value, rather than organizing the whole scenario as one sequence of instructions. section 3.1.4

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines stepwise control and surveys loops, mutable data, pointers, procedures, eager computation, modularity, and opaque data types.
Examples
Uses a thermometer scenario with conditional alarms and database writes, then contrasts it with stream-based reasoning.
What you can do after
Evaluate whether sensor logic follows an imperative sequence and compare it with a stream-based alternative.
How it connects
Connects imperative programming to procedural and structured programming, object-oriented programming, modularity, opaque data types, and functional programming.

Also explained in

Compare how 15 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.