From the course: Functional Programming with PHP

Why use functional programming? - PHP Tutorial

From the course: Functional Programming with PHP

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 I've found to answer this question 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 PHP, I'm going to assume that you currently use primarily PHP, but the code you write follows an object oriented paradigm. And because of this, I'm going to make a further assumption. I'm going to assume that you've 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, 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 size program, thousands of different variables are being operated in thousands of different places at run time, and the application can get itself into buggy states, which can be difficult to recreate. And 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. 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 taking the large number of complex ideas present in any large computer program and organizing them in a coherent way, while at the same time, making sure that the code remains easy to test and modify. Of course, this goal is what object oriented programming aims to do as well, although it achieves it with only varied success. The difference between object oriented and functional programming is in the choices they make with regards to organizing code. As you probably know already, the basic idea behind object oriented programming, is that humans think in terms of objects and relationships between these objects, and therefore, computer programs should be organized in terms of objects so that we can leverage our existing thought patterns to write programs more effectively. Now the problem with this of course, is that our thought patterns are often not as well defined as we sometimes like to think. Once we go beyond contrived examples such as SUV as a subclass of car, car as a subclass of vehicle, which demonstrates the concept, but well, frankly isn't very helpful, the object oriented programming doctrine doesn't provide much guidance regarding code organization. What we're left with is a rather daunting array of concepts like interfaces, subclasses, constructors, public, private, and protected data, accessors, mutators, and method overwriting, all of which form a complicated web of relationships that are prone to abuse by all but the most experienced and disciplined programmers. So that's object oriented programming. Functional programming on the other hand, aims to bring 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 parts of a computer program as simply as a mathematical function, such as the function of X equals X plus one. Where could a bug possibly be hiding in a function like this? Now that's just a very basic example, we'll look further into the details of functional programming along with the many differences between it and object oriented programming throughout the rest of the course. And again, since many of you are probably most familiar with object oriented programming, I'll continue to use it in a lot of comparisons with functional programming to clarify the concepts I'll show you.

Contents