DEV Community

Cover image for From Chaos to Clarity: A Declarative Approach to Function Composition and Pipelines in JavaScript
Ivan Brajković
Ivan Brajković

Posted on

From Chaos to Clarity: A Declarative Approach to Function Composition and Pipelines in JavaScript

Table of Contents


The Art of Clean Code 💎

Have you ever stared at someone else’s code and thought, “What kind of sorcery is this?” Instead of solving real problems, you’re lost in a labyrinth of loops, conditions, and variables. This is the struggle all developers face—the eternal battle between chaos and clarity.

Code should be written for humans to read, and only incidentally for machines to execute.Harold Abelson

But fear not! Clean code isn’t some mythical treasure hidden in a developer’s dungeon—it’s a skill you can master. At its core lies declarative programming, where the focus shifts to what your code does, leaving the how in the background.

Let’s make this real with an example. Say you need to find all the even numbers in a list. Here’s how many of us started—with an imperative approach:

const numbers = [1, 2, 3, 4, 5];
const evenNumbers = [];
for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] % 2 === 0) {
    evenNumbers.push(numbers[i]);
  }
}
console.log(evenNumbers); // Output: [2, 4]
Enter fullscreen mode Exit fullscreen mode

Sure, it works. But let’s be honest—it’s noisy: manual loops, index tracking, and unnecessary state management. At a glance, it’s hard to see what the code is really doing. Now, let’s compare it to a declarative approach:

const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]
Enter fullscreen mode Exit fullscreen mode

One line, no clutter—just clear intent: “Filter the even numbers.” It’s the difference between simplicity and focus versus complexity and noise.

Why Clean Code Matters 👨‍💻

Clean code isn’t just about looking nice—it’s about working smarter. Six months down the line, would you rather struggle through a maze of confusing logic, or read code that practically explains itself?

While imperative code has its place—especially when performance is critical—declarative code often wins with its readability and ease of maintenance.

Here’s a quick side-by-side comparison:

Imperative Declarative
Lots of boilerplate Clean and focused
Step-by-step instructions Expresses intent clearly
Harder to refactor or extend Easier to adjust and maintain

Once you embrace clean, declarative code, you’ll wonder how you ever managed without it. It’s the key to building predictable, maintainable systems—and it all begins with the magic of pure functions. So grab your coding wand (or a strong coffee ☕) and join the journey toward cleaner, more powerful code. 🌟✨


The Magic of Pure Functions 🪄

Have you ever encountered a function that tries to do everything—fetch data, process inputs, log outputs, and maybe even brew coffee? These multitasking beasts might seem efficient, but they’re cursed artifacts: brittle, convoluted, and a nightmare to maintain. Surely, there must be a better way.

Simplicity is prerequisite for reliability.Edsger W. Dijkstra

The Essence of Purity ⚗️

A pure function is like casting a perfectly crafted spell—it always yields the same result for the same input, without side effects. This wizardry simplifies testing, eases debugging, and abstracts complexity to ensure reusability.

To see the difference, here’s an impure function:

let discount = 0;   

const applyDiscount = (price: number) => {
  discount += 1; // Modifies a global variable! 😈
  return price - discount;
};

// Repeated calls yield inconsistent results, even with same input!
console.log(applyDiscount(100)); // Output: 99
console.log(applyDiscount(100)); // Output: 98
discount = 100;
console.log(applyDiscount(100)); // Output: -1 🤯
Enter fullscreen mode Exit fullscreen mode

This function modifies global state—like a spell gone awry, it’s unreliable and frustrating. Its output relies on the changing discount variable, turning debugging and reuse into a tedious challenge.

Now, let’s craft a pure function instead:

const applyDiscount = (price: number, discountRate: number) => 
  price * (1 - discountRate);

// Always consistent for the same inputs
console.log(applyDiscount(100, 0.1)); // 90
console.log(applyDiscount(100, 0.1)); // 90
Enter fullscreen mode Exit fullscreen mode

Without global state, this function is predictable and self-contained. Testing becomes straightforward, and it’s ready to be reused or extended as part of larger workflows.

By breaking tasks into small, pure functions, you create a codebase that’s both robust and enjoyable to work with. So, the next time you write a function, ask yourself: "Is this spell focused and reliable—or will it become a cursed artifact poised to unleash chaos?"


Building Bridges with Function Composition 🧩

With pure functions in hand, we’ve mastered the craft of simplicity. Like Lego bricks 🧱, they’re self-contained, but bricks alone don’t build a castle. The magic lies in combining them—the essence of function composition, where workflows solve problems while abstracting implementation details.

Let’s see how this works with a simple example: calculating the total of a shopping cart. First, we define reusable utility functions as building blocks:

type CartItem = { price: number };

const roundToTwoDecimals = (value: number) =>
  Math.round(value * 100) / 100;

const calculateTotal = (cart: CartItem[]) =>
  cart.reduce((total, item) => total + item.price, 0);

const applyDiscount = (discountRate: number) => 
  (total: number) => total * (1 - discountRate);
Enter fullscreen mode Exit fullscreen mode

Now, we compose these utility functions into a single workflow:

// Domain-specific logic derived from reusable utility functions
const applyStandardDiscount = applyDiscount(0.2);

const checkout = (cart: CartItem[]) =>
  roundToTwoDecimals(
    applyStandardDiscount(
      calculateTotal(cart)
    )
  );

const cart: CartItem[] = [
  { price: 19.99 },
  { price: 45.5 },
  { price: 3.49 },
];

console.log(checkout(cart)); // Output: 55.18
Enter fullscreen mode Exit fullscreen mode

Here, each function has a clear purpose: summing prices, applying discounts, and rounding the result. Together, they form a logical flow where the output of one feeds into the next. The domain logic is clear—calculate the checkout total with discounts.

This workflow captures the power of function composition: focusing on the what—the intent behind your code—while letting the how—the implementation details—to fade into the background.


Streamlining Code with Pipelines ✨

Function composition is powerful, but as workflows grow, deeply nested compositions can become hard to follow—like unpacking Russian dolls 🪆. Pipelines take abstraction further, offering a linear sequence of transformations that mirrors natural reasoning.

Building a Simple pipe Utility 🛠️

Many JavaScript libraries (hello, functional programming fans! 👋) offer pipeline utilities, but creating your own is surprisingly simple:

const pipe =
  (...fns: Function[]) =>
  (input: any) => fns.reduce((acc, fn) => fn(acc), input);
Enter fullscreen mode Exit fullscreen mode

This utility chains operations into a clear, progressive flow. Refactoring our previous checkout example with pipe gives us:

const checkout = pipe(
  calculateTotal,
  applyStandardDiscount,
  roundToTwoDecimals
);
Enter fullscreen mode Exit fullscreen mode

The result is almost poetic: each stage builds upon the last. This coherence isn’t just beautiful—it’s practical, making the workflow intuitive enough that even non-developers can follow and understand what’s happening.

A Perfect Partnership with TypeScript 🤝

TypeScript ensures type safety in pipelines by defining strict input-output relationships. Using function overloads, you can type a pipe utility like this:

function pipe<T1, R>(func: (arg1: R) => T2): (arg1: T1) => R;
function pipe<T1, T2, R>(
  func1: (arg1: T1) => T2,
  func2: (arg2: T2) => R
): (arg1: T1) => R;
// Extend further for more functions if needed

function pipe(...funcs: Array<(input: any) => any>) {
  return (input: any) => funcs.reduce((acc, fn) => fn(acc), input);
}
Enter fullscreen mode Exit fullscreen mode

A Glimpse Into the Future 🔮

Although creating your own utility is insightful, JavaScript’s proposed pipeline operator (|>) will make chaining transformations even simpler with native syntax.

cart 
  |> calculateTotal
  |> applyStandardDiscount
  |> roundToTwoDecimals;
Enter fullscreen mode Exit fullscreen mode

Pipelines don’t just streamline workflows—they reduce cognitive overhead, offering clarity and simplicity that resonate beyond the code.


Adapting Pipelines for Evolving Needs 💫

In software development, requirements can shift in an instant. Pipelines make adaptation effortless—whether you’re adding a new feature, reordering processes, or refining logic. Let’s explore how pipelines handle evolving needs with a few practical scenarios.

Adding Tax Calculation 🏛️

Suppose we need to include sales tax in the checkout process. Pipelines make this easy—just define the new step and slot it in at the right place:

const applyTax =
  (taxRate: number) =>
  (subtotal: number) => subtotal + subtotal * taxRate;

const applySalesTax = applyTax(0.1);

const checkout = pipe(
  calculateTotal,
  applyStandardDiscount,
  applySalesTax,
  roundToTwoDecimals
);
Enter fullscreen mode Exit fullscreen mode

If requirements change—like applying sales tax before discounts—pipelines adapt effortlessly:

const checkout = pipe(
  calculateTotal,
  applySalesTax,
  applyStandardDiscount,
  roundToTwoDecimals
);
Enter fullscreen mode Exit fullscreen mode

Adding Conditional Features: Member Discounts 🏷️

Pipelines can also handle conditional logic with ease. Imagine applying an extra discount for members. First, define a utility to conditionally apply transformations:

const identity = <T>(x: T) => x; // Returns input unchanged
const applyMemberDiscount = (isMember: boolean) =>
  isMember ? applyDiscount(0.1) : identity;
Enter fullscreen mode Exit fullscreen mode

Next, incorporate it into the pipeline dynamically:

const checkout = (isMember: boolean) =>
  pipe(
    calculateTotal,
    applySalesTax,
    applyStandardDiscount,
    applyMemberDiscount(isMember),
    roundToTwoDecimals
  );
Enter fullscreen mode Exit fullscreen mode

The identity function acts as a no-op, making it reusable for other conditional transformations. This flexibility allows pipelines to adapt seamlessly to varying conditions without adding complexity to the workflow.

Extending Pipelines for Debugging 📜

Debugging pipelines can feel tricky—like searching for a needle in a haystack—unless you equip yourself with the right tools. A simple but effective trick is to insert logging functions to illuminate each step:

const log = (message: string) => 
  <T>(value: T) => (console.log(message, value), value);

const checkout = pipe(
  calculateTotal,
  log("After calculating total:"),
  applyStandardDiscount,
  log("After applying discount:"),
  roundToTwoDecimals
);
Enter fullscreen mode Exit fullscreen mode

While pipelines and function composition offer remarkable flexibility, understanding their quirks ensures you can wield their power without falling into common traps.


Avoiding the Traps of Function Composition 🛡️

Function composition and pipelines bring clarity and elegance to your code, but like any powerful magic, they can have hidden traps. Let’s uncover them and learn how to avoid them effortlessly.

The Trap #1: Unintended Side Effects 💥

Side effects can sneak into your compositions, turning predictable workflows into chaotic ones. Modifying shared state or relying on external variables can make your code unpredictable.

let counter = 0; // Shared state lurking in the shadows 

const increment = () => counter++; // Modifies external state!
const pipeline = pipe(increment, increment); 
pipeline(); // Counter modified unpredictably
Enter fullscreen mode Exit fullscreen mode

The Fix: Ensure all functions in your pipeline are pure.

const increment = (counter: number) => counter + 1; // No side effects

const pipeline = pipe(increment, increment); 
console.log(pipeline(0)); // Output: 2, predictable 🎉
Enter fullscreen mode Exit fullscreen mode

The Trap #2: Overcomplicating Pipelines 🚧

Pipelines are great for breaking down complex workflows, but overdoing it can lead to an confusing chain that’s hard to follow.

const processUserName = pipe(
  extractFirstLetter,
  capitalizeFirstLetter,
  trimWhitespace,
  validateStringLength,
  normalizeCase,
  // ... and more
);
Enter fullscreen mode Exit fullscreen mode

The Fix: Group related steps into higher-order functions that encapsulate intent.

const normalizeUserName = pipe(trimWhitespace, capitalize); 
const validateUserName = pipe(validateStringLength, validateAllowedChars);

// Use higher-order functions in the main pipeline
const processUserName = pipe(normalizeUserName, validateUserName);
Enter fullscreen mode Exit fullscreen mode

The Trap #3: Debugging Blind Spots 🔍

When debugging a pipeline, it can be challenging to determine which step caused an issue, especially in long chains.

The Fix: Inject logging or monitoring functions to track intermediate states, as we saw earlier with the log function that prints messages and values at each step.

The Trap #4: Context Loss in Class Methods 🔑

When composing methods from a class, you may lose the context needed to execute them correctly.

class Calculator {
  base = 10;
  multiply(value: number) {
    return value * this.base;
  }
}

const calc = new Calculator();
const multiplyFn = calc.multiply;

console.log(multiplyFn(5)); // Error: Cannot read properties of undefined
Enter fullscreen mode Exit fullscreen mode

The Fix: Use .bind(this) or arrow functions to preserve context.

const multiplyFn = calc.multiply.bind(calc);
console.log(multiplyFn(5)); // Works as expected 🎉
Enter fullscreen mode Exit fullscreen mode

By being mindful of these traps and following best practices, you’ll ensure that your compositions and pipelines remain as effective as they are elegant, no matter how your requirements evolve.


A Journey Towards Elegance 🚀

Mastering function composition and pipelines isn’t just about writing better code—it’s about evolving your mindset to think beyond implementation. It’s about crafting systems that solve problems, read like a well-told story, and inspire with abstraction and intuitive design.

No Need to Reinvent the Wheel 🛞

Libraries like RxJS, Ramda, and lodash-fp offer production-ready, battle-tested utilities backed by active communities. They free you to focus on solving domain-specific problems instead of worrying about implementation details.

Takeaways to guide your practice 🗺️

  • Clean Code: Clean code isn’t just about appearances—it’s about working smarter. It creates solutions that simplify debugging, collaboration, and maintenance. Six months down the line, you’ll thank yourself for writing code that practically explains itself.
  • Function Composition: Combine pure, focused functions to craft workflows that elegantly tackle complex problems with clarity.
  • Pipelines: Abstract complexity by shaping your logic into clear, intuitive flows. Done well, pipelines boosts developer productivity and make workflows so clear that even non-developers can understand them.

Ultimately, your code is more than a series of instructions—it’s a story you’re telling, a spell you’re casting. Craft it with care, and let elegance guide your journey. 🌟✨

Top comments (0)