What is Functional Programming?

What is Functional Programming?

April 7, 2017

Functional programming is an important, yet overlooked, concept in software development. In a world dominated by object oriented programming (OOP) and separation of concerns, most developers completely overlook the functional programming paradigm when looking for jobs or learning new technologies. In this article, we discuss the basics of what functional programming is and the benefits of practicing it when writing code.

What is functional programming?

Functional programming is a paradigm for writing code. In other words, functional programming is not what you write your code with but HOW you write your code. It is a way of approaching development challenges.

Object oriented programming is also considered a paradigm. When you organize your code into classes with inheritance, data types, and methods you are really just adhering to the OOP paradigm.

How is functional programming different?

If both OOP and functional programming are just paradigms, then what makes them different? There are a few basic concepts that distinguish functional programming from other paradigms. They are:

  • Pure vs impure functions
  • Shared state
  • Immutable data types
  • Declarative vs imperative design

Let's discuss each one of these topics and how they apply to functional programming:

Pure vs Impure Functions

Functional programming relies heavily on pure functions. A function is considered pure if it always returns the same output for a given input and has no side effects. Side effects can be anything from printing a message to the console or calling another function from within the parent function. Take the following as an example:

An impure function

function(x){
  console.log("hi!");
  User.save()
  return x * 2;
}

This function is considered impure because of the side effects. While the function will always return x * 2 it's also updating some User object and printing a message to the console. These side effects make the function impure because we can't guarantee the same thing will happen every time. What if the User.save() method silently fails or we forget we are logging messages to the console?

Now let's take a look at a pure function:

A pure function

function(x){
  return x * 2;
}

This is considered a pure function since it will ALWAYS return the same result based on the parameters we give it.

Why is this important? Functional programming revolves around independent functions feeding other functions. The result of one function is used as the parameters of another function (f(a(x))). When functions are pure, we can safely use them within other functions. Their return values are reliable and independent of other functions. This results in more reliable code.

Shared State

In object oriented programming, you have classes with private/public methods. They can communicate with other classes, share data, etc. When objects rely on one another like this they have a shared state.

Shared state can be a problem for several reasons. When things don't occur in the right order or different functions finish running at different times you can run into issues. Functional programming avoids the idea of shared state. Everything is self contained inside functions. Replacing functions with their resulting values will not alter or effect different parts of the program.

Immutable Data Types

Functional programming encourages immutable data types. An immutable object is one that can't be changed after creation. This ensures consistency with data flow and prevents unpredictable things from happening.

Declarative vs Imperative

Functional programming falls within the declarative paradigm. This essentially means the code you write tells your program WHAT to do vs HOW to do it. For instance, a for loop is an imperative statement because it explains what to do FOR each item in a data set, whereas something like getData(getUser())is more declarative in that it doesn't describe how to get the user and simply calls another function.

Conclusion

This is a basic rundown of what characterizes functional programming. Remember that it's a paradigm and more of an approach to problem solving. You can practice functional programming with whatever language you choose. The overarching concept emphasizes a strict separation of concerns and reliability with data flow. For a more in depth explanation of functional programming, see Eric Elliott's Master the JavaScript Interview: What is Functional Programming?.


Join the conversation...

Posted by Tom
April 10, 2017

good explanation. important to remember that functional programming is a paradigm. i feel like most devs out there that struggle with this concept are already understanding it in different ways.


More by the stackchief