Programming Paradigms
The term "Programming Paradigms" refers to several styles or approaches that we take while writing codes. They also refer to different ways in which we can structure our code in order to make our programs more effective, readable, and maintainable.
Programming paradigms are crucial because each one offers a variety of problem-solving techniques, allowing you to select the best strategy for a particular task.
It is also important to note that no matter which approach or programming paradigm we take while writing our codes, the results will still be the same.
Two of the most commonly used programming paradigm styles are functional programming (FP) and object-oriented programming (OOP).
Functional programming, as the name implies, relies heavily on the use of functions, and one important feature is that data can exist independently or outside of functions.
For example, we want to write a function that takes two numbers, multiplies them, and gives us a result.
Using Functional programming, our approach to writing this code will be to
List out our data
Use them in our function
var num1 = 20;
var num2 = 30;
function multiplyTwoNums(num1, num2){
return num1 * num2;
}
console.log(multiplyTwoNums(20, 30));
// output = 600
From the example above, we can see that data and functionality are kept apart while writing FP code, and data is only sent into functions when computation is desired.
In object-oriented programming (OOP), the approach used differs from functional programming (FP) in that it groups data and functions into meaningful objects instead of keeping them apart.
Using the same example above, if we are to write a function that takes two numbers, multiplies them, and gives us a result,.
Using object-oriented programming, our approach to writing this code will be to
Create an object
Pass the two numbers as properties
Use them inside the function
var multiply = {
num1 : 20,
num2 : 30,
calculation : function(){
return multiply.num1 * multiply.num2;
}
}
console.log(multiply.calculation());
// output = 600
Subscribe to my newsletter
Read articles from Chinonye Precious directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by