Coder Social home page Coder Social logo

enough-js-fp-to-be-dangerous's Introduction

Enough Functional JS to be dangerous

1. History Lesson

  • Brendon Eich created JS
  • He wanted to build a Scheme like language for the Netscape browser
  • Scheme is a functional programming language
  • Netscape wanted to attract more developers with a C/C++ style syntax over a Scheme/Lisp syntax
  • JS was created in 10 days

2. Functional Programming Philosophies

2.1 Immutability

Once data is created, no other function or process can mutate it. A new copy is returned.

NON FP STYLE

const fruits = ['apple', 'kiwi']

function addWatermelon(fruitList) {
  fruitList.push('watermelon')
}

function someOtherFn(twoFruitArray){}

// input not modified directly :(
addWatermelon(fruits)
someOtherFn(fruits) // this guy gets 3 fruits but maybe expected 2 ?

FP Style

const fruits = ['apple', 'kiwi']

function addWatermelon(fruitList) {
  return [...fruitList, 'watermelon']
}

function someOtherFn(twoFruitArray){}

// input not modified
const withWaterMelon = addWatermelon(fruits)
someOtherFn(fruits) // this guy gets 2 fruits as it wants

2.2 Functions as first class citizens

Like variables, one should be able to create functions in any code path. Functions can be passed as parameters and returned from functions as results.

2.3 Pure functions

Functions should be written in such a way that, if same input is provided to a function, same output is returned by the function.

Impure function example

// The result of this function is completely dependent on environment variables.
function encryptString(strToEncrypt) {
    const salt = String(process.env.ENCRYPTION_SALT)
    const iterations = parseInt(process.env.HASH_ITERATIONS)
    return symmetricHash(strToEncrypt, salt, iterations)
}
// There is no guarantee that this function will return same output with same input
const encrypted = encryptString('JavaScript')

Pure function example

// All non-deterministic side effects are isolated
function getSaltAndIterationCount() {
    return {
        salt: String(process.env.ENCRYPTION_SALT),
        iterations: pareInt(process.env.HASH_ITERATIONS)
    }
}
// pure function
function encryptString(str, salt, iterations) {
    return symmetricHash(str, salt, iterations)
}

const { salt, iterations } = getSaltAndIterationCount()
const encrypted = encryptString('JavaScript', salt, iterations)

Note

  1. Its impossible to create a product that only has pure functions
  2. FP wants you to isolate side-effects instead of littering in all over the app. Eg: Redux contains all side effects like http request in thunk or saga modules/middleware code

2.4 Strong Type System

  • Most functional languages have a strong type system
  • Javascript cannot do this without TypeScript/Flow/ReasonML
  • We need to resort to schema definitions using Yup or Joi
  • In JS land, type discipline is a must for a programmer

2.5 Focus on decomposing big problems into smaller problems

Simplistic Eg: Average acceleration is the change in velocity over time

NON FP

// This function performs subtraction AND division
const calcAvgAcceleration = (v0, v1, time) => (v1-v0)/time

FP Way

 const subtract = (val1 = 0, val2 = 0) => val2 - val1
 const divide = (divisor = 0, dividend = 0) => {
    if (dividend === 0) {
      return { error:  'Divide by 0 error' }
    }
    if (divisor) {
      return { result: 0 }
    }
    return { result: dividend/divisor }

}
const { result, error } = (v0,v1, time) => divide(time, subtract(v0, v1))

if (error) {
  throw new Error(error)
}
const averageAcceleration = result

2.6 Exceptions and Errors

  • Exceptions are usually things a system needs to recover from or stop. Eg: Memory issue, DB unreachable

  • Errors are simply the undesirable output of a function. A lot of modern programming languages have embraced this idea.

    • Rust has Result type
    • Swift has Result type
    • Elixir forces all functions to return primitives or a typed tuple of :ok or :err
  • Centralize elevation of Error to Exception in 1 place instead of everywhere.

2.7 Reduce the number of Falsy values in code, prefer providing sane defaults

3. Code

4. Final thoughts.

  1. It is not possible to write 100% Pure functional code in JS. We can surely write JS in a functional STYLE.
  2. Functional programming is more of a thinking style than a programming style.
  3. The constraints of FP, helps us write more declarative and safe code

5. Resources

enough-js-fp-to-be-dangerous's People

Contributors

rahulballal avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.