From the course: Learning Functional Programming with JavaScript (ES5)

What is functional programming? - JavaScript Tutorial

From the course: Learning Functional Programming with JavaScript (ES5)

Start my 1-month free trial

What is functional programming?

- [Instructor] First, we'll talk about some of the core concepts of functional programming. Now, there are many different possible definitions of functional programming, depending on who you ask, but most of them agree on the following concepts. First of all, functional programming keeps functions and data separate. It avoids state change and mutable data, and it treats functions as first-class citizens. Let's go through each of these briefly so you can see what I mean. The first concept, keeping data and functions separate, is best illustrated by a comparison with object-oriented programming, which does not follow this concept. Object-oriented programming, as we see in this simple class representing a person, likes to group together its data with the functions in charge of operating on that data. For instance, we have this variable, age, and then we have a function in charge of incrementing the variable. In contrast, functional programming stores its data in simple constructs like arrays or hashes and makes separate functions that take the data as an argument and return some sort of transformed version of the data. Notice that in this function, increaseAge, we're transforming a copy of the object, not the original object itself. In the next concept, we'll see why we do this. The main benefit of doing things this way, instead of using classes, is that it gives us immediate polymorphism with regards to our data. For example, the function we've created works out of the box with any piece of data that has an age field. Normally, we would have had to create classes for each type of object and add in an increaseAge function for each, or mess around with having a Superclass for all things with age. Now we come to our second concept. When we talk about mutability and state change, this is what we mean. As we can see in this simple example, the variable greeting contains several different values throughout its lifetime. In a small program like this one, this isn't really a problem, but in a full-sized program, a variable like this could hold many different values, depending on when and where it's referenced. This means that when modifying the program, we have to keep track of all the state changes this variable goes through, which can be very difficult for even an experienced programmer. The solution to this problem and the pattern often found in functional programming is to make all of our data immutable, either through the use of constants or simply through treating our data in an immutable way and not making unnecessary changes. This forces us to create a new variable to represent each change we want to make to the original. Notice how, just by looking at the name of each constant, we can infer what it contains and what it might be used for. This makes debugging much easier and is one of the big benefits of writing functional code. Since in functional programming, all of our data is contained in simple structures, as I mentioned before, the cost of copying and transforming the data in this manner is relatively inexpensive. And finally, functional programming treats functions as first-class citizens. What this means is that just as we can assign values, such as numbers or strings, to a variable, we can also assign functions. In the eyes of JavaScript, the third declaration here is virtually no different from the other two. This has some important implications for the ways in which we can organize our programs. First class functions give us the ability to both pass functions as arguments to other functions and to return functions. As we'll see later, this can give our programs amazing flexibility and allow us to significantly increase code reusability, in some cases.

Contents