liveBook · Manning

Functional, stateless philosophy

As explained in Functional Programming in JavaScript

A programming paradigm that builds software by composing and transforming functions, typically emphasizing pure functions and immutable data over mutable state and side effects.

Functional programming treats a program as a flow from input through functions to output. Rather than coordinating work through changing variables and shared objects, it abstracts data operations and control flow into functions, aiming to avoid side effects and reduce state mutation. section 1.2 section 1.0

The practical goal is simpler code under growth: small independent functions are easier to reason about, test, reuse, and combine. A function is most dependable when its result depends only on its inputs instead of external state that may change between calls. section 1.1 section 1.2

Replace a changing counter with an explicit input

An imperative increment function reads and updates an outside counter, so successive calls depend on hidden history. A functional version instead accepts counter and returns counter + 1. The state is visible at the call boundary, and the function leaves no external value changed. section 1.2

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.

Breaking student display into lookup, formatting, and DOM-update steps shows the central move: decompose one mixed task into smaller functions, then combine them. section 1.3

This style works through a few reinforcing habits:

  • Decompose a large task until each function has one clear purpose, ideally as a simple independent unit of work. section 1.3
  • Compose compatible functions so one function’s output becomes the next function’s input, forming a reusable pipeline. section 1.3 section 4.5
  • Process collections with fluent chains such as filter, map, and reduce instead of manually tracking loop variables and totals. section 1.3 section 1.4
  • Separate unavoidable effects, such as database access or DOM updates, from data lookup and formatting where possible. section 1.2

Functional code is declarative: it emphasizes what data transformation should happen rather than the step-by-step mutation used to achieve it. Composition can also be written point-free, creating functions without naming their arguments; that can raise abstraction, but overuse can obscure a program. section 1.4 section 4.5

Turn enrollment records into an average

Instead of a loop that updates totalGrades and totalStudentsFound, a chain filters students enrolled more than once, selects grades, and computes their average. Each operation describes one transformation, while the chain carries the data through the calculation. section 1.3

Statelessness is a design direction, not a claim that applications never interact with storage, events, or the DOM. Functional techniques reduce the complexity those interactions introduce by keeping more of the program in predictable function transformations; reactive streams can handle event-driven input. section 1.0 section 1.3

How Functional Programming in JavaScript covers it

How deep it goes
Defines the style, contrasts imperative rewrites, and introduces decomposition, chains, composition, and reactive events without fully developing each technique.
Examples
Uses Hello World DOM output, counters, student display and SSN validation, enrollment grades, and collection transformations.
What it assumes
Assumes working JavaScript plus object-oriented or structured design knowledge, while starting functional ideas with browser-based examples.
What you can do after
Apply functional techniques in JavaScript by decomposing tasks, composing functions, and chaining data transformations.
How it connects
Connects pure functions, immutability, referential transparency, function composition, higher-order functions, reactive programming, and point-free programming.

Also explained in

Compare how 19 books explain functional, stateless philosophy →

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.