>> 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: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.
>> 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.