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










