liveBook · Manning

Declarative programming

As explained in The Art of Code

Declarative style expresses the desired result or transformation while minimizing explicit control-flow instructions for how to produce it.

Suppose you need to keep words longer than three characters and convert them to uppercase. A loop makes you spell out iteration, branching, and where to put each result. A declarative pipeline puts the transformation first: filter, map, then collect. section 6.1.2

Make the transformation visible

List<String> result = words.stream() .filter(w -> w.length() > 3) .map(String::toUpperCase) .collect(Collectors.toList()); section 6.1.2

This style works well when behavior can be composed from small functions. Pure functions depend only on their inputs and produce the same output for the same inputs, making each step easier to understand and test without tracking hidden state. section 6.1.2

Do not force every task into a pipeline. Writing files, updating databases, sending network messages, or relying on heavily mutated state may call for imperative code. An API may still make a pipeline clearer, but any side effects should remain explicit. For small, simple operations, imperative code can also avoid pipeline setup overhead. Choose the form that makes the work clear without hiding what it costs or changes. section 6.4

How The Art of Code covers it

How deep it goes
Defines the style through functional composition and a stream example, then weighs side effects, performance, and readability without detailing pipeline internals.
Examples
Filters words longer than three characters, uppercases them, and collects the results with a Java stream.
What you can do after
Apply it via Java streams for readable transformations, reserving imperative control flow for stateful or sequence-sensitive work.
How it connects
Connects functional programming, pure functions, referential transparency, side effects, and imperative programming to the choice of transformation style.

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 The Art of Code. The section links point to the parts of the book it draws on.