Functional-Light Programming

Ted Wu
Code Intersect
Published in
4 min readMar 2, 2017

(Information from Kyle Simpson’s lecture at forwardJS)

Functional programming is awesome

Before attending this “Functional Light Programming” workshop, I thought I knew what functional programming was. I recognized certain functional programming patterns but I couldn’t really explain to you why it is important or useful.

Functional programming patterns have been used quite often in Javascript.

Let’s start off with the concept of functional programming.

Functional programming is a programming paradigm that focuses on composing pure functions, avoiding mutations, and also minimizing side effects

It is a declarative rather than imperative.

Declarative is focused on the the outcome (WHAT) rather than the precise set of instructions to go through a set of values (HOW).

Here is an analogy.

If an Uber driver accidentally dropped off your friend at the wrong spot and your friend called you to ask how to get to your house.

Imperative way would be to give them a set of instructions

The declarative response would be to give them an address:

420 Bush Street, San Francisco, CA 94108.

With more declarative responses, we are abstracting lots of the details because we assume that our friend has a GPS, has a sense of direction, or know the steps to get there.

So why is functional programming useful?

Readability: Functional code uses a set of patterns that other functional programmers can understand.

Predictability: Functional code tends to be more predictable since it is mathematically proven (with the same input you can expect same output)

Testability: Functional code is easier to test since if you are confident what the outcome looks like, you can easily test it.

With functional programming, you want to minimize side effects.

A side effect refers to the modification of state outside of the state’s scope— for instance:

  • Network requests
  • Writing some data to disk (I/O)
  • Generate a random number
  • Grab a current date timestamp

You can’t really avoid side effects entirely in your codebase. What you can do is to separate your code into procedures with side effects and pure functions with no side effects. This way it is easier to debug.

There are real world techniques to avoid side effects. One is to wrap impure functions with a pure function. Second is to create an adaptor function that restores the original state so that the program state doesn’t get affected.

  1. Wrapping impure function with a pure function.

f(x) is an impure function by itself because y value can be affected by side effects. However if you wrap it with F(x) which is a pure function that takes an input x and returns y without mutating the original state of y.

2. Adaptor function that restores original state (This is the brute force way)

Since f() is an impure function, you can have a pure F() function that stores original x and y values so that after a series of operations, you can restore them back to original values to maintain functional purity.

There are lots of concepts of functional programming that I can adapt to work. Since functional programming promotes value immutability, I can either do Object.freeze so that no side effects can mutate the object or use Object.assign to copy the values of the state to a target object. I could also use const to avoid reassignment of variables.

By using these methods and techniques, the readers of my code can be certain that nothing will change that object and that the values are read-only and immutable.

Javascript is not a pure functional language because it lacks native support for immutable data. Some of the techniques used in real-world codebases like making a copy of the object or freezing deep nested objects can hurt performance. However I believe that there are positive changes and support for Javascript libraries like Immutable.js to make Javascript more functional.

Let’s keep on learning and documenting new ideas, perspectives, and techniques!

--

--

Ted Wu
Code Intersect

Life-long learner. ENFP. My mission is to invest in relationships and seek harmony within my career, financial, social, physical, and community wellbeings