Most of what I will discuss in this article is knowledge accumulated from reading, “ ”, by . Let’s dig right in… Functional Programming in JavaScript Luis Atencio What is functional programming? In simple terms, functional programming is a software development style that places a major emphasis on the use of functions. You might say, “Well, I already use functions daily, what’s the difference?” Well, it’s not a matter of just applying functions to come up with a result. The goal, rather, is to on data with functions in order to and in your application. abstract control flows and operations avoid side effects reduce mutation of state Abstract Control Flows To understand what means, we need to first look at a model of programming called or . Imperative programming treats a computer program as merely a sequence of top-to-bottom statements that change the state of the system in order to compute a result. abstract control flows Imperative Procedural Programming Imperative programming tells the computer, in great detail, how to perform a certain task (looping through and applying a given formula to each item in an array for instance). This is the most common way of writing code. Here is an example. array = [ , , , , , , , , , ]; ( i = ; i < array.length; i++) { array[i]= .pow(array[i], ); } array; var 0 1 2 3 4 5 6 7 8 9 for let 0 Math 2 //-> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] Functional programming, on the other hand, falls under the umbrella of : it’s a type of programming that expresses a set of operations without revealing how they’re implemented or how data flows through them. Declarative Programming Declarative programming separates program description from evaluation. It focuses on the use of expressions to describe what the logic of a program is without necessarily specifying its or control flow state changes. You only need to be concerned with applying the right behavior at each element and give-up control of looping to other parts of the system. For example, letting do most of the heavy lifting in the task we did earlier, would be considered a more functional approach. Array.map() [ , , , , , , , , , ].map( .pow(num, )); 0 1 2 3 4 5 6 7 8 9 => num Math 2 //-> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] With this approach, we abstract our loops with functions, and compared to the previous example, you see that this code frees you from the responsibility of properly managing a loop counter and array index access. Put simply, the more code you have, the more places there are for bugs to occur. Also, standard loops aren’t reusable artifacts unless they’re with functions. abstracted Ideally, manual loops should be removed completely from your code in favor of , functions like , and , which accept functions as parameters so that your code is more reusable, extensible, and declarative. first-class higher-order map reduce filter Avoid Side Effects Functional programming is based on the premise that you use (functions with no side-effects) as building blocks when building your programs. Pure functions have the following characteristics: Pure Functions They depend only on the input provided and not on any hidden or external input.They don’t inflict changes beyond their scope, such as modifying a global object. Intuitively, any function that doesn’t meet these requirements is “impure.” Consider the following function: counter = ; { ++counter; } var 0 ( ) function increment return This function is impure because it , counter, which isn’t local to the function’s scope. Generally, , as shown in the example above. Its result is unpredictable because the variable can change at any time between calls. reads/modifies an external variable functions have side effects when reading from or writing to external resources counter One might ask, “If you’re unable to create and modify objects or print to the console, what practical value would you get from a program like this?”, Indeed, pure functions can be hard to use in a world full of dynamic behavior and mutation. But practical functional programming doesn’t restrict all changes of state; it just provides a framework to help you manage and reduce them while allowing you to separate the pure from the impure. Impure code produces externally visible side effects. Reduce Mutation of State Immutable data is data that can’t be changed after it’s been created. In JavaScript, as with many other languages, all primitive types ( String, Number, and so on) are inherently immutable. But other objects, like arrays, aren’t immutable. The of a program can be defined as a snapshot of the data stored in all of its objects at any moment in time. Sadly, JavaScript is one of the worst languages when it comes to securing an object’s state. A JavaScript object is highly dynamic, and you can modify, add, or delete its properties at any point in time. Although this may give you the liberty to do many slick things, it can also lead to code that’s extremely difficult to maintain. state To step back a bit, you might ask why its recommended you always remove manual loops from your code? Well, a manual loop is an imperative control structure that’s hard to reuse and difficult to plug into other operations. In addition, it implies code that’s constantly changing or mutating in response to new iterations. functional programs aim for statelessness and immutability as much as possible. Stateless code has zero chance of changing or breaking global state. To achieve this, you’ll and , known as . use functions that avoid side effects changes of state pure functions ES6 uses the keyword to create . This moves the needle in the right direction because constants can’t be reassigned or re-declared. In practical functional programming, you can use const as a means to bring simple configuration data ( URL strings, database names, and so on) into your functional program. . You can prevent a variable from being reassigned, but For example, this code would be perfectly acceptable: const constant references But this doesn’t solve the problems of mutability to the level that FP requires how can you prevent an object’s internal state from changing? student = Student( , , , ); student.lastname = ; const new 'Alonzo' 'Church' '666-66-6666' 'Princeton' 'Mourning' What you need is a stricter policy for immutability of which, is a good strategy to protect against mutations. For simple object structures, a good alternative is to adopt the also referred to as the . A value object is one whose equality doesn’t depend on identity or reference, just on its value; once declared, its state may not change. Encapsulation Value Object pattern Module pattern { _code = code; _location = location || ; { : { _code; }, : { _location; }, : { parts = str.split( ); zipCode(parts[ ], parts[ ]); }, : { _code + + _location; } }; } ( ) function zipCode code, location let let '' return code ( ) function return location ( ) function return fromString ( ) function str let '-' return 0 1 toString ( ) function return '-' In JavaScript, you can to a function’s internal state by returning an object literal that exposes a small set of methods to the caller and treats its encapsulated properties as . These variables are only accessible in the object literal via , as you can see in the example above. use functions and guard access pseudo-private variables closures The returned object effectively behaves like a primitive type that has no mutating methods. Hence, the method, although not a pure function, behaves like one and is a pure string representation of this object. Value objects are lightweight and easy to work within both functional and object-oriented programming. toString In Conclusion… When thinking about your application’s design, ask yourself the following questions: Do I constantly change my code to support additional functionality? If I change one file or function, will another be affected? Is there a lot of repetition in my code? Is my code unstructured or hard to follow? If you answer yes to any of these questions, then, by all means, make functional programming your friend. Just remember, In functional programming, functions are the basic units of work, which means everything centers around them. A function is any callable expression that can be evaluated by applying the () operator to it. Functions can return either a computed value or undefined (void function) back to the caller. Because FP works a lot like math, functions are meaningful only when they produce a usable result (not null or undefined ) otherwise, the assumption is that they modify external data and cause side effects to occur. In order to benefit from functional programming, you must learn to think functionally and as a result, develop your — the instinct of looking at problems as a combination of simple functions that together provide a complete solution. functional awareness At a high level, functional programming is effectively the interplay between and . It’s this duality that makes functional programs modular and so effective. decomposition (breaking programs into small pieces) composition (joining the pieces back together) Previously published at https://medium.com/@phillaipmusiime/lets-talk-functional-programming-bc12d66d604d