Latest / CyberSecurity Summary / Functional Programming in JavaScript: How to improve your JavaScript programs using functional techniques
Transcript
- 0:00Speaker 1: Welcome everyone to another deep dive. You know, our goal
- 0:02Speaker 1: here is always the same, to help you get a
- 0:04Speaker 1: really thorough, multi perspective understanding of complex topics without feeling
- 0:10Speaker 1: totally overwhelmed by information overload.
- 0:12Speaker 2: Right, because there there's just so much noise out there, exactly.
- 0:18Speaker 1: And today's mission is unpacking a really fascinating book. It's
- 0:20Speaker 1: called Functional Programming in JavaScript by Lewis Atensio. A great read, Yeah,
- 0:25Speaker 1: it really is. And the goal today is to demystify
- 0:29Speaker 1: functional programming, or FP as we'll call it, and figure
- 0:33Speaker 1: out how it acts as a well a shortcut to
- 0:36Speaker 1: writing cleaner, less buggy.
- 0:38Speaker 2: Code, specifically in JavaScript, which, as anyone who writes it knows,
- 0:41Speaker 2: can be a bit of a wild West.
- 0:43Speaker 1: Oh absolutely. I mean, as web apps get more complex,
- 0:46Speaker 1: JavaScript's dynamic, mutable nature can just turn your code into
- 0:49Speaker 1: a totally tangled mess.
- 0:50Speaker 2: Yeah, complete house of cards.
- 0:52Speaker 1: Right. So we're going to explore how shifting to a
- 0:54Speaker 1: functional mindset can actually protect you from JavaScript itself.
- 0:58Speaker 2: It's a fundamental paradigm shift, really, moving from an imperative
- 1:01Speaker 2: way of thinking to a declarative one.
- 1:03Speaker 1: And there's this brilliant quote from Michael feathers and the
- 1:06Speaker 1: text that really nails this. He says, Ooh, object oriented
- 1:11Speaker 1: programming makes code understandable by encapsulating moving parts, and FP.
- 1:16Speaker 2: Makes code understandable by minimizing moving parts.
- 1:20Speaker 1: Yes, minimizing the moving parts, which is just I mean,
- 1:24Speaker 1: it's such a perfect way to frame it.
- 1:26Speaker 2: What's fascinating here is that it completely changes how you
- 1:29Speaker 2: approach problem solving. In imperative programming, you're detailing exactly how
- 1:34Speaker 2: a computer.
- 1:34Speaker 1: Should do a task, like manually managing a loop counter
- 1:38Speaker 1: just to square an array of numbers.
- 1:40Speaker 2: It's highly mechanical. But with declarative programming, you just describe
- 1:44Speaker 2: what the logic is without worrying about the control flow.
- 1:46Speaker 2: It's a lot like writing a SEQL query.
- 1:49Speaker 1: You just asked for the data. You don't tell the
- 1:51Speaker 1: database how to spin the hard drive to go find it.
- 1:53Speaker 2: Precisely, you separate the program's description from its evaluation.
- 1:58Speaker 1: I always like to think of imperative programming like micromanaging
- 2:02Speaker 1: a new employee to build a presentation. You're hovering over them, saying,
- 2:06Speaker 1: click this menu, type this word, make it bold, exactly right.
- 2:09Speaker 1: But declarative is like giving the task to a highly
- 2:12Speaker 1: competent assistant. You just give them the final goal and
- 2:15Speaker 1: trust their process.
- 2:16Speaker 2: And that trust lets developers focus on the actual business logic,
- 2:20Speaker 2: not the plumbing.
- 2:21Speaker 1: Yeah, the plumbing is where the bugs live. So to
- 2:24Speaker 1: build this trust, the book talks about two core pillars,
- 2:28Speaker 1: pure functions and immutability.
- 2:30Speaker 2: The foundational building blocks of FP.
- 2:32Speaker 1: Let's talk about pure functions first. These are functions that
- 2:36Speaker 1: depend only on the inputs you give them, and they
- 2:38Speaker 1: inflict zero changes beyond their scope.
- 2:41Speaker 2: Right, they operate in total isolation.
- 2:43Speaker 1: The book gives some great examples of impure functions to
- 2:46Speaker 1: contrast this, like date dot now.
- 2:50Speaker 2: Oh yeah, date dot now is completely unpredictable.
- 2:53Speaker 1: Because it relies on time. You call it twice, you
- 2:55Speaker 1: get two different answers. Or like modifying a global counter variable.
- 2:59Speaker 2: Which is affect. You're reaching outside the function and messing
- 3:02Speaker 2: with the global state.
- 3:03Speaker 1: Which leads us to referential transparency. Meaning if a function
- 3:07Speaker 1: always yields the exact same result for the same input,
- 3:10Speaker 1: it can literally be mathematically substituted.
- 3:12Speaker 2: Which makes the code incredibly predictable.
- 3:15Speaker 1: Predictability is exactly what we want. But the second pillar, immutability,
- 3:20Speaker 1: that's where things get tricky in JavaScript, very.
- 3:22Speaker 2: Tricky JavaScript loves mutation.
- 3:25Speaker 1: Yeah, the book points out this native flaw array dot sort.
- 3:29Speaker 1: It doesn't give you a nice new, sordid array, No.
- 3:32Speaker 2: It sorts it in place, right.
- 3:34Speaker 1: It modifies the original reference, creating this massive, dangerous side
- 3:38Speaker 1: effect which.
- 3:39Speaker 2: Can break entirely unrelated parts of your app that we're
- 3:42Speaker 2: relying on that original array.
- 3:43Speaker 1: Okay, let's unpack this. Yeah, because if we can't mutate objects,
- 3:47Speaker 1: and we can't change global variables or even print to
- 3:50Speaker 1: a screen without causing a quote unquote side.
- 3:52Speaker 2: Effect, I know where you're going with this.
- 3:54Speaker 1: I mean, what practical value does a pure program actually have?
- 3:57Speaker 1: Isn't an entire app just one giant side effect?
- 4:00Speaker 2: It is without side effects. Your program is basically just
- 4:04Speaker 2: a space heater for your room. It just burns CPU cycles.
- 4:07Speaker 1: Exactly, So how do we actually build anything useful?
- 4:09Speaker 2: Practical FP doesn't forbid side effects, it just manages them.
- 4:13Speaker 2: The goal is to tightly isolate the pure logic from
- 4:16Speaker 2: the impure interactions.
- 4:17Speaker 1: Oh okay, so like dom manipulation or database calls. Right.
- 4:22Speaker 2: The book uses this monolithic show student function as an example.
- 4:26Speaker 2: It queries the database, formats the data, and renders the
- 4:30Speaker 2: HTML all in one giant block.
- 4:33Speaker 1: The total nightmare to test.
- 4:34Speaker 2: I bet absolutely so the functional approach decomposes that into smaller,
- 4:38Speaker 2: single purpose pieces. You put all the impure database and
- 4:42Speaker 2: UI stuff at the very edges of your app.
- 4:44Speaker 1: Leaving the core logic perfectly pure, intestable exactly. Okay, that
- 4:48Speaker 1: makes sense. But enforcing that immutability in a language like JavaScript,
- 4:52Speaker 1: which naturally just lacks it, right, how do we tame
- 4:56Speaker 1: this wild nature?
- 4:57Speaker 2: Well, the book introduces a few strategies, like the value
- 5:00Speaker 2: object pattern.
- 5:01Speaker 1: Write the zip code example.
- 5:03Speaker 2: Yeah, By returning an object literal interface with pseudo private
- 5:08Speaker 2: variables inside a closure, you basically create a primitive like object.
- 5:12Speaker 1: It has no mutating methods. You can read it, but
- 5:15Speaker 1: you can't change it exactly.
- 5:16Speaker 2: It's a really solid defense.
- 5:18Speaker 1: But then there's object dot freeze, which is a native tool.
- 5:20Speaker 2: It is, but it has a major limitation. It's only
- 5:22Speaker 2: a shallow freeze.
- 5:24Speaker 1: Right, So, if I freeze a person object, the top
- 5:26Speaker 1: level stuff like their name is safe.
- 5:28Speaker 2: But if they have a nested property like address dot country.
- 5:32Speaker 1: That nest to property can still be completely mutated.
- 5:35Speaker 2: Yeah, it leaves you vulnerable unless you write recursive deep
- 5:39Speaker 2: freeze functions. To lock down every single level.
- 5:42Speaker 1: Which brings up a big concern. Isn't deep freezing every
- 5:46Speaker 1: single object or you know, costly making deep copies of
- 5:50Speaker 1: massive data structures just a total nightmare for performance?
- 5:53Speaker 2: It sounds like it would be.
- 5:54Speaker 1: Yeah, why go through all this trouble? I mean copying
- 5:57Speaker 1: huge JSON trees every time a single value changes.
- 6:01Speaker 2: Well, this connects to the bigger picture. You're avoiding cascading
- 6:04Speaker 2: bugs caused by a shared mutable state. The peace of
- 6:07Speaker 2: mind and testability you gain is huge.
- 6:10Speaker 1: Well, what about the performance hit.
- 6:12Speaker 2: That's where lenses come in. The book introduces them via
- 6:15Speaker 2: the ramda dot js library. They act as functional references
- 6:19Speaker 2: that let you read and immutably update nested properties.
- 6:22Speaker 1: Oh, using copy on write semantics exactly.
- 6:24Speaker 2: It gives you structural sharing for free. You aren't doing
- 6:27Speaker 2: heavy deep copies. You're just sharing the unchanged memory branches.
- 6:30Speaker 1: Oh wow, So you get the safety without the massive
- 6:33Speaker 1: memory overhead. That's really clever it is.
- 6:35Speaker 2: The syntax is unobtrusive too.
- 6:38Speaker 1: So JavaScript has some flaws, but its design actually naturally
- 6:43Speaker 1: enables functional programming too.
- 6:44Speaker 2: Right, Oh, definitely. In fact, functions in JavaScript are first.
- 6:48Speaker 1: Class citizens, meaning they can be assigned to variables passed.
- 6:51Speaker 2: Around, right, and that supports higher order functions, functions that
- 6:54Speaker 2: can accept other functions as arguments or return them like a.
- 6:58Speaker 1: Ray dot sort going on to that it's dangerous, but
- 7:02Speaker 1: it is a higher order function.
- 7:04Speaker 2: Yeah, And the book points out how JS natively converts
- 7:07Speaker 2: numbers to strings for sorting based on Unicode, right.
- 7:10Speaker 1: Which is why ten gets sorted before two, which is
- 7:13Speaker 1: just hilarious and awful.
- 7:14Speaker 2: It's the classic JS bug. But because sort is a
- 7:17Speaker 2: higher order function, you can pass a custom comparator function
- 7:20Speaker 2: into it to fix that behavior.
- 7:22Speaker 1: It's treating behavior as data.
- 7:24Speaker 2: Exactly, and that leads directly into closures and scopes. A
- 7:28Speaker 2: closure is basically a data structure that binds a function
- 7:31Speaker 2: to its lexical.
- 7:32Speaker 1: Environment at the exact moment it's declared. Yes, here's where
- 7:35Speaker 1: it gets really interesting. I like to picture a closure
- 7:38Speaker 1: like a snow globe.
- 7:39Speaker 2: Snow globe.
- 7:40Speaker 1: Yeah, the function is the little house inside, and the
- 7:44Speaker 1: closure traps all the surrounding snow, the variables in the
- 7:48Speaker 1: outer scope. Oh, I see, It traps them exactly as
- 7:51Speaker 1: they were when the globe was sealed. It prevents the
- 7:53Speaker 1: outside world from melting them or messing with them.
- 7:55Speaker 2: That's a great visualization and understanding that snow globe is
- 7:59Speaker 2: crucial because of the notorious loop counter ambiguity.
- 8:03Speaker 1: Bug, oh Man, the vara bug.
- 8:05Speaker 2: Yep. If you declare a loop with var, it gets
- 8:07Speaker 2: hoisted to the top of the function, and.
- 8:09Speaker 1: If you're creating closures inside that loop, they all end
- 8:12Speaker 1: up sharing that exact same vari reference.
- 8:15Speaker 2: So it mutates unexpectedly. By the time the loop is done,
- 8:18Speaker 2: all your closures point to the final maximum value of
- 8:21Speaker 2: varas it causes so many bugs.
- 8:23Speaker 1: The closures can also fix this right like using four eights.
- 8:26Speaker 2: Exactly for each creates a fresh scope for every iteration,
- 8:29Speaker 2: or you know, just using e S six's let qword
- 8:32Speaker 2: solves it by properly block scoping the variable.
- 8:34Speaker 1: It all comes back to managing states safely, and that
- 8:37Speaker 1: transitions perfectly into how we process data declaratively. We're fluent
- 8:41Speaker 1: chains and high level operation.
- 8:43Speaker 2: Yeah, moving away from manual loops entirely.
- 8:46Speaker 1: In standard OP you use method chaining like string dot
- 8:50Speaker 1: substring dot to lower case.
- 8:52Speaker 2: It reads left to right. But in FP you use
- 8:54Speaker 2: function chaining and lambda expressions.
- 8:58Speaker 1: A fat arrow function give you.
- 9:00Speaker 2: A really terse one line syntax that implicitly returns values, which.
- 9:05Speaker 1: Perfectly aligns with the FP rule that every function must
- 9:08Speaker 1: return a usable result.
- 9:10Speaker 2: The book shows this incredible example using dot map from
- 9:13Speaker 2: the low dash library.
- 9:14Speaker 1: Oh, the one extracting student full names.
- 9:17Speaker 2: Right, you extract the names from a whole collection of
- 9:19Speaker 2: objects without writing a single for loop.
- 9:21Speaker 1: You're just treating the data and the control flow as
- 9:24Speaker 1: simple connections between high level components.
- 9:26Speaker 2: It's so clean.
- 9:28Speaker 1: But wait, if we're purely nesting functional calls inside out. Yeah,
- 9:32Speaker 1: like concat to lowercase, say.
- 9:34Speaker 2: Right on the Lisp style nesting.
- 9:36Speaker 1: Right, that looks incredibly unreadable to me. How is this
- 9:39Speaker 1: actually better for developers if they can't easily read what
- 9:41Speaker 1: the code is doing.
- 9:43Speaker 2: This raises an important question, because you're right, deep nesting
- 9:46Speaker 2: is awful for human readability, and that is exactly why
- 9:49Speaker 2: functional programmers use tools like low dash or.
- 9:52Speaker 1: REMDA to create fluent chains.
- 9:54Speaker 2: Exactly, they create pipelines. The data flows sequentially from one
- 9:58Speaker 2: black box operation to the next, so.
- 10:01Speaker 1: It reads top to bottom or left to right again.
- 10:03Speaker 2: Yes, you preserve the mathematical purity, but you keep it
- 10:06Speaker 2: readable for the humans maintaining the code.
- 10:09Speaker 1: So what does this all mean, I mean FP and
- 10:12Speaker 1: JavaScript isn't just a trendy API you plug in.
- 10:15Speaker 2: No, it's not.
- 10:16Speaker 1: It's a profound shift in thinking. By embracing pure functions,
- 10:21Speaker 1: treating objects as immutable values, and leveraging those higher order
- 10:25Speaker 1: functions and closures we talked about, you build modular, highly
- 10:29Speaker 1: predictable applications.
- 10:31Speaker 2: And you don't even have to abandon object oriented programming entirely.
- 10:34Speaker 1: To do it right. The text mentions that sweet spot.
- 10:36Speaker 2: Yeah, JavaScript is a hybrid language. It allows you to
- 10:39Speaker 2: blend a rich op domain model for the broad architecture
- 10:42Speaker 2: with pure functional operations handling the data transformations inside.
- 10:47Speaker 1: It's the best of both worlds, truly. You know, before
- 10:49Speaker 1: we wrap up, I want to leave you the listener
- 10:50Speaker 1: with the final thought. We briefly touched on this concept
- 10:53Speaker 1: from the text called lazy evaluation right.
- 10:55Speaker 2: Deferring execution until the data is actually needed, just to
- 10:59Speaker 2: save cpusite.
- 11:00Speaker 1: Exactly, only computing data at the exact moment it's strictly
- 11:04Speaker 1: required to form an output.
- 11:06Speaker 2: It's highly efficient.
- 11:07Speaker 1: But what if we applied that same architectural philosophy to
- 11:10Speaker 1: our own human productivity?
- 11:12Speaker 2: Oh that's interesting.
- 11:13Speaker 1: Think about our information consumption? What if we stopped eagerly
- 11:18Speaker 1: processing the massive flood of daily data, the emails, the news,
- 11:22Speaker 1: the social media, and only evaluated the information strictly required
- 11:26Speaker 1: for the immediate decision in front of us.
- 11:28Speaker 2: Wow, we'd probably have a lot less mental burnout.
- 11:31Speaker 1: Right, Just like saving CPU cycles, maybe we need to
- 11:33Speaker 1: save our cognitive cycles. A functional mindset for the brain.
- 11:37Speaker 1: I love that something for you to moul over. Thanks
- 11:40Speaker 1: for tuning into this deep dive and we'll see you
- 11:42Speaker 1: next time.