The Fun of Functional

by Rob Zoszak
11 May 20223 min read

If you do a search for functional programming you'll get a lot of similar answers describing it. Common themes include pure functions, recursion, referential transparency, and immutability. It's all great! But in reality I find it difficult to follow functional practices strictly in JavaScript. Sometimes we're just doing functional for the sake of functional.

It's a tough nut to crack, but I think it's worth trying. Sometimes you just have to break the rules and that's why I like to think of my approach to programming as a functional style.

Please note that I did not come up with everything you see in this series. Feel free to bend my ideas into your own. Diversity and innovation are our friends.

And we're off!

Let's start with a few commandments. These are JavaScript programming features which we'll drop from our vocabulary!

Commandment 1

Avoid the

function
keyword and avoid the
return
keyword.

  • We don't use
    this
  • We write less code

Before:

function add5(n) {
  return n + 5;
}

After:

const add5 = (n) => n + 5;

Commandment 2

Don't use

for
/
while
loops and avoid the
let
keyword at all costs.

  • We avoid mutation
  • We write less code

Before:

const sum = (arr) => {
  let result = 0;
  for(let i = 0; i < arr.length; i++) {
    result = result + arr[i];
  }
  return result;
}

After:

const sum = (arr) => arr.reduce((a, c) => a + c, 0);

Commandment 3

Don't use

if
statements, and avoid
switch
statements.

  • We write less code

Before:

const maybeDoSomething = (v) => {
  if (v) return doSomething(v);
  else return null;
}

After:

const maybeDoSomething = (v) => v ? doSomething(v) : null;

Commandment 4

Don't use

async
/
await

  • We use promise chains
  • We write self documenting code
  • We write less code

Before:

const rng = async () => {
  try {
    const random = await api.getRandom();
    return { random };
  } catch(e) {
    return null;
  }
}

After:

const onError = () => null;
const onRandom = (random) => ({ random });
const rng = () =>
  api.getRandom()
    .then(onRandom)
    .catch(onError)

Whoa! Shots fired!

Most developers might turn their back at this point. But you're still reading! Perhaps you're as crazy as I am? Or maybe you disagree with my approach and you're looking for more reasons to have me assassinated?

In any case it's great to have you here. Stay with me, there's a point to all this!

Now that we've established the rules of engagement we can talk about our goals:

  • Write code that is easy to test
  • Write code that is easy to reason about
  • Write code that is self documenting
  • Most important - WRITE LESS CODE!

I highlight that last point because, along with all our dropped features, we're 90% of the way there. The last 10% will take most of the explaining...

In the next post we'll start implementing an API for a fictional todo application. Hope to you see you there!

Sign up and stay in the loop!

Your email address

No spam and no sharing of your details. Just useful thoughts and ideas in your inbox. :)