DEV Community

Cover image for 🚀 Part 1 : Unlocking the Power of Pure Functions
MAHMUDUL HASAN NABIL
MAHMUDUL HASAN NABIL

Posted on • Edited on

🚀 Part 1 : Unlocking the Power of Pure Functions

In the world of functional programming, pure functions are game-changers! They shape how we write, test, and scale code, especially in environments that require concurrency and parallelism.

Imagine you have a simple recipe for lemonade. Every time you follow this recipe exactly the same way — using the same ingredients and measurements — you’ll always get the same delicious lemonade. That’s what makes it a pure function: same ingredients, same result every time!

A pure function is one that, given the same inputs, will always produce the same output without causing any side effects (no modifying external state, no database writes, etc.).

This simple concept is a fundamental building block of functional programming.

Let’s see some examples to clarify:

Pure Function Example

const pureSort = (arr) => arr.slice().sort((a, b) => a - b);

// Test the function
const numbers = [3, 1, 4, 1, 5];
const sortedNumbers = pureSort(numbers);

console.log(sortedNumbers); // [1, 1, 3, 4, 5]
console.log(numbers);       // Original array remains unchanged: [3, 1, 4, 1, 5]
Enter fullscreen mode Exit fullscreen mode

👉Note: In a truly pure function, the function should not modify the original array. Here’s how we can do this by using slice() to create a shallow copy of the array internally, followed by sort() to return a sorted version. Since slice() doesn’t alter the original array, this approach remains entirely pure and ensures we get a sorted copy
without any side effects.

Impure Function

const impureSort = (arr) => arr.sort((a, b) => a - b);

console.log(impureSort(numbers)); // [1, 1, 3, 4, 5]
console.log(numbers);             // Original array is now modified to [1, 1, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

👉 Note: In the impure version, the function changes the original array, creating a side effect that can lead to unintended behavior if numbers is used later in the program.

Semi-Pure function

const semiPureSort = (arr) => {
    const arrCopy = [...arr];  // Copying input to avoid mutating the original
    return arrCopy.sort((a, b) => a - b);
};

const numbers = [3, 1, 4, 1, 5];
console.log(semiPureSort(numbers)); // [1, 1, 3, 4, 5]
console.log(numbers);               // Original array remains [3, 1, 4, 1, 5]

Enter fullscreen mode Exit fullscreen mode

👉Note: This semiPureSort function is only partially pure because it creates a copy of the input, leading to a side effect in terms of memory usage. However, it’s generally considered “pure enough” because it does not directly alter external state or have unpredictable outcomes.

Here’s why pure functions are such a big deal and how they can make your code more predictable, maintainable, and performant.

Key Benefits of Pure Functions

1. No Side Effects: Pure functions don’t modify any external state. This makes them predictable and easy to debug because there’s no risk of a function unexpectedly altering data elsewhere in your program.

2. Immutability: Pure functions work well with immutable data. They don’t change their inputs but instead return new values. This leads to fewer bugs because you don’t have to track changes across your codebase.

3. Simpler Testing: Because pure functions are isolated and always return the same output for the same input, testing becomes a breeze. You only need to focus on inputs and outputs without setting up elaborate test environments.

4. Better Composition: Pure functions are building blocks that can be easily combined with other functions. This modular approach allows you to build complex operations by composing smaller, reusable functions.

5. Performance Optimizations: Pure functions enable techniques like memoization (caching function results based on inputs) since you can rely on the function to return consistent results.

6. Concurrency and Parallelism: Here’s where things get interesting! Pure functions are excellent candidates for concurrent and parallel execution. Let’s dive deeper into this one.

Have you tried pure functions in a concurrent environment? What challenges did you face?

🚀 Ready for more ? Check out Part 2 : Unlocking the Power of Pure Functions to dive deeper!

Top comments (0)