liveBook · Manning

Imperative programming

As explained in The Art of Code

Imperative style structures computation as explicit commands and control-flow statements that change or inspect state.

Imperative programming spells out how a result is produced: create or inspect state, execute commands in order, and use loops and conditionals to control what happens next. Procedures and methods can also change state outside their local scope, such as by writing a file or updating a database. section 6.1.1

List<String> result = new ArrayList<>();
for (String word : words) {
    if (word.length() > 3) {
        result.add(word.toUpperCase());
    }
}

This Java loop creates a result list, checks each word, and adds an uppercase version when the word is long enough. The sequence of instructions is explicit; the overall transformation takes a little more work to spot. section 6.1.1

That explicit control is useful when an algorithm depends on heavily mutated state or the job necessarily has side effects. It has a cost: mutations can introduce subtle bugs, and an effect such as changing a shared object may not be visible in a method’s signature. Hidden effects make behavior harder to test and trust. section 6.1.1 section 6.4

One pass or a clearer pipeline?

To find the youngest and oldest people and produce sorted names, an imperative loop updates both age comparisons and collects names in one pass, then sorts them. A functional version expresses each result separately with streams. The loop uses fewer traversals; the separate pipelines have lower cognitive complexity in this example. Whether that difference matters depends on the data size and the clarity of the finished code. section 10.2.2

Listing 10.1 Imperative coding
Person youngest = null;  
Person oldest = null;
Integer minAge = null;
Integer maxAge = null;
List<String> names = new ArrayList<>();

for (Person p : persons) {
  int age = p.age();

  if (minAge == null || age < minAge) {    #1
    minAge = age;                          #1
    youngest = p;                          #1
  }                                        #1

  if (maxAge == null || age > maxAge) {    #2
    maxAge = age;                          #2
    oldest = p;                            #2
  }                                        #2

  names.add(p.name());        #3
}


Collections.sort(names);     #3

log.info("Youngest: {}, oldest: {}, count: {}, sorted names: {}", 
  youngest, oldest, persons.size(), names);

The imperative version keeps the minimum, maximum, and collected names together in one loop. section 10.2.2

Listing 10.2 Functional programming
Optional<Person> youngest = persons.stream()     #1
  .min(Comparator.comparingInt(Person::age));   #1

Optional<Person> oldest = persons.stream()       #2
  .max(Comparator.comparingInt(Person::age));    #2

List<String> sortedNames = persons.stream()      #3
  .map(Person::name).sorted().toList();          #3

log.info("Youngest: {}, oldest: {}, sorted names {}",
  youngest.orElse(null), oldest.orElse(null), sortedNames);

Compare the separate stream operations with the loop to judge which version makes the intended work easier to follow. section 10.2.2

Choose the style that keeps the work clear. A well-crafted pipeline can state a transformation with less control-flow noise, but forcing side effects or awkward exception handling into one can make it worse. Prefer an imperative loop when its state changes are central to the task; do not optimize for a small performance difference without a specific problem. section 6.4 section 6.5

How The Art of Code covers it

How deep it goes
Defines commands, control flow, mutation, and side effects, then compares practical implementations without descending into language runtime internals.
Examples
Uses a Java word-filtering loop and competing implementations for finding age extremes and sorting names.
What it assumes
Assumes familiarity with lists, loops, and conditionals; illustrates their use rather than teaching those constructs from zero.
What you can do after
Practice/perform it when explicit state changes suit the task, while weighing clarity and measured performance against a functional pipeline.
How it connects
Contrasts imperative programming with functional programming, pure functions, declarative pipelines, and side effects.

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