Check out a free preview of the full Functional JavaScript First Steps course:
The "Why Functional JavaScript" Lesson is part of the full, Functional JavaScript First Steps course featured in this preview video. Here's what you'd learn in this lesson:

Anjana explains that functional programming is more predictable and easier to test and debug. Using functional programming in JavaScript can be an easier alternative to object-oriented techniques or prototypical inheritance.

Get Unlimited Access Now

Transcript from the "Why Functional JavaScript" Lesson

>> Okay, so we've just talked about this a little bit but this really brings us back to why would we bother doing this? Why would we bother jumping through the hoops of making sure we have no side effects of making sure our functions are deterministic and always return the same output based on the same input?

[00:00:17] Why? Why are we here? Why are we spending our short time on the planet, thinking about functional programming. Well, as I just mentioned, when functions are deterministic, when you know exactly what output you gonna get given what input, you get code that's much more predictable, right? You don't have to worry about like, well what if name has been changed?

[00:00:45] Or what if somebody deleted the name variable or I don't know. So that means there's also less opportunity for bugs to arise. It's safer, it's more dependable, you could say because of that determinism. That means, it's also much easier to test and to debug and to find places where there are problems because If you've ever tested a function that's not pure, you might have had to do things like do all this complicated setup to kind of mock out the state of the world because the state of the world matters for how the program runs.

[00:01:20] Whereas with a functional program, which is going to boil down to a pure function, all you need to provide to test your function is a test input. Give it a string, see if it gives you the back of the right string back. Give it an array of numbers, see if it gives you the right different array of numbers back and so that makes it much easier to test our code and to maintain our code and to think about the interactions between different parts of our program because they each become kind of these isolated pure deterministic units.

[00:01:55] And we're gonna talk about how we can build complex programs out of simple functions like this because these pure functions, in essence, become very simple, how we can get complexity out of that we're gonna talk about a lot later. Now, some bad reasons to use functional programming include because thinking that it is the best paradigm that all those other paradigms we mentioned are somehow substandard, not the case.

[00:02:22] As I mentioned earlier, all these different programming paradigms they have something interesting to teach us about how we can think about programs. And they all are kind of a different flavor of programming that we can use at different times, depending on our needs and depending on the mindset that's most helpful for us.

[00:02:45] But functional programming has a lot of advantages and so especially for writing code, for example, like data transformations or things where you need to be really certain about what the results of your program are gonna be because it's a high stakes environment. That's a really great time for functional programming, but it doesn't mean it's always the best choice.

[00:03:06] So, by the end of the day, we should have a little bit more of a sense and we're gonna get to talk about it later over the course of our exercises, as to why and when functional programming is a good fit. Another terrible reason to use functional programming is because it's so buzzword II and it's so hip and it's so cool and it makes you look super smart to other programmers.

[00:03:26] That's shouldn't be our objective here our objective should be to get a deeper understanding of how we can think about the world of programs and the world of computation. And that will make us smart, and it will make us feel smart and eventually look smart. But that's not our objective here our objective is not to impress people with lots of fancy terminology and Greek symbols, it's to understand our programs better write code that's easier to test easier to debug, make our lives easier as developers.

[00:03:58] And that's why in this workshop, we're really focusing on those ideas and not on the kind of fancy jargon and whatnot, which it can be said is fun to learn sometimes and can be fun to dig into but that's not what we're focusing on today. So now the question becomes why are we doing this in JavaScript?

[00:04:21] And if I'm a JavaScript developer, why is functional programming something that I might be interested in? Well, if you ask me, object-oriented JavaScript is hard. [LAUGH] I think that prototype inheritance and the understanding what this is gonna be, depending on where it's called, like all of this stuff when I was learning JavaScript, I found really tricky I don't know about you all.

[00:04:45] But I found this very hard to think about and very hard to remember and very hard to understand and look at my code and know what it was gonna do. And so when I started programming in a functional style in JavaScript, that became so much easier, because again, the functions are predictable.

[00:05:02] You can test them, you can see exactly what they're gonna do if you give them a certain input. And so to my mind, functional JavaScript is actually a lot easier to write and to work with than object oriented. That's just my opinion yours may vary, but another great reason to learn functional programming in JavaScript Is because there is an established community as we know from all the other great functional courses on front end masters.

[00:05:30] There's a really great community of folks who are doing functional programming in Java Script. There's a lot of tools, there's a lot of libraries, there's even frameworks that are based of functional principles. There is a lot of resources for you to Help you in your functional programming journey that don't require you to learn a new syntax for a new language.

[00:05:51] I don't require you to bang your head against academic textbooks by yourself in your room on your own. You have people you have tools, you have community. And if you ask me, that is the most important thing when you're trying to learn anything, but especially anything about programming.

[00:06:06] Is making sure you are surrounded by a community of learners and other people who learn in public, other people who like teaching and sharing their learning. That is a really great way to learn any concept, especially a mind bending one like functional programming. So JavaScript is a great environment for doing that because the community is so supportive of functional programming.

>> How long should a function be like how many lines?
>> How many lines should a function be? That is a, sorry,
>> The maximum of two lines like I heard, like if it's more than 15 lines, you have to do two functions like yeah
>> How many lines should a function be at most I did not think that there is a number.

[00:06:55] That is an answer to that. I do not think that a 15 line function is necessarily a bad function. Someone in chat raised the point, that each function should do only one thing. I think that is a great rule of thumb. That each function should do one kind of operation or one kind of computation.

[00:07:18] However, as we're gonna see later, we can make really complex functions that do some kind of complex thing out of much simpler functions where it's much easier to see exactly what it does. So the tendency in functional programming, is that you're gonna end up with very short functions.

[00:07:39] And as you're gonna see today, all of our functions are gonna be probably no more than a few lines. But that said, I don't think that focusing on the length of the function is the most important thing at this point, I think it's much more important to focus on the purity of the function and the predictability of the function.

[00:07:58] Is it deterministic? Do you know exactly what's gonna happen when you pass in certain arguments? Do you know what output you're gonna get? But as you're gonna see later, we are going to naturally tend to make smaller and smaller and smaller functions that then we can reuse and combine together using composition to make more complex operations that are actually end up quite short, because we're delegating a lot of that computation to other functions.