liveBook · Manning

Composition

As explained in The Art of Code

Function composition is the construction of a function by chaining functions so that one function's output becomes the next function's input.

Function composition builds a larger transformation by chaining smaller functions: one function’s output becomes the next function’s input. Each function can focus on one responsibility, then be reused independently or combined into a readable sequence. section 6.2.2

Split and recombine sanitization

A single sanitizeField function that normalizes whitespace and removes special characters is difficult to reuse partially. Separate normalizeSpaces and removeSpecialCharacters, then compose them as sanitizeField = normalizeSpaces.andThen(removeSpecialCharacters). The result preserves the original behavior while making each transformation independently usable. section 6.2.2

Use composition as a practical design choice:

  • Pass, store, and return functions when behavior needs to be assembled dynamically. section 6.2.1
  • Prefer small transformations that read as a top-down story instead of one large block with multiple responsibilities. section 6.2.2 section 6.0
  • Keep imperative code when side effects, heavy mutation, or clearer performance characteristics make composition a poor fit. section 6.4
  • Check the language’s support: Kotlin and C# commonly use nested calls or extensions, while Haskell provides the (.) operator natively. section 6.2.2
Function<String, String> sanitizeField =
 normalizeSpaces.andThen(removeSpecialCharacters);

A compact listing shows two focused string transformations combined into a reusable composed function. section 6.2.2

How The Art of Code covers it

How deep it goes
Explains single-responsibility decomposition, chaining, language support, and tradeoffs, but stops short of formal composition theory.
Examples
Uses string sanitization and book-title extraction, sorting, and collection to illustrate composed transformations.
What it assumes
Assumes familiarity with functions and introduces their treatment as first-class values that can be passed, stored, and returned.
What you can do after
Build it from scratch by splitting multi-step behavior into reusable functions and composing them into readable transformations.
How it connects
Connects composition with functional programming, first-class functions, pure functions, immutability, and data pipelines.

Also explained in

Compare how 11 books explain composition →

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.