Latest / CyberSecurity Summary / Functional JavaScript: Introducing Functional Programming with Underscore.js
Transcript
- 0:00Speaker 1: Imagine for a second that you are opening up the
- 0:03Speaker 1: back of one of those vintage mechanical pocket watches. You know,
- 0:06Speaker 1: you take the casing off and you just see this
- 0:08Speaker 1: incredibly complex web of tiny golden gears.
- 0:11Speaker 2: Oh yeah, those are I mean, as a piece of engineering,
- 0:14Speaker 2: they are an absolute marvel to look.
- 0:16Speaker 1: At it right exactly, But if you turn just one gear,
- 0:19Speaker 1: it instantly turns three others, and then those turn ten more,
- 0:22Speaker 1: which is great for a watch, but as a metaphor
- 0:25Speaker 1: for software architecture, it is an absolute nightmare.
- 0:28Speaker 2: One hundred percent. It's a total disaster.
- 0:30Speaker 1: Because, you know, when you're dealing with traditional object oriented
- 0:33Speaker 1: programming or OOP, mutating state in one place often triggers
- 0:37Speaker 1: this like cascade of hidden changes across your entire application.
- 0:41Speaker 2: Yeah, you change a user's status over in this one module,
- 0:45Speaker 2: and suddenly, completely out of nowhere, a totally unrelated billing
- 0:50Speaker 2: object just breaks over there.
- 0:51Speaker 1: Exactly. You've basically built a web of change, and trying
- 0:55Speaker 1: to keep the entire state of that mechanical machine in
- 0:58Speaker 1: your head, I mean, it just becomes impossible as the
- 1:00Speaker 1: code based scales up, which is.
- 1:01Speaker 2: You know why we spend so much time chasing obscure
- 1:04Speaker 2: state bugs instead of actually building new features. We tend
- 1:08Speaker 2: to think we are so neatly categorizing our systems into
- 1:10Speaker 2: these rigid classes and objects, but those implicit connections between
- 1:15Speaker 2: the objects, they eventually just create total architectural gridlock.
- 1:18Speaker 1: And that gridlock is exactly what we are unpacking in
- 1:21Speaker 1: this deep dive because today we are looking at Michael
- 1:24Speaker 1: Fogus's book Functional JavaScript.
- 1:26Speaker 2: Yeah, it's such a great text, it really.
- 1:27Speaker 1: Is, and Focus uses the underscore dot js library to
- 1:34Speaker 1: basically demonstrate a completely different mental model for handling that
- 1:37Speaker 1: complexity we just talked about. Right, we are moving away
- 1:40Speaker 1: from focusing on nouns, you know, the rigid objects and
- 1:44Speaker 1: classes that hold state, and we're shifting our focus entirely
- 1:47Speaker 1: to verbs, pure isolated.
- 1:49Speaker 2: Function, which is a huge mental shift.
- 1:51Speaker 1: Huge And even if you're writing in modern E six
- 1:54Speaker 1: or typescript today, the architectural philosophies that Focus lays out
- 1:57Speaker 1: about data transformation, I mean, they are going to completely
- 2:01Speaker 1: change how you approach system design.
- 2:03Speaker 2: Well in the environment. Here is crucial to understand too,
- 2:06Speaker 2: because JavaScript is multi paradigm. It doesn't force your hand
- 2:10Speaker 2: one way or the other.
- 2:11Speaker 1: Right, it's very flexible, extremely flexible.
- 2:13Speaker 2: I mean you can write imperative step by step state mutations.
- 2:17Speaker 2: You can build these massive, prototype heavy object oriented webs
- 2:21Speaker 2: You can even lean heavily into metaprogramming by manipulating the
- 2:26Speaker 2: execution model itself with proxies and reflection. Yeah. But and
- 2:30Speaker 2: this is the key. It also provides the foundational tools
- 2:34Speaker 2: for functional programming right out of the box, specifically through
- 2:37Speaker 2: how it treats functions.
- 2:39Speaker 1: So rather than that mechanical watch where every gear is
- 2:41Speaker 1: locked into the adjacent gear, Fogus frames functional programming as
- 2:45Speaker 1: like an industrial assembly line.
- 2:46Speaker 2: I love that analogy me too.
- 2:48Speaker 1: So raw materials, which is your data, go in one end,
- 2:52Speaker 1: and as they move down the configor belt, they are
- 2:54Speaker 1: transformed by these discrete single purpose machines right the verbs exactly.
- 2:59Speaker 1: And a huge constraint of this assembly line is immutability.
- 3:03Speaker 1: So the original raw materials are never destroyed and they're
- 3:06Speaker 1: never mutated in place. You are always producing a brand
- 3:10Speaker 1: new version of the data at every single station on
- 3:13Speaker 1: the line.
- 3:13Speaker 2: So let's look at how that actually changes the developer's
- 3:16Speaker 2: day to day approach in the real world. Fogus introduces
- 3:20Speaker 2: a really practical example early on with this parsiage function. Right,
- 3:23Speaker 2: the goal is super simple. You take a user input
- 3:26Speaker 2: string and you parse it into an integer. In a
- 3:29Speaker 2: standard imperative approach, you might write a function that parses
- 3:32Speaker 2: the string. Sure, but it also probably contains hard coded
- 3:36Speaker 2: console dot log statements for debugging oh always yeah, or
- 3:40Speaker 2: maybe direct DOM manipulations to show warning banners on the
- 3:43Speaker 2: screen if the input is malformed.
- 3:45Speaker 1: I see that pattern all the time. The developer tightly
- 3:47Speaker 1: couples the business logic, which is parsing the age, with
- 3:50Speaker 1: the reporting logic.
- 3:51Speaker 2: Exactly, and that coupling makes the code incredibly brittle. Think
- 3:55Speaker 2: about it if your environment changes. Say you want to
- 3:58Speaker 2: move that exact same parsiage function from a browser environment
- 4:01Speaker 2: to a no JS.
- 4:02Speaker 1: Server where there is no dom.
- 4:03Speaker 2: Right, there is no dom to manipulate, so the function
- 4:07Speaker 2: just completely breaks. Or say you decide you want to
- 4:10Speaker 2: send your errors to a telemetry service like data dog
- 4:14Speaker 2: instead of just a browser console.
- 4:15Speaker 1: You basically have to rip open the core business logic
- 4:18Speaker 1: of your application just to update your logging sets.
- 4:21Speaker 2: It's a mess.
- 4:22Speaker 1: But focuses functional alternative abstracts all of those actions. He
- 4:26Speaker 1: pulls the concepts of warnings and errors and notes out
- 4:30Speaker 1: into their own isolated single purpose.
- 4:33Speaker 2: Functions be hindy verbs.
- 4:34Speaker 1: Right, things like a fail function or worn function, and
- 4:37Speaker 1: then the parsiage function just takes those verbs in as arguments.
- 4:40Speaker 1: So if you want to change how the system logs data,
- 4:43Speaker 1: you literally just inject a different worn function into the pipeline.
- 4:46Speaker 1: The actual parsing logic is never ever touched.
- 4:49Speaker 2: It's basically a textbook application of inversion of control, but
- 4:53Speaker 2: done through functional composition rather than relying on one of
- 4:56Speaker 2: those really heavy op dependency injection frame works.
- 5:00Speaker 1: Yeah, this can get so glowed.
- 5:02Speaker 2: They really can't here. Functions just become these perfect hermetically
- 5:06Speaker 2: sealed units of abstraction. They don't know anything about the
- 5:10Speaker 2: outside world, they don't care about the outside world. They
- 5:12Speaker 2: only know what is passed directly into them.
- 5:14Speaker 1: But to build an entire system out of these isolated verbs,
- 5:19Speaker 1: the language has to support them natively, right like this
- 5:22Speaker 1: hinges entirely on JavaScript treating functions as first class. Yes, now,
- 5:27Speaker 1: I know you listening are probably familiar with the concept
- 5:29Speaker 1: of first class functions, but let's just look at the
- 5:32Speaker 1: mechanical implications of it really quick. Because a function in
- 5:36Speaker 1: JavaScript is literally just an object under the hood. It
- 5:39Speaker 1: can be assigned to a variable, it can be stored
- 5:42Speaker 1: in an array, or like we just saw, it can
- 5:44Speaker 1: be passed as an argument.
- 5:46Speaker 2: And that capability is basically the engine of applicative programming.
- 5:50Speaker 1: Applicative programming, Yeah, which.
- 5:51Speaker 2: Sounds fancy, but it's simply the practice of a function
- 5:55Speaker 2: calling another function that was passed to it. It's the
- 5:57Speaker 2: core mechanism that powers the functional obstrc actions we use
- 6:01Speaker 2: to replace traditional control.
- 6:02Speaker 1: Flow, right, you mean the holy trinity of map, reduce,
- 6:06Speaker 1: and filter. We use these constantly to replace clunky forour loops.
- 6:10Speaker 1: But I want to push back on the architectural cost
- 6:12Speaker 1: of this for a second.
- 6:13Speaker 2: Okay, laid on me.
- 6:14Speaker 1: If we are strictly following this functional assembly line model,
- 6:17Speaker 1: we are constantly passing functions around, We're creating closures. We
- 6:21Speaker 1: are allocating memory for brand new arrays every single time
- 6:25Speaker 1: we call map or filter, all because we refuse to
- 6:28Speaker 1: mutate the original array.
- 6:30Speaker 2: Right, to maintain immutability.
- 6:31Speaker 1: Yeah, but if I just write a standard imperative for
- 6:34Speaker 1: loop that mutates an array in place, I am mathematically
- 6:37Speaker 1: utilizing way fewer CPU cycles and I'm allocating way less memory.
- 6:42Speaker 1: So at scale, doesn't treating everything as this immutable assembly
- 6:46Speaker 1: line of tiny functions just absolutely tank the application's performance.
- 6:51Speaker 2: It's a totally valid architectural concern, and honestly, historically, in
- 6:54Speaker 2: older runtime environments, you would be absolutely right. The overhead
- 6:58Speaker 2: of the call stack alone plus the garbage collection from
- 7:01Speaker 2: all those new arrays would be a massive bottleneck, right.
- 7:04Speaker 1: It would just choke the system.
- 7:05Speaker 2: But the mechanics of modern JavaScript engines, particularly Googles V
- 7:10Speaker 2: eight engine, you know, the one that powers Chrome and Node,
- 7:13Speaker 2: they drastically alter that math through runtime compilation strategies.
- 7:17Speaker 1: Okay, walk me through the mechanics of that, because how
- 7:20Speaker 1: does vight actually compensate for the overhead of hundreds of
- 7:25Speaker 1: tiny function calls.
- 7:26Speaker 2: Well, it all comes down to how just in time
- 7:28Speaker 2: or JIT compilation works. You see, V eight doesn't just
- 7:32Speaker 2: blindly execute your code line by line. It actually watches
- 7:36Speaker 2: the code execute, it gathers profiling data, wall it runs,
- 7:40Speaker 2: and then it dynamically optimizes it.
- 7:42Speaker 1: Wait, it optimizes it on the fly exactly.
- 7:45Speaker 2: And one of the main tools it uses to do
- 7:46Speaker 2: this is called inline caching. So if V eight sees
- 7:49Speaker 2: that a specific tiny function like say our isolated warn
- 7:52Speaker 2: function from earlier. If it sees that function being called
- 7:55Speaker 2: repeatedly with the same exact types of arguments, it will
- 7:57Speaker 2: eventually just stop executing it as a separate function call.
- 8:00Speaker 1: Wait, so it actually rewrites the execution path.
- 8:03Speaker 2: Yes, literally, it performs what's called code inlining. The compiler
- 8:07Speaker 2: takes the actual machine code instructions of that tiny worn
- 8:10Speaker 2: function and pastes them directly into the calling functions execution block.
- 8:15Speaker 1: Oh wow.
- 8:16Speaker 2: Yeah, so the overhead of the function call, the context switching,
- 8:19Speaker 2: pushing of popping variables from call stack, all of that
- 8:21Speaker 2: is entirely eliminated. V eight essentially flattens your beautiful, modular
- 8:26Speaker 2: functional code into this highly optimized, imperative machine code block
- 8:31Speaker 2: under the hood.
- 8:32Speaker 1: That completely changes the perspective on performance. So their argument
- 8:35Speaker 1: that a massive fore loop is always faster that completely
- 8:38Speaker 1: ignores how the modern engine actually optimizes the code.
- 8:41Speaker 2: Well yeah, and in fact, a massive forur loop that
- 8:44Speaker 2: does like five different things and mutates variables of all
- 8:46Speaker 2: different types can actually confuse the GI compiler. Oh really yeah.
- 8:51Speaker 2: Vight relies heavily on what are called hidden classes, which
- 8:54Speaker 2: are basically predictable object shapes to optimize property access. If
- 8:59Speaker 2: your imperative loop is constantly altering the shape of your objects,
- 9:02Speaker 2: you know, adding or deleting properties dynamically, vight will actually
- 9:05Speaker 2: de optimize that block of code. It kicks it back down, exactly,
- 9:08Speaker 2: It kicks it back down to slow, unoptimized execution. But
- 9:12Speaker 2: by writing small, pure functions that always return predictable data shapes,
- 9:17Speaker 2: you are actually feeding the V eight engine exactly what
- 9:20Speaker 2: it needs to optimize your code to near native speeds.
- 9:24Speaker 1: That is fascinating.
- 9:25Speaker 2: Do you get the developer experience of highly modular code
- 9:28Speaker 2: without having to sacrifice the run time performance?
- 9:30Speaker 1: Okay, so the engine basically has our back on the
- 9:32Speaker 1: performance side, But let's look at the developer experience of
- 9:35Speaker 1: actually snapping these functions together. Because you know, in the
- 9:38Speaker 1: real world they rarely fit perfectly out of the box.
- 9:41Speaker 2: No, they definitely do not.
- 9:42Speaker 1: So what do we do when two systems need to
- 9:44Speaker 1: talk but their function signatures are completely mispatched. Fogus dives
- 9:49Speaker 1: into this problem by looking at JavaScript's native array sorting mechanism,
- 9:53Speaker 1: which is notoriously quirky.
- 9:54Speaker 2: Oh it is a perfect case study and weird API design.
- 9:58Speaker 2: If you take an array of integers, say two, three,
- 10:02Speaker 2: negative six, zero, negative one oh eight, and forty two, okay,
- 10:06Speaker 2: and you just call the native dot sort method on
- 10:08Speaker 2: that array. It doesn't actually sort them numerically, right.
- 10:11Speaker 1: Which drives beginners crazy. It sorts them lexicographically. It coerces
- 10:15Speaker 1: everything to a string first, so negative one oh eight
- 10:18Speaker 1: comes before negative six, because the character one comes before
- 10:20Speaker 1: the character six in unicode. It's this bizarre historical artifact
- 10:24Speaker 1: from JavaScript's early days, when it was mostly just dealing
- 10:28Speaker 1: with string data from HTML forms, and so to get
- 10:31Speaker 1: a mathematical sort, the API forces you to pass in
- 10:35Speaker 1: a very specific comparator function.
- 10:37Speaker 2: And the contract for that comparator function is incredibly rigid, right.
- 10:41Speaker 2: It expects exactly two arguments, and it must return a
- 10:44Speaker 2: negative number if the first is smaller, a positive number
- 10:48Speaker 2: if it's larger, and zero if they're totally equal.
- 10:51Speaker 1: Which means usually developers just write this messy inline anonymous
- 10:55Speaker 1: function with some subtraction math or like an i false
- 10:58Speaker 1: block jammed directly inside the torque call.
- 11:01Speaker 2: Yeah, we've all written that code.
- 11:02Speaker 1: Oh totally. But Fogus abstracts this to show the power
- 11:05Speaker 1: of functional composition. He starts by writing a super generic
- 11:08Speaker 1: predicate function called less equal. It just takes two numbers
- 11:12Speaker 1: and returns a boolean just true.
- 11:13Speaker 2: Or false, right, and a predicat being any function that
- 11:16Speaker 2: takes a value and just returns a boolean. It is
- 11:19Speaker 2: basically the simplest, most reusable piece of logic you can
- 11:22Speaker 2: possibly write.
- 11:23Speaker 1: But the SARDAPI doesn't want a boolean.
- 11:25Speaker 2: No, it does not.
- 11:26Speaker 1: It will completely fail if you just pass it less
- 11:28Speaker 1: or equal. So Fogus writes a higher order function, which
- 11:31Speaker 1: is a function that returns another function, and he calls
- 11:34Speaker 1: it comparator. This comparator function takes any boolean predicate as
- 11:37Speaker 1: its argument, and it returns a brand new function that
- 11:40Speaker 1: maps the true false output into the negative one, zero
- 11:44Speaker 1: or one format that the dot sort API demands.
- 11:47Speaker 2: The mechanism there is what's really fascinating, Yeah, because you
- 11:50Speaker 2: are physically decoupling the core business logic, you know, evaluating
- 11:56Speaker 2: if a is less than b from the arbitrary framework
- 11:59Speaker 2: requirement of those nig one, zero one return values.
- 12:02Speaker 1: It's exactly like a travel adapter. That's a good way
- 12:04Speaker 1: to look at it, right, Like if I take my
- 12:05Speaker 1: laptop to Europe, the wall socket, which is the dot
- 12:08Speaker 1: sort API, it demands a specific three prong shape my
- 12:12Speaker 1: laptop charger, which is my less equal predicate. It only
- 12:15Speaker 1: has a two prong shape. I don't rewire my laptop
- 12:18Speaker 1: charger to fit the European socket.
- 12:20Speaker 2: No, that would ruin it for when you go back
- 12:22Speaker 2: home exactly.
- 12:23Speaker 1: Instead, I just plug my charger into an adapter, and
- 12:25Speaker 1: then I plug the adapter into the wall. Focus's comparator
- 12:29Speaker 1: function is that travel adapter.
- 12:30Speaker 2: And by not rewiring the core logic, that lesser equal
- 12:34Speaker 2: predicate remains completely pure and completely reusable. You can use
- 12:39Speaker 2: it tomorrow inside a filter function or a su function,
- 12:42Speaker 2: which actually do expect boolean returns. If you had buried
- 12:45Speaker 2: that logic inside of bespoke comparator, it would be locked
- 12:49Speaker 2: away forever and totally useless to the rest of your application.
- 12:53Speaker 2: And that is really the essence of functional composition, right.
- 12:57Speaker 2: You build complex behavior just by snapping the gether these tiny, generic,
- 13:01Speaker 2: re usable verbs.
- 13:02Speaker 1: But let's look at the other side of the equation,
- 13:04Speaker 1: because if our verbs are these tiny decoupled adapters, what
- 13:07Speaker 1: happens to our nouns? I mean, the prevailing philosophy in
- 13:10Speaker 1: most enterprise software is object oriented design, where the data
- 13:14Speaker 1: and the methods that operate on that data are all
- 13:16Speaker 1: bundled together into these massive classes. If we are decoupling
- 13:20Speaker 1: all the verbs, do we just toss out the classes entirely.
- 13:22Speaker 2: Well, this is where Fogus introduces a concept called data thinking,
- 13:26Speaker 2: and it aggressively challenges standard op doctrine. He actually quotes
- 13:31Speaker 2: Alan Perlis, you know, one of the pioneers in computer
- 13:33Speaker 2: science who famously said it is better to have one
- 13:37Speaker 2: hundred functions operate on one data structure than ten functions
- 13:41Speaker 2: on ten data structures.
- 13:42Speaker 1: Wow. That completely flies in the face of how most
- 13:45Speaker 1: developers are taught to model their domains. Oh, completely, because
- 13:48Speaker 1: we're taught that if you have a user, you build
- 13:49Speaker 1: a user class. If you have an invoice, you build
- 13:52Speaker 1: an invoice class.
- 13:53Speaker 2: And the fundamental problem with that approach is that you
- 13:56Speaker 2: are basically inventing a brand new, highly specific micro language
- 14:01Speaker 2: for every single entity in your entire Systemma. Well, a
- 14:05Speaker 2: user class might have a dot get role method, but
- 14:08Speaker 2: an invoice class has a dot calculatol method. If you
- 14:11Speaker 2: want to write a utility function to format some data,
- 14:15Speaker 2: it has to be custom tailored to understand the specific
- 14:18Speaker 2: bespoke methods of the user class or the invoice class.
- 14:21Speaker 2: Your logic becomes completely coupled to the arbitrary shape of
- 14:24Speaker 2: that specific class.
- 14:25Speaker 1: Ah I see, So Fogus argues that we should just
- 14:28Speaker 1: strip all of that away and rely on bare fundamental
- 14:31Speaker 1: data primitives.
- 14:32Speaker 2: Yes, just plain JavaScript objects, which are essentially just associative
- 14:37Speaker 2: arrays of key value pairs and standard arrays.
- 14:40Speaker 1: He demonstrates this with this lame CSV parsing example. So
- 14:44Speaker 1: imagine a simple data set of employees. They've got names, ages,
- 14:48Speaker 1: and departments. A traditional OP approach would parse that CSV
- 14:53Speaker 1: and instantiate like a table class, which then holds an
- 14:56Speaker 1: array of row classes which are mapped to individual employees classes.
- 15:00Speaker 2: Which creates a massive memory footprint, not to mention the
- 15:03Speaker 2: sheer amount of boilerplate code required just to define all
- 15:05Speaker 2: this classes in the first place.
- 15:06Speaker 1: Right, It's exhausting. But Fogus bypasses all of it. He
- 15:10Speaker 1: just parses the CSV directly into an array of plane
- 15:13Speaker 1: JavaScript objects, and that's it. That's the entire data model,
- 15:16Speaker 1: an array of objects. But okay, here is where I
- 15:19Speaker 1: have a major issue with this data thinking concept. If
- 15:21Speaker 1: you strip away all the classes, you are also stripping
- 15:24Speaker 1: away all the structural guarantees in a large code base.
- 15:28Speaker 1: If I pass an employee object into a function, the
- 15:32Speaker 1: class definition guarantees that the employee has an email property,
- 15:36Speaker 1: and it guarantees that the email is a string. Right
- 15:38Speaker 1: if we just throw raw arrays of loose objects around
- 15:42Speaker 1: the code base, aren't we basically just crossing our fingers
- 15:45Speaker 1: and hoping the data is shaped correctly? Like how do
- 15:48Speaker 1: you prevent the whole system from collapsing due to missing
- 15:51Speaker 1: properties without the strict contracts of a class.
- 15:53Speaker 2: So that right there is the defining trayoff between the
- 15:56Speaker 2: two paradigms. In an OP world, the validation happens internally
- 16:01Speaker 2: within the object itself. The object protects its own state exactly.
- 16:05Speaker 2: But in a functional data thinking world, you rely on
- 16:09Speaker 2: validation at the boundaries of your system.
- 16:11Speaker 1: Okay, what do you mean by the boundaries?
- 16:13Speaker 2: Think about when the data actually enters your application. Say
- 16:15Speaker 2: you're parsing adjason payload from an API request or you're
- 16:18Speaker 2: reading that CSV file from earlier. That is your boundary.
- 16:22Speaker 2: So you write robust validation functions right there at the edge.
- 16:25Speaker 2: If the data fails the schema check at the boundary,
- 16:27Speaker 2: you reject it immediately. But once the data passes the
- 16:30Speaker 2: boundary and enters your internal functional assembly line. You just
- 16:34Speaker 2: trust it. You don't need heavy classes to babysit the
- 16:37Speaker 2: data internally because you've already rigorously verified its shape at
- 16:40Speaker 2: the door.
- 16:41Speaker 1: Oh I see, So you decouple the structural validation from
- 16:44Speaker 1: the runtime behavior exactly. And once you do that, the
- 16:47Speaker 1: real power of that Alan Perliss quote really clicks into
- 16:50Speaker 1: place because your data is completely uniform. It's just planarrays
- 16:54Speaker 1: and objects, which means every single generic function in your
- 16:57Speaker 1: toolkit can operate on it.
- 17:00Speaker 2: Focus showcases this brilliantly by building an entire pseudosequal querying
- 17:05Speaker 2: engine using just functional concepts operating on these simple arrays
- 17:09Speaker 2: of objects.
- 17:09Speaker 1: Yeah, this part of the book is an absolute masterclass
- 17:13Speaker 1: because he builds the suite of functions to manipulate the
- 17:16Speaker 1: data as if it were an actual relational database. Like
- 17:19Speaker 1: he writes a project function which maps directly to a
- 17:22Speaker 1: seql select statement. You pass it your array of employee
- 17:25Speaker 1: objects and an array of keys you want, like say
- 17:28Speaker 1: name and department, and it just maps over the collection
- 17:31Speaker 1: and returns a brand new array of objects containing only
- 17:34Speaker 1: those specific keys.
- 17:36Speaker 2: And then he writes an as function which acts exactly
- 17:39Speaker 2: like a sequel alias mapping over the array to seamlessly
- 17:42Speaker 2: rename specific keys in the returned objects, and.
- 17:45Speaker 1: A restrict function, which is essentially just a seql wear clause.
- 17:49Speaker 1: It uses an underlying filter to evaluate a predicate against
- 17:52Speaker 1: each object, returning an array of only the objects that
- 17:55Speaker 1: actually pass the test.
- 17:57Speaker 2: But the mechanical brilliance here is the composability. The project
- 18:01Speaker 2: function takes an array of objects as an argument, and
- 18:03Speaker 2: it returns inn array of objects. The restrict function is
- 18:06Speaker 2: the exact same thing. Because their inputs and outputs share
- 18:09Speaker 2: the exact same primitive data structure. They just chain together flawlessly.
- 18:14Speaker 1: You can pass the result of restrict directly into project,
- 18:17Speaker 1: and then the result of that directly into As you
- 18:19Speaker 1: are executing highly complex relational queries against your data without
- 18:24Speaker 1: a single custom class, without a heavy RM, and critically
- 18:27Speaker 1: without ever mutating the original data set. It's just beautiful.
- 18:31Speaker 2: It's just data flowing smoothly through an assembly line of
- 18:33Speaker 2: pure verbs.
- 18:34Speaker 1: Right. And to address your earlier concern about memory overhead,
- 18:38Speaker 1: because JavaScript passes objects by reference, returning a new array
- 18:43Speaker 1: from restrict or project doesn't actually mean you are duplicating
- 18:47Speaker 1: the actual object data in memory over and over. Oh really, Yeah,
- 18:51Speaker 1: you are simply creating a new array that contains pointers
- 18:53Speaker 1: to the existing objects. So the memory overhead is surprisingly minimal,
- 18:58Speaker 1: But the architectural safety you gain by not mutating that
- 19:01Speaker 1: original array is just massive.
- 19:03Speaker 2: Which perfectly transitions into the final core concept that focus tackles,
- 19:08Speaker 2: which is the existential threat to all of this functional safety,
- 19:11Speaker 2: the big one, Because if we have this pristine assembly
- 19:14Speaker 2: line where data flows through isolated machines without ever being destroyed,
- 19:18Speaker 2: what is the single biggest danger to that factory? Mutability,
- 19:21Speaker 2: specifically mutability in the global scope.
- 19:24Speaker 1: And JavaScript is uniquely terribly vulnerable to this due to
- 19:28Speaker 1: how its lexical environment was originally designed.
- 19:31Speaker 2: Yeah, if you declare a variable without strict bindings using
- 19:35Speaker 2: the var keyword historically or just completely omitting a declaration
- 19:40Speaker 2: keyword entirely, the JavaScript engine handles that missing reference in
- 19:45Speaker 2: a very, very dangerous way.
- 19:47Speaker 1: Let's trace that execution path for a second. When the
- 19:50Speaker 1: interpreter hits a variable assignment that wasn't properly declared to
- 19:53Speaker 1: say global state equal true, it doesn't just crash.
- 19:57Speaker 2: No, it tries to be helpful.
- 19:59Speaker 1: Right. It looks at the current execution contexts environment record,
- 20:03Speaker 1: it doesn't find global state there, so it traverses up
- 20:05Speaker 1: the scope chain to the outer lexical environment, and it
- 20:09Speaker 1: just keeps moving up the chain until it hits the
- 20:11Speaker 1: global execution context.
- 20:13Speaker 2: And if it doesn't find it declared there either. Instead
- 20:15Speaker 2: of throwing a reference er, the engine quietly invisibly attaches
- 20:20Speaker 2: that variable directly to the global.
- 20:21Speaker 1: Object, which is terrifying.
- 20:23Speaker 2: It is in a browser, that's the window object. In
- 20:25Speaker 2: node it's the global object.
- 20:27Speaker 1: And Fogus illustrates this with a terrifyingly simple example. A
- 20:31Speaker 1: variable silently leaks to the global scope, so he writes
- 20:36Speaker 1: a global variable lavin la vida global. Now, because it
- 20:40Speaker 1: exists on the global object, it is fully accessible to
- 20:43Speaker 1: every single function, every module, and every third party script
- 20:48Speaker 1: running anywhere in that environment.
- 20:50Speaker 2: Which means literally any other piece of code may be
- 20:53Speaker 2: written by a completely different developer on another team can
- 20:56Speaker 2: execute an assignment like a global variable.
- 20:59Speaker 1: I drink your monad, and just like that, the state
- 21:02Speaker 1: is overwritten instantly without any warning whatsoever. No, and because
- 21:06Speaker 1: object oriented programming relies so heavily on internal state mutations,
- 21:11Speaker 1: if your classes depend on any external context, they are
- 21:14Speaker 1: highly highly susceptible to these invisible side effects. Yeah, when
- 21:18Speaker 1: your application inevitably crashes because a booleon suddenly became a string,
- 21:22Speaker 1: the stack trace isn't going to tell you who drank
- 21:24Speaker 1: your milkshake. It will only tell you that the code
- 21:26Speaker 1: failed three steps later when it tried to evaluate the string.
- 21:29Speaker 1: It's the broken tooth on the mechanical pocket watch all
- 21:32Speaker 1: over again.
- 21:33Speaker 2: And the functional programming heavily mitigates this exact problem through
- 21:38Speaker 2: the principle of referential transparency. A pure function does not
- 21:43Speaker 2: read from the global scope, and it absolutely does not
- 21:46Speaker 2: write to the global scope. Its output is derived entirely
- 21:49Speaker 2: and exclusively from the arguments passed explicitly into it. If
- 21:54Speaker 2: a function needs configuration data, you have to pass that
- 21:57Speaker 2: data in as an argument.
- 21:59Speaker 1: You don't just reach out into the ether to grab it.
- 22:01Speaker 1: You enforce a very strict contract of explicit inputs and
- 22:05Speaker 1: explicit return values. Literally, no one can overwrite your data
- 22:09Speaker 1: because you never expose a mutable reference to it. You
- 22:12Speaker 1: only ever return a newly transformed copy.
- 22:15Speaker 2: And when you apply that discipline across an entire code base,
- 22:18Speaker 2: you just eliminate entire categories of bugs. The cognitive load
- 22:22Speaker 2: required to understand the system drops dramatically because you don't
- 22:25Speaker 2: have to keep this massive mental map of what else
- 22:28Speaker 2: might be secretly modifying your variables.
- 22:29Speaker 1: You just look at the assembly line exactly.
- 22:31Speaker 2: You just observe the data flowing from one station to
- 22:34Speaker 2: the next.
- 22:34Speaker 1: It really highlights how focus is functional. JavaScript is so
- 22:37Speaker 1: much less about learning the syntax of a specific library
- 22:41Speaker 1: like Underscore and way more about adopting this defensive, scalable
- 22:45Speaker 1: engineering mindset. For sure, we've explored how JavaScript's multi paradigm
- 22:50Speaker 1: nature allows us to basically shift away from rigid oop structures.
- 22:54Speaker 1: We unpacked how first class functions power those applicator techniques,
- 22:58Speaker 1: and how modern jit can like vight, make these abstractions
- 23:02Speaker 1: incredibly performant under the hood.
- 23:04Speaker 2: Yeah, and we saw how higher order functions can act
- 23:06Speaker 2: as adapters to decouple our logic from framework constraints, and
- 23:10Speaker 2: how data thinking allows us to model complex you know,
- 23:13Speaker 2: pseudoseqal pipelines using literally nothing but primitive rays, plane objects,
- 23:18Speaker 2: and highly composable verbs.
- 23:19Speaker 1: And ultimately we saw how enforcing a mutability and avoiding
- 23:22Speaker 1: the global scope chain protects our systems from unpredictable state mutations.
- 23:26Speaker 1: It's an entirely different way of modeling the world, which
- 23:29Speaker 1: actually leaves me with a final thought for you to
- 23:31Speaker 1: consider as you step away from this.
- 23:33Speaker 2: Deep dive to curious.
- 23:34Speaker 1: Well, we spent this whole time talking about software architecture, right,
- 23:38Speaker 1: but the principles of object oriented versus functional design apply
- 23:42Speaker 1: really heavily to how we structure human systems too.
- 23:45Speaker 2: Wait, how so think.
- 23:46Speaker 1: About corporate structures. We very often organize our teams using
- 23:50Speaker 1: rigid object oriented principles. We build these complex, heavy hierarchies,
- 23:55Speaker 1: you know, VP classes, director classes, manager classes, and we
- 23:58Speaker 1: lock specific responsibilities and information silos deep inside those titles.
- 24:03Speaker 2: That is painfully accurate.
- 24:05Speaker 1: Right, and then when the market shifts and a totally
- 24:08Speaker 1: new problem arises, our rigid human classes struggle to adapt
- 24:12Speaker 1: because all the communication pathways are so tightly coupled to
- 24:15Speaker 1: the hierarchy.
- 24:16Speaker 2: We end up spending months trying to rewrite the org
- 24:18Speaker 2: chart just to solve the new problem.
- 24:20Speaker 1: Exactly, Well, what if we apply data thinking to our teams.
- 24:24Speaker 1: What if we stripped away the rigid hierarchies and viewed
- 24:27Speaker 1: our organizations as functional assembly lines. The raw material is
- 24:30Speaker 1: the project, The verbs are the specific skills of the
- 24:34Speaker 1: individuals on the team completely regardless of their title.
- 24:38Speaker 2: That's a really powerful way to look at it.
- 24:39Speaker 1: Could we build more resilient, adaptable companies if we stopped
- 24:42Speaker 1: obsessing over managing the state of our org charts and
- 24:45Speaker 1: started focusing on building better pipelines for our people's skills
- 24:49Speaker 1: to flow through. So next time you're stuck in a
- 24:52Speaker 1: bureaucratic bottleneck, ask yourself, are we acting like a broken
- 24:55Speaker 1: pocket watch or are we building an assembly line?