Address
Whiteland, IN 46184
Work Hours
Monday to Friday: 9AM - 5PM
Weekend: 1PM - 3PM
For a long time while coding I heard about programming paradigms but I really did not pay attention to it until fairly recently when I started to realise that programming paradigms and patterns are important in writing structured and maintainable code. The most popular paradigms include; Imperative, Functional, Logical and Object-Oriented.
Functional programming is a programming paradigm in computer science that is declarative and unlike object-oriented programming, it does not rely on object mutation and side effects. Functional programming instead relies on pure functions and immutable objects and variables that accepts input in the form of parameters and returns the same output every time as far as the input is the same. Take the below example of a pure function in JavaScript.
function foo(param1, param2) {
return param1 * param2
}
foo(2, 4);
The above function will evaluate to 2 * 4 which will always return 8 as the output as far as the inputs are 2 and 4.
Note that all variable declarations in functional programming in javascript are done with the const keyword instead of the easily mutable let and var keywords. For example;
const name = “Tony” . The case is slightly different for objects as they still remain somewhat mutable even if declared with the const keyword. Se an example below;
const myObj = {
name: “Tony”
nationality: “Nigerian”
}
myObj.name // outputs Tony.
Even though the above cannot be reassigned a new value by going myObj = “Tony”, we can still mutate the object like this.
myObj.name = “John”.
myObj.name // now outputs John.
The above is not acceptable from a purely functional programming perspective as all objects and variables should be immutable. This brings us to the useful Object.freeze(myObj) javascript method. This method freezes an object so nothing can be added or removed from the properties set of the object. All object declarations in functional programming in Javascript must use this method.
The major benefits of functional programming are that it reduces the risks of introducing bugs in your code due to unexpected values from mutable objects or variables. This means that programs written in a functional programming style are much easier to debug and debugging is the major time consumer in programming.
Functional programming by nature results in a clean and straightforward source code that is easier and cleaner to maintain and extend than constructing classes in object-oriented programming since you are not thinking about designing class hierarchies, encapsulation, inheritance, polymorphism etc. Each function is focused only on its block and what is going on inside it.
Conclusion:
Functional programming is gaining popularity in the modern javascript community especially amongst react developers as it comes highly recommended. Even though functional programming may not be suitable for your kind of application, its benefits and importance are something that cannot be overlooked.