From the course: Functional Programming with Java

Why use functional programming? - Java Tutorial

From the course: Functional Programming with Java

Start my 1-month free trial

Why use functional programming?

- [Instructor] So the first thing you may be wondering is what is functional programming anyway? This is a big question most people ask when they hear about how useful functional programming is, and the best way that I've found to answer it is to consider why we'd want to use functional programming in the first place. In other words, what are the problems that functional programming sets out to solve? Well, if you're watching a course about functional programming in Java, I'm going to assume that you currently use primarily Java and that the code you write follows an object-oriented paradigm. Now, the first thing I want to point out here, and this is a pretty common misconception in the functional programming world by the way, is that functional programming and object-oriented programming are not opposites. They can be used alongside each other. And in this course, we'll see exactly how this is done. Basically, there are just a few key places where object-oriented programming falls short or doesn't provide much guidance, and it's precisely in these areas that functional programming can step in and help. Don't worry if that doesn't quite make sense to you yet. You'll see exactly what I mean as we go through the course. So that being said, the next logical question is where exactly does object oriented programming fall short then? Well, I'm going to assume that you've probably run into this problem before. In the software you write, especially as programs get larger and more complex, certain bugs start to show up that are difficult to track down and fix. Partly because they're hard to recreate. You know, it takes a long convoluted series of steps to make them show up, and partly because even when you figure out how to recreate them, it's next to impossible for you to keep track of all the changes that occur while the program is running. In a typical enterprise-sized program, thousands of different variables are being operated on in thousands of different places at runtime, and the application can get itself into buggy states, which can be difficult to recreate. This is exactly the type of situation that functional programming aims to avoid. Once you learn to think and program in a functional style, this type of situation will become much less frequent. In this course, you'll learn many techniques that can be used to create very large, powerful, and virtually bug-free code bases that are composed of many smaller, self-contained, and easily testable parts. So then at its core, functional programming is concerned with helping us take the large number of complex ideas in any large computer program and organize them in a coherent way while at the same time making sure that the code remains easy to test and modify. As we'll see shortly, there are several ways that functional programming aims to do this, but all of them are centered around this idea. Functional programming brings the precision of mathematical functions into computer programs. While there will always be room for human error in programming no matter what paradigm is being used, adding the provable certainty that comes with mathematics into computer programming makes it far easier to avoid bugs. Imagine if we were able to represent all the parts of a computer program as simply as a mathematical function \ such as this one here, the function of X = X + 1. Where could a bug possibly be hiding in a function like this? Now we'll see exactly how to write our programs like this very shortly.

Contents