liveBook · Manning

Imperative programming

As explained in BDD in Action, Second Edition

Imperative programming style expresses computation as ordered commands that change program state.

Imperative programming expresses computation as a sequence of instructions that describe how to perform a task. An average-age calculation can add each age to a running total, then divide that total by the list size. This contrasts with declarative programming, which states the desired result—such as the average—without specifying each operation. section 7.6.3

val ages = listOf(20,40,50,15,80);
var totalAges = 0
for (age in ages) {
    totalAges = totalAges + age
}
val averageAge = totalAges / ages.size

A loop, mutable total, and final division make the calculation procedure explicit. section 7.6.3

The same pattern appears in imperative-style Gherkin: scenarios list low-level interactions such as entering fields, setting options, and clicking buttons. Such detail can obscure business intent, couple scenarios to the interface, and increase maintenance; declarative scenarios instead describe the requested outcome. section 7.6.3

Finding a hotel

An imperative scenario says to enter Paris, choose dates and distance, and press search. A more declarative version asks for a hotel within 10 km of Paris for two nights on the specified date. section 7.6.3

How BDD in Action, Second Edition covers it

How deep it goes
Demonstrates the style through an average calculation and Gherkin interactions, then contrasts it with declarative expression.
Examples
Uses a running-total average calculation and a hotel-search scenario built from field entries and button clicks.
What you can do after
Evaluate/understand when procedural detail clarifies implementation and when it obscures business intent or increases maintenance.
How it connects
Links imperative programming with declarative programming, functional programming, and executable specifications.

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 BDD in Action, Second Edition. The section links point to the parts of the book it draws on.