From the course: Functional Programming with Python

The goal of functional programming - Python Tutorial

From the course: Functional Programming with Python

Start my 1-month free trial

The goal of functional programming

- So the big question that most people ask when they hear about how useful functional programming is, is what exactly is functional programming? Well, to answer this question, it might be helpful to consider the question of why we'd want to use functional programming in the first place. In other words, what's the goal of functional programming, and what are the problems that it sets out to solve? Well, if you currently use a primarily object-oriented programming language, such as Java or C++, or especially a procedural programming language, such as C, you've probably run into this problem before. There are certain bugs 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-size program, thousands of different variables are being operated on, and thousands of different places at runtime. And the application can get itself into buggy states, which can be very difficult to recreate sometimes. 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. By learning functional programming, 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 sort of organization is what object-oriented programming aims to do as well. Although it achieves it with only varied success. The main 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 easily. 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 is a subclass of car, car is a subclass of vehicle, which demonstrates the concept, but isn't really very helpful. The object-oriented programming doctrine doesn't really provide much guidance regarding code organization. And what we're left with is a rather daunting array of concepts like interfaces, subclasses, constructors, public-private protected data accessors, mutators, and method overriding. All of which form a very complicated web of relationships, and are really prone to abuse by all but the most experienced and disciplined programmers. Now 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 or something like that. Where could a bug possibly be hiding in a function like this. 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 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 show you.

Contents