liveBook · Manning

Decomposition

As explained in Functional Programming in JavaScript

Decomposition is the process of dividing a complex whole into smaller constituent parts that can be handled or analyzed separately.

Decomposition divides a complex task into smaller, manageable parts. In functional programming, those parts are functions: each handles one logical subtask, and their results can be joined into a complete solution. Splitting work this way makes the pieces easier to understand independently while preserving the larger goal. section 1.3

Start by identifying the jobs hidden inside one large operation. Keep splitting a job until its functions are simple and independent. A useful guide is the singularity principle: give each function one purpose. Pure functions and referential transparency support this design because connected functions need compatible inputs and outputs. section 1.3

Figure 1.3. The process of decomposing breaks showStudent into smaller parts. These subtasks are independent and simpler to understand, so that when combined, they help solve the bigger picture.

The diagram shows showStudent broken into independent subtasks that can be understood separately and then combined. section 1.3

Split display work into a pipeline

A student lookup can be expressed as separate steps: find a student, convert the result to CSV, and append it to the page. Composition connects those steps so that each function's output becomes the next function's input. This replaces one tangled operation with named units whose data flow is explicit. section 1.3

Decomposition also helps when a problem contains smaller versions of itself. Recursion solves that shape of problem by separating a base case, which returns a concrete answer, from a recursive case, which calls itself with smaller input. The smaller inputs must eventually reach the base case or the program will not terminate. section 3.5

Add an array recursively

To sum an array, return 0 for an empty array. Otherwise, add the first value to the sum of the remaining values. Each call reduces the array, and the results combine as calls return. The same pattern supports traversal of nested objects, XML or HTML, graphs, and trees when each level presents the same task. section 3.5

Small functions are not only an organizational choice. They can make code extensible and can enable memoization, which avoids repeating expensive evaluations. Decomposition therefore connects program structure to optimization, especially when recursive work is expressed in smaller self-similar parts. section 7.5

How Functional Programming in JavaScript covers it

How deep it goes
Explains function-level splitting and self-similar recursive reduction, including base and recursive cases rather than formal complexity analysis.
Examples
Uses showStudent as a composed pipeline and array summation as a recursive reduction, alongside tree and document traversal applications.
What it assumes
Introduces decomposition in plain functional terms while noting that recursive thinking can be challenging.
What you can do after
Apply it by splitting JavaScript operations into focused functions or smaller self-similar recursive problems.
How it connects
Connects decomposition with function composition, purity, referential transparency, recursion, memoization, and tail recursion.

Also explained in

Compare how 10 books explain decomposition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.