Functional Programming, for an Architect
Pure functions, immutability, and higher-order functions — why they matter even in a codebase that is mostly object-oriented.
Functional Programming, for an Architect
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A pure function's output depends only on its inputs, and calling it changes nothing outside itself — no mutated argument, no write to a database, no console output — which makes it trivially safe to test, cache, and run in parallel.
- Immutability means a value, once created, is never changed in place; an update produces a new value instead, which eliminates an entire category of bugs where one part of a program mutates data that another part is still relying on.
- A higher-order function either takes a function as an argument (like `map` or `filter`) or returns one, and this is what lets behavior itself be passed around and composed like any other value.
- Function composition — building a complex operation by chaining small, single-purpose functions — is functional programming's answer to the same problem object composition solves for objects: build complexity out of simple, independently testable pieces.
- You do not need a purely functional language to get most of this benefit; writing pure, immutable-by-default code inside an object-oriented or multi-style language is a choice, and most mainstream languages now support it well enough to make it a realistic default.
A different axis than object-oriented vs. not
It’s tempting to treat “functional” and “object-oriented” as two competing camps you have to pick a side of, but for a working architect the more useful framing is that functional programming is a discipline you can layer on top of almost any codebase, object-oriented or not. The core commitments — functions with no side effects, data that doesn’t change after it’s created, and treating functions themselves as values you can pass around — are things you can adopt selectively, inside one module or one service, without rewriting anything else. Russ Olsen’s talk makes this point directly: functional programming spent decades as an academic curiosity and has become mainstream practice precisely because it turned out to be a set of habits, not a religion requiring a particular language.
Pure functions: the unit of trust
A function is pure when its return value depends only on its arguments, and calling it has no observable effect on the rest of the world — no mutated argument, no file write, no network call, no shared counter incremented. tax(amount, rate) => amount * rate is pure. A method that reads a global taxRate variable, or that mutates the order object it was handed, is not. The architectural payoff of purity is trust: a pure function can be tested with nothing but inputs and expected outputs, can be safely called from multiple threads at once with no risk of a race condition (there’s no shared state to race over), and can be memoized — its result cached against its inputs — because the same inputs are guaranteed to always produce the same output.
Immutability: no more surprise mutations
Immutable data means that once a value is constructed, it is never modified in place; any “change” produces a new value and leaves the original untouched. Instead of list.add(item) mutating list, an immutable approach returns a new list that is the old one plus the item, leaving the original list exactly as it was. This sounds wasteful until you consider the bug class it eliminates: aliasing bugs, where two parts of a program hold references to what they both believe is “their” object, and one part’s mutation silently corrupts the other’s view of the world. These bugs are notoriously hard to track down because the mutation and its symptom can be far apart in the code and in time. Immutable data makes that entire failure mode structurally impossible — a reference you hold is guaranteed to still describe the exact value it described when you first got it.
Higher-order functions and composition
A higher-order function either accepts a function as one of its arguments or returns a function as its result. map(list, fn), filter(list, predicate), and reduce(list, combiner, initial) are the canonical examples: instead of writing a loop that mutates an accumulator, you pass the behavior you want — the mapping function, the predicate, the combining function — into a generic traversal that handles the looping for you. This is powerful because it lets you build complex transformations by composing small, independently-testable functions: data.filter(isActive).map(toSummary).reduce(sumTotals) reads as a pipeline of named, single-purpose steps rather than one tangled loop doing all three jobs at once. Composition is functional programming’s version of the same goal object-oriented composition pursues (see object-oriented-programming-principles): build something complex out of small pieces, each of which does one thing and can be reasoned about alone.
Where an architect actually uses this
You rarely get to mandate a pure-functional rewrite of an existing system, and you usually shouldn’t want to — most production codebases benefit from a pragmatic mix. Where this pays off concretely: data-transformation and business-rule code (the kind of logic that computes a price, a score, or a report) is an excellent candidate for a pure, immutable style, because it’s exactly the code you want to unit-test exhaustively and run in parallel without fear. State that genuinely needs to change over time — a database connection, a user session, an in-memory cache — still needs an imperative or object-oriented home; trying to force that into pure functional style usually just moves the mutation somewhere more awkward rather than removing it. The architectural judgment call is recognizing which parts of a system are “compute a value from inputs” (functional-shaped) and which are “manage change over time” (object/state-shaped), and giving each the style that fits — a distinction worth keeping in mind heading into reactive-programming next, which is largely an attempt to model the second kind of problem using functional-style tools.
Check yourself
Why are pure functions easier to test than functions with side effects?
A pure function's behavior is completely determined by its arguments, so a test just needs to call it with some inputs and check the output — there's no database to set up, no mock to configure, and no ordering-dependent state to reset between tests.
What specific class of bug does immutability eliminate?
Bugs caused by "aliasing" — one part of a program holding a reference to the same mutable object another part is modifying, so a change made in one place silently and unexpectedly shows up somewhere else; if values can't be mutated in place, every reference to a value is guaranteed to still reflect what it was when you first read it.
Do you have to switch to a language like Haskell or Clojure to benefit from these ideas?
No — pure functions, immutable data structures, and higher-order functions like map and filter are all available as a style choice in mainstream languages that support multiple styles, such as JavaScript, Python, Java, and C#; adopting the discipline is a decision you make, not a language you're forced into.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.