Sign in
Log inSign up

PARADIGM OF PROGRAMMING

Omojuwon Soneye's photo
Omojuwon Soneye
·Jan 22, 2021·

11 min read

Have you ever thought about how you code? How do you write code ? What style do you use

Paradigm of programming is not a tool or programming language . It simply refers to your style of programming. We all write code in a distinctive style , which affects our code readability or scalability. This determines your code is a good code or a bad code

MAJOR PARADIGM OF PROGRAMMING

  • Procedurral programming -Functional programming -Object oriented programming

PROCEDURAL PROGRAMMING

This is the style of coding i started with and also most of you started with. Writing code in a manner or style thats does not give any account to logic .It is simply put as a step by step kind of programming. The code runs like a single thread, it however is not the best way to write code

With this style we have variable and functions scattered all over the program , which are interdependent. Modifications in one function might lead to breaking the whole code base 😱😱😱. This is called SPAGHETTI CODE

FUNCTIONAL PROGRAMMING

This style of writing code , treats code as First class , with a clear defined task. Functions are structured as PURE FUNCTIONS - what are PURE FUNCTIONS ?

PURE FUNCTIONS Any function that has the following features is a pure function

Referential transparency: The function always gives the same return value for the same arguments. This means that the function cannot depend on any mutable state.

Side-effect free: The function cannot cause any side effects. Side effects may include I/O (e.g., writing to the console or a log file), modifying a mutable object, reassigning a variable, etc.

A lot of theories , no code . Let take a look at the following codes to get a clearer picture of Functional programming

This function below is a pure function . why ? A pure function is referentially transparent . That is the value kept in this function gives a predictable result . It is not susceptible to change , provided we use the same values .

const multiply = (a , b )=>{
    return a * b
}
multiply(2 ,3)
\\ expect result is 6

The function multiply below is impure , because the value of the a and be , is susceptible to change , due to Maths.Random . We can not predict the change in it

let a = Math.floor(Math.random() * 4);

 let b =Math.floor(Math.random() * 10);

const multiply = (a, b) => {
    return a * b;
}
multiply(a, b);

A function that has side effect is an impure function - function that is log into the console .

const greeting = ()=>{
console.log("hey");
};
greeting();

OBJECT ORIENTED PROGRAMMING This is not a tool rather a style of coding , that is focused on objects rather than functions

Functions stores methods and properties in a unit like an object would . You might be confused, how can a function be an object .

In JavaScript object can act as an object , that had properties and method . The major difference between a function and an object is that a function can be called .

PILLARS OF OOP

ENCAPSULATION in procedural programming , we store the variable and function in seperate entities . This makes the code difficult to read as we have a lot of entries in the function However in OOP, we store related variable and logic in a function(unit )

// Procedural way 
let name = "juw";
let age = 10;
function servant(name , age ) {
    return name + " " + age
}
// using encapsulation 

let servant = {
    name : "juw",
    age : 10,
    add: ()=>{
        return this.name  + " " + this.age 
    }
}

servant.add()

ABSTRACTION This type of method in OOP , doesn’t really care about the function or variable that makes the code work . it just hide certain properties , and makes them Inaccessible outside the scope of the function . The major concern is that the codes run when you keep the input in the code and it gives out the output

The num variable is not accessible outside the scope of this loop . When using this function , the client doesn’t really care about the mathematical computation going on in the function . We just want to put the age in the function and get the weight . There really is no need giving access to the mathematical computation outside the function


function weight  (age){
Let num = age * 3
Return num
}
weight (age)(

Without using the key word let , it is possible to get access to the num variable outside the function


function weight  (age){
num = age * 3
Return num
}
weight (age);

POLYMORPHISM

This is an important aspect of Object oriented programming that modifies method or properties in a parent object by the child object


class Animal   {
 constructor(name)
 {
    this.name = name
 }

 makeSound(){
     console.log("woof woof");
 }
};

class Dog  extends Animal{
    constructor(name){
        super(name)
        this.name = name
    };

    makeSound(){
        console.log("bark");
    };
};
// we have extended or linked the properties in animal to dog

const brandy = new Animal;
const rex = new Dog;
rex.makeSound();
// rex.makeSound will give "bark" . it overides the parent function , that was extended in Dog

if the make sound in rex is commented out . rex.makeSound will log in the console "woof woof" from the previous class . TRY IT

INHERITANCE This is a concept in programing that helps cut out redundacy in code by creating an OBJECT , Properties or method can be inherited by objects.

This aids easy modifications of a behavioral method/function

There are two types of INHERITANCE

-Classical Inheritance -Prototypical inheritance

CLASSICAL INHERITANCE

This is a type of inheritance that uses CLASSES , to create a blueprint for which other object are created from . JavaScript is is a not a Class base language it is a prototypical based language , uses prototype . It other can be used as a classes based language , not the best practice


class Employee {
   constructor(name, age) {
      this.name = name;
      this.age = age;
   }

   // Prototype
   getDetails(){
      return `${this.name} :: ${this.age}`  
   }
}

let james = new Employee( "juwon", 12);



class Relationship extends Employee {
    constructor( name , age , Relationship)
     {
        super(name, age);
       this.Relationship = Relationship;
    }
    getAll(){
       return `${this.name} :: ${this.age} :: ${this.Relationship}`;
    }
 }

 let juwon = new Relationship( "juwon", 21 , "single");

PROTOTYPICAL INHERITANCE

A Prototype is created to solve this , what is a prototype ? Prototypes are the mechanism by which JavaScript objects inherit features from one another.


let Employee = function (name, age){
   this.name = name;
   this.age = age

}
// prototype
Employee.prototype.draw = ()=>{
   return `${this.name} :: ${this.age}`
}

let Relationship =  function(name ,age , salary){
   Employee.call(this, name , age)

   this.salary = salary
}
// extend prototype
Object.setPrototypeOf(Relationship, Employee.prototype);
Relationship.prototype.detail = function (){
   return `${this.name} :: ${this.age} :: ${this.salary}`
}
let j = new Relationship("ju", 1,8)
console.log(j);

Situation where in your functions , they have two protoypes that are behavioral . Rewriting this prototype in both objects makes your code redundant . Best practice is to create a function with that protoype , for which can be inherited . Lets code , so it all makes sense .


// the code below is redudant as both Square and Circle have the same prototype

const Circle = (name) => {
    this.name = name;

}
Circle.prototype.color = () => {
    console.log("blue");
}
const Square = (name) => {
    this.name = name;

}
Square.prototype.color = () => {
    console.log("blue");
}




// This is a cleaner code , with a good use of Prototypical inheritance
const Color = (name)=>{
    this.name = name
}

const Circle = (name) => {
    this.name = name;
            Color.call(this , name  )

}
Circle.prototype - Object.create(Color.prototype)
const Square = (name) => {
    this.name = name;
        Color.call(this , name  )
}
Square.prototype = Object.create(Color.prototype)
// THis is a better code , it enables easy modification of the color . If we change our mind and we want color indingo
Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct