Introducing Functional Programming

javascript Aug 02, 2019

There are many different ways to approach software design and construction. The two most well-known design philosophies, or programming paradigms, are Object-oriented programming (OOP) and Functional Programming (FP). A programming paradigm is a way of thinking about software design and construction. Programming paradigms are based on several defining principles and are used to organize and characterize the design and construction of software applications. Functional Programming is a programming paradigm focused on building software through expressions and declarations. In this section, we will discuss the very basics of Object-Oriented Programming and Functional Programming, and compare the two programming paradigms.

Object-Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm based on objects and statements. Objects are programming abstractions used to organize pieces of an application. In OOP, objects usually contain and store data in attributes, have procedures they can run in methods, and have some notion of this or self, a way for the object to reference itself. Generally, objects come in the form of classes. A class can be thought of as the definition of an object, with its attributes, methods, and this scope. An object is the instantiation of a class. In OOP, statements are instructiondriven code. This will be covered more in the Declarative Versus Imperative topic. Many programming languages work well for OOP software development. The most popular OOP languages are C++, Java, and Python.

Functional Programming

Functional Programming (FP) is a programming paradigm based on expressions and declarations instead of objects and statements. In short, this means that FP relies on functions instead of objects to organize code and build applications. Functional Programming is thought to have originated from lambda calculus, which was created in the 1930s. Functional Programming relies on seven key concepts: declarative functions, pure functions, higher order functions, shared state, immutability, side effects, and function composition. Each of these concepts will be covered in a subsequent topic in this chapter.

Functional Programming is designed to be more concise, predictable, and testable. These benefits, however, can result in FP code being denser than other coding paradigms. Some of the most common Functional Programming languages are JavaScript, PHP, and Python.

Declarative Versus Imperative

There are two general ways to think about writing code: Declarative and Imperative. Code written in the Functional Programming paradigm should be declarative.

Declarative code is code that expresses the logic of a computation without describing its control flow. Imperative code is code that uses statements to change a program's state.

These definitions are difficult to understand if you have never studied declarative and imperative code before. Declarative code is generally used with Functional Programming and imperative code is generally used with Object-Oriented Programming. There is no "right answer" when deciding which coding style to use; they both have their trade-offs. However, declarative code fits the Functional Programming paradigm better than imperative.

Imperative Functions

Imperative code is most common in OOP. The technical definition is complicated, but we can simplify it. An imperative approach to coding is about HOW you solve the problem. Consider finding a table at a restaurant. You approach the host/hostess and say "I see that the table in the corner is empty. My wife and I are going to walk over and sit down." This is an imperative approach because you describe exactly how you are going go from the host/hostess to getting a table for your party.

Declarative Functions

Declarative programming is most common in FP. The declarative approach to coding can be simplified as WHAT we need to do. Consider the restaurant example from the previous paragraph. A declarative approach to getting a table would be to approach the host/hostess and say "table for two please." We describe what we need, not every step we will take to get the table. Declarative programming is the act of conforming to the mental model of the developer rather than the operational model of the machine. From these definitions and metaphors, we can conclude that declarative programming is an abstraction of some imperative implementation.

Now, let's move from the metaphor to actual code. Consider the code shown in the following snippet:

function addImperative( arr ) {
 let result = 0;
 for ( let i = 0; i < arr.length; i++ ) {
   result += arr[ i ];
 }
 return result;
}
function addDeclarative( arr ) {
 return arr.reduce( ( red, val ) => red + val, 0 );
}

In the preceding snippet, we create two functions to add the values in an array. The first function, addImperative, is an imperative approach to this problem. The code states exactly how the array will be added, step by step. The second function, addDeclarative, is a declarative approach to the same problem. The code states how the array will be added. It abstracts out much of the imperative solution (for loop) by using the JavaScript array reduce operation.

The simplest way to begin writing declarative code instead of imperative code is by creating functions. These functions should abstract away the step-by-step nature of the imperative parts of your code. Consider array operations such as find, map, and reduce. These functions are all array member functions that are declarative. They abstract away the step-by-step nature of iterating over an array. Using them will help introduce declarative concepts into your code and reduce some of the imperative code that you write.

Neeraj Dana

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Angular, React, React-Native, Vue js, Machine Learning