Functional (Programming) mindset

LeoLanese
14.8K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

"OOP vs FP: Don't be an FP programmer, don't be an OOP programmer... BE A BETTER PROGRAMMER." ~ @fernando_cejas


To start with this workshops you need to have an undertanding of what Functional Programming is. You can check this previous workshop: https://tech.io/playgrounds/0ccbd2817eab67cc4e41211af5c23d1520042/becoming-functional/welcome-


To become Functional we need to start Thinking Functionally. Lets go throw some steps to get the right mindset into Functional Programming mindset to become into functional.


Use ES6 Arrow Functions (fat arrow) as much as posible

Why:

  • Arrow functions create a concise expression that "encapsulates" a small piece of functionality.
  • Additionally, arrows retain the scope of the caller inside the function eliminating the need of self = this. Remember: Minimize moving parts

Further Information: ES6 Arrow functions create a concise expression that encapsulates a small piece of functionality. Additionally, arrows retain the scope of the caller inside the function eliminating the need of self = this.


Use Function Delegation

Why: Function delegates encapsulate a method allowing functions to be composed or passed as data.

https://stackblitz.com/edit/function-delegation


Separate the pure from the impure

If a function is impure, if posible, split it and simple as creating two functions


Don't change objects in functions


Better ifs or no ifs techniques

Expressions instead of Statements

https://stackblitz.com/edit/use-expressions-instead-statements?file=index.js

Better ifs, no ifs: Avoid Nested ifs/complex || conditions

Better ifs, no ifs: Use functions

https://stackblitz.com/edit/use-functions-no-ifs?file=index.js


Avoid loops and iterations

A loop is an imperative control structure that is hard to reuse and difficult to plug in to other operations. We can use: Recursion,.map(), .reduce(), .filter(), etc

Why: - make the code clean - make the logic reusable - minimizing moving parts

Tip: Think about results over steps. Next time you are about to iterate something, stop, and think about: "How this can look if I don't iterate this?" Loops & Iteration


Use array manipulation functions & Avoid mutator methods

DO NOT use the mutator methods, these methods modify the array: .push(), .copyWith(), .fill(), .pop(), .reverse(), .shift(), .sort(), .splice(), .unshift()

Better USE non-mutating methods (Accessor methods, Iteration methods ): .concat(), .join(), .slice(), .toString(), .reduce(), .reduceRight(), etc.

Full list:

Mutator_method


Use Higher-order-Function (HoF) & Spread Operator when possible


Use Method Chaining

Method chains allow a series of functions to operate in succession to reach a final result. Method chains allow function composition similar to a pipeline.


Use pipelines

A pipeline allows for easy function composition when performing multiple operations on a variable. Since JavaScript lacks a Pipeline operator, a design pattern can be used to accomplish the task.


Dependency injection

Dependency injection works by moving the impure parts of the code out of the function. So you have to pass them in as parameters.


sum all values of the Array

Further information: Array.prototype.reduce()


Get cats younger than 7 months

Further Information: Array.prototype.filter()


Lazy functions

Controlling the side-effect: A side effect isn’t a side effect until it actually happens.


Variable Assignment

function doubleAndAddTen(x) {
    const doubled = x * 2;
    return doubled + 10;
}

Sequenced Side Effects

// Impure
console.log('One');
console.log('Two');
function pureLog(msg) {
    return () => console.log(msg);
}

const sideEffectSequence = (firstEffect, secondEffect) =>
    () => (ignoredReturnValue => secondEffect())(firstEffect());

const sequencedSideEffect = sideEffectSequence(pureLog('One'), pureLog('Two'));

sequencedSideEffect(); // One, Two

Avoid impure methods

Date (Date.now), Math.random (since it always produces a new value no matter what the inputs are), console.log(), this, global variables, exceptions thrown, etc. In fact, because JavaScript passes object references around, every function that takes an object or array is potentially subject to impurity.


Bend your language to the problem, not the problem to the language

The most obvios tip is: Please, take advantage of all the language features, if you have something that it is already done, well: use it. Do not spend time working on languague problem, focus on bussiness goal issues.


Open Source Your Knowledge: become a Contributor and help others learn. Create New Content