Functional Programming

Thoughts on functional programming and why it matters.

Published January 15, 2025 ET

Introduction

As far as I understand functional programming, the people who prefer it are really just sick people with deep skeleton-laden closets.

Why do I feel this way?

Well, why on earth would anyone prefer example A over example B.

given an object with nested results...

const response = {
  data: {
    results: [1, 2, 3]
  }
};

A)

const { path, compose } = require('ramda');
const results = compose(path('data', 'results'))(response);

B)

const results = response.data.results;

The answer, so far as I've heard, is "to avoid the rightward drift of nested functions"


I suppose I haven't given functional programming a totally fair squeeze. I tend to encounter it and judge it before really understanding it...

I am going to try to convince myself it's superior, but my hypothesis is that the folks who like it really don't necessarily like it. I think it's a clear philosophy, though, and writing code can often feel like an unguided process. To have a strong underlying philosophy in how you write code can provide guidance and ultimately greater satisfaction. Hence, functional programming...

The John Hughes paper "Why Functional Programming Matters"

tl;dr: modularity is the key to successful programming, especially for complex applications

  • Uses Miranda, a "lazy", "purely functional" programming language designed by David Turner
    • This has influenced Haskell, Orwell, and Clean

Other advantages:

  1. Fewer bugs because there's no assignment, no side effects, no flow of control

    There are no side-effects because there are no assignment statements, so once variables are given a value they never change. A function call can have no effect but to compute its result. This also eliminates the needs to worry about flow of control.

    -> I need to see some examples to understand this fully