# Functional Programming

Functional programming is a programming paradigm which follows style of building the structure and element of software program. In this paradigm, functions are the basic building blocks and every function must follow few rules in order to make the software more readable, maintainable, less bugs and efficiency. In order to explain this Paradigm, I use one of the most popular and stable programming language- JavaScript.

### Do everything within a function

Not like object oriented or imperative paradigms, everything should be within a function.

```function foo(){

}
```

### Function should be a Pure functions

Each function should follow below rules in order to be a pure function.

• Function returns the same result if arguments are given the same.

Imagine, we want to implement a function that calculates Tax base on given annual salary.

```var taxRate = '0.10'; //10%

function calculateTax(annualSalary){
return annualSalary * taxRate;
}
```

Future if the government increased the tax rate, then we have to change the function in order to calculate the same tax rate for previous years. That mean this function does not return same result if arguments are given in same. So to make it pure function, a ya rate should be passed as parameter.

```var taxRate = '0.10'; //10%

function calculateTax(annualSalary, rate){
return annualSalary * rate;
}
```
• It does not cause any observable side effects.

Observable side effects include modifying a global object or a parameter passed by reference.

```let counter = 1;

function increaseCounter(value) {
counter = value + 1;
}
```

In this Inpure function changes the global variable increased by 1. In order to be a pure function, code should be as below.

```let counter = 1;

const increaseCounter = (value) => value + 1;
```

### Use Higher Order Function

In order to be a Higher Order function, following statement should be satisfy.

• Function can be passed as a parameter.
• A Function can return another function.

### 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. Often, in object oriented programming, objects are shared between scopes by adding properties to other objects.

### Avoid mutating state

Mutating mean, any object can be modified after it’s created. So in Functional programing, we have to avoid such thing. That means, `"An immutable object is an object that can’t be modified after it’s created."`

Don’t confuse with `const` key work, because const create variable name binding that can’t be changed once it’s created but property value still can be changed.

`Object.freeze` gives one-level deep readonly in javascript

```const a = Object.freeze({
foo: 'hi',
bar: 'there',
});```

When working with array, making a copy of the array every time for value change, does not make sense for this rule. However, we can thing about data structure in order to reuse old value with new values.

### Don’t Iterate

Don’t use `for, white` loop to iterate items over and over again, instead of that use `map, reducer, filter` function which take as a function parameter.

### Summaries

Functional programming is one of best programming paradigm from small scale to large scale – Enterprise level application, because we can think of small logic wrap into a function so that no dependency with other function, easily unittestable, readable and reusable. However, History of functional programming ran from 1930 lambda calculus(introduced by mathematician Alonzo Church), was a mathematical logic written as a function for computational. Until 21st century, Angularjs and JQuery were invented, Developers were not much aware about functional programming and Object Orianted principle dominated the industry.