Programmers have been writing code for decades now. Billions of lines, gallons of coffee and countless hours in front of a keyboard. Over time, patterns and styles have emerged to make the task of building and maintaining code much easier. This is an ongoing process which has gone through many eras. From machine code to procedural code to patterns that are widely used today like imperative and Object-Oriented Programming (OOP), programming languages try to follow one or more of these paradigms to help facilitate development. Lately, one paradigm has been showing up in more and more languages due to many of its useful qualities.
Functional programming is a style of computing that treats a program like the computation of mathematical functions. Like a math equation, all function in this paradigm are deterministic. This means for any given input, the output will always be the same no matter how many times it is called. Since functions are such a critical part of functional programming, they are considered first-class functions. What this means is a function is treated like any other entity in a program such as an object or a number. They can be assigned to a variable and passed to another function. That function is called a higher-order function which means it can take a function as an input and/or return a function as an output. This behavior is used extensively. In this paradigm functions are said to be pure, meaning there are no side effects from running the function. What is a side effect? Anything that changes the state of the application or mutates an object. It doesn’t matter how you call or interact with the functions as long as the inputs do not change, the output does not change.
This behavior allows functions to be run in parallel since they are by definition, thread-safe. They also allow environments to safely cache commonly run functions since the same inputs will always produce the same output.
Some of you may be wondering how purely functional languages handle non-deterministic behavior. After all, the world isn’t as simple and clean as a carefully constructed math problem. How does a purely functional language deal with concepts like IO, Randomness and error handling? All of these concepts produce side effects which break the paradigm and seemingly make pure functional programing impossible. Like a shrewd lawyer, functional programming has an answer. While a function cannot cause a side effect, it can create a value to describe a desired side effect that the caller applies. In pseudocode:
Functional programming aims to make development like evaluating mathematical functions. This style avoids changing the state of a program and cause unintended consequences. This is a style to avoid errors due to mutable data and simplifies testing. It can handle side effects causing behaviors like interacting with memory or IO using functions that describe the side effect, so it can be dealt with transparently. Functional programming elements are showing up in every major language and it is worth checking them out to augment your code.