- The process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects.
- Declarative rather than imperative, and application state flows through pure functions.
- A programming paradigm, meaning that it is a way of thinking about software construction based on some fundamental, defining principles.
In Contrast with object oriented programming, where application state is usually shared and colocated with methods in objects.
Other examples of programming paradigms include object oriented programming and procedural programming.
We can say that in functional programming, functional code tends to be more concise, more predictable, and easier to test than imperative or object oriented code.
After that, Second question is in our mind that :
To understand the meaning and importance of the functional programming, we need to understand the below core concepts:
Avoid shared state
Avoid mutating state
Avoid side effects
Reusability through Higher Order Functions
Declarative vs Imperative
A pure function is a function which :
Given the same inputs, always returns the same output, and
Has no side-effects
2. Functional Composition :
3. Avoid Shared State :
Shared state is any variable, object, or memory space that exists in a shared scope, or as the property of an object being passed between scopes. A shared scope can include global scope or closure scopes.
- The first problem with shared state is that in order to understand the effects of a function, you have to know the entire history of every shared variable that the function uses or affects.
- Another common problem associated with shared state is that changing the order in which functions are called can cause a cascade of failures because functions which act on shared state are timing dependent.
So here question comes in our mind, Why we talk about immutability or an immutable object ? but here topic heading is avoid mutating state.
5. Avoid side effects :
- Modifying any external variable or object property (e.g., a global variable, or a variable in the parent function scope chain)
- Logging to the console
- Writing to the screen
- Writing to a file
- Writing to the network
- Triggering any external process
- Calling any other functions with side-effects
6. Reusability through Higher Order Functions :
Abstract or isolate actions, effects, or async flow control using callback functions, promises, monads, etc…
Create utilities which can act on a wide variety of data types
Partially apply a function to its arguments or create a curried function for the purpose of reuse or function composition
Take a list of functions and return some composition of those input functions
7. Declarative vs Imperative:
Functional programming is a declarative paradigm, meaning that the program logic is expressed without explicitly describing the flow control.
Imperative programs spend lines of code describing the specific steps used to achieve the desired results — the flow control: How to do things. Where as Declarative programs abstract the flow control process, and instead spend lines of code describing the data flow: What to do. The how gets abstracted away.
Imperative code frequently utilizes statements. A statement is a piece of code which performs some action. Examples of commonly used statements include for, if, switch, throw, etc… where as Declarative code relies more on expressions. An expression is a piece of code which evaluates to some value. Expressions are usually some combination of function calls, values, and operators which are evaluated to produce the resulting value.
Pure functions instead of shared state & side effects
Immutability over mutable data
Function composition over imperative flow control
Lots of generic, reusable utilities that use higher order functions to act on many data types instead of methods that only operate on their colocated data
Declarative rather than imperative code (what to do, rather than how to do it)
Expressions over statements
Containers & higher order functions over ad-hoc polymorphism