functional programming

We are writing cleaner code that is more predictable and easier to test ever since we began implementing some of the key concepts of functional programming. Functional programming is a programming model in which everything is created within pure functions, emphasizes immutability, and ignores external state.

Pure Functions

A pure function is a function in which the same inputs will always return the same output. Pure functions are independent of external state and are immutable. Removing the risk of bugs due to external variables and functions.

We can detach functions from an external state, by passing all required values as parameters to our function. This guarantees we encompass all required computational values within the function itself. If a bug arises within our function we only have to look within the function itself to debug. Otherwise, we may have an inconspicuous external variable that may alter the intent of our function. This can be seen when an external variable changes data type or structure.

Immutability is when we create new versions of variables when updating instead of updating variables in place. For example, instead of adding an item to an array we would create a new array to store the original value along with the new item. By making variables immutable we are easily able to track state. This becomes valuable when performing any type of parallel processing because it removes the risk of functions using stale data.

Functional Programming Examples

When first being exposed to functional programming it can be hard to comprehend the associated concepts. Therefore, we have put together some examples to further explain the concepts we discussed earlier.

In this example, we are calculating the area of a circle. We have encapsulated the value of pi within our function instead of globally and are simply returning the result of the calculation. Setting pi at a global level would make our function impure and would make testing this function harder as we wouldn't know to what precision pi is specified.

def area_of_cirlce (radius):
    pi = 3.14
    return pi * (radius ** 2)

Below is an example, where we could easily mutate the numbers parameter which is a list of numbers. Instead of removing the odd numbers from the list, we create a new list and return to the calling function. Keeping the original state of the list in tact.

def  get_evens(numbers):
    evens = []
    for number in numbers:
        if (number % 2 == 0):
            evens.append(number)
    return evens

Conclusion

Practicing functional programming has helped our code to be more predictable and easier to test, due to the simplicity and purity of our functions. External considerations aren't needed because we are writing more reusable and efficient functions that ignore external state.

Have more questions about functional programming? Contact us today, we would love to hear from you.

How functional programming can simplify your code and eliminate side effects
Tagged on: