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














