liveBook · Manning

Declarative programming

As explained in BDD in Action, Second Edition

Declarative programming style expresses desired results or relationships without specifying every execution step.

Declarative programming states the result or relationship you want and leaves the execution method implicit. It emphasizes what to achieve rather than how to achieve it, contrasting with imperative programming’s step-by-step instructions. section 7.6.3

Calculating an average

An imperative implementation initializes a total, loops through ages, adds each value, and divides by the list size. A declarative implementation states the operation directly with ages.average(). section 7.6.3

Writing executable scenarios

A low-level scenario lists field entries, date selection, distance selection, and button clicks. A declarative scenario describes the business outcome: looking for a hotel within a distance of a city for specified dates and nights. section 7.6.3

val ages = listOf(20,40,50,15,80);
val averageAge = ages.average();

View the concise average calculation that expresses the desired operation without exposing its iteration steps. section 7.6.3

When I look for a hotel within 10 km of Paris for 2 nights on 04-04-2019

View a scenario phrased as a user goal rather than a sequence of interface interactions. section 7.6.3

How BDD in Action, Second Edition covers it

How deep it goes
Defines the outcome-versus-procedure distinction and illustrates it with code and scenarios, without explaining language implementation internals.
Examples
Uses average-age calculations, restaurant requests, room tidying, and hotel-search scenarios to contrast outcomes with instructions.
What it assumes
Builds from the distinction between imperative commands and declarative outcomes before applying it to programming and scenarios.
What you can do after
Apply it by expressing computations and Gherkin scenarios as desired outcomes instead of detailed interaction sequences.
How it connects
Connects declarative programming with imperative programming, functional programming, Gherkin, and executable specifications.

Also explained in

Compare how 14 books explain declarative 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.