Skip to main content

Programming Paradigm – Definition, Meaning & Examples

A programming paradigm is the coding pattern you prefer to write your program.

A programming paradigm is not a language, syntax, or code. It is the communication style you use to program the computer.

What Are the Common Types of Programming Paradigms?

There are several ways you can write your program. The three common styles developers use are:

  • Procedural programming
  • Functional programming
  • Object-oriented programming

Let's discuss the three.

What Is a Procedural Programming Paradigm?

Procedural programming (PP) is an imperative way to instruct the computer on the steps (procedures) it should follow to accomplish a given task.

tip

An imperative leader instructs you on what to do and how to do it. You have no choice but to follow the laid down tenets.

Example of an imperative statement: Create a beautiful portfolio app by doing the following:

  1. Get your laptop.
  2. Open VSCode.
  3. Create an index.js file.
  4. Add an <h1> element.

What Is an Object-Oriented Programming Paradigm?

Object-oriented programming (OOP) is an imperative way to instruct the computer to create new objects. These objects can contain data (properties or attributes) and procedures (functions).

An object's data and procedures represent information about the object and the functionalities (behaviors) you want it to have.

Note: JavaScript objects are like real-world objects. For instance, a laptop is an object that has properties such as brand, screen, color, and operating system.

You can replicate a laptop object in JavaScript as follows:

// Create a factory function for creating new laptop objects:
function createLaptopObject(brd, scr, col, op) {
return {
brand: brd,
screen: scr,
color: col,
operatingSystem: op,
specialFeature: function () {
return `My ${this.brand} ${this.screen} inches laptop has a numeric keypad.`;
},
};
}

// Create a laptop object:
const mySpecialLaptop = createLaptopObject("Apple", 14, "Gray", "Mac");

Try Editing It

The snippet above used a JavaScript factory function to instruct the computer to create a new laptop object.

And that is OOP (object-oriented programming)! It is a programming paradigm based on using objects to develop your application.

What Is a Functional Programming Paradigm?

Functional programming (FP) is a declarative way to state the functions (tasks) the computer should accomplish.

tip

A declarative leader tells you what to do but not how to do it. It is entirely up to you to decide how to accomplish the given task.

Example of a declarative statement: Create a beautiful portfolio app.

Functional programming hinges on using pure functions to write your program. This programming paradigm gives you the following benefits:

  • It allows you to pass functions around freely. In other words, you can use functions conveniently as arguments or return values.
  • It helps to minimize bugs because you are sure your function is not mutating any external state.

Below are some common functional programming terminologies developers use:

So, now that you know the procedural, object-oriented, and functional programming paradigms, let's discuss how to use them.

How to Use Programming Paradigm in Software Development

Suppose you are about to start a chess game against the computer, and you need to decide who should make the first move. People typically flip a coin to make such a decision. Alternatively, you can use a rock-paper-scissors contest. Let's discuss how.

What is the problem?

The issue to settle: Who gets the white chess pieces required to make the first game move?

Method to settle the issue: Play 5 rounds of rock-paper-scissors game.

Contestants: Player vs. Computer

What you need to know to resolve the problem

Below is the brief on resolving the issue of who should make the first chess move.

  • Contestants should choose one of the three items: rock, paper, or scissors.
  • The following principles should help determine each round's winner:
    • Suppose the computer plays rock, and the player chooses scissors. In that case, the computer wins, and vice versa. (Rock crushes scissors.)
    • Suppose the computer plays rock, and the player chooses paper. In that case, the player wins, and vice versa. (Paper covers rock.)
    • If the computer plays scissors and the player selects paper, the computer wins, and vice versa. (Scissors cuts paper.)
    • If both contestants choose the same object, then it's a tie.
  • After the fifth round, the winner should be declared, congratulated, and given the white chess pieces.

What paradigm is the information for solving the chess game issue?

How did you interpret the brief to decide who should start the chess game?

  • Is the information a step-by-step procedure that the contestants must follow accordingly?
  • Does the brief require you to create objects to resolve the chess game issue?
  • Is the brief a list of tasks (functions) you can accomplish as you dim it fit?

You would be correct in whatever way you've interpreted the brief. You can use any paradigm (procedural, object-oriented, or functional) to implement the guidelines on resolving who starts the chess game.

The same principle applies in programming. Programmers can choose the paradigm they wish to use to solve any programming problem.

  • Do you prefer to solve problems procedurally? You can. It is a perfect way to resolve issues.
  • Do you prefer to think in an object-oriented way? You absolutely can. It is a lovely technique to use.
  • Do you fancy using functional tactics to accomplish your goals? Fantastic! Go for it. It is an excellent path to success.

Solving problems is the goal of programming! Paradigms are the means (tactics) you prefer to use to solve those problems.

Let's discuss using the three paradigms to resolve the chess dilemma.

What is the procedural approach to deciding who starts the chess game?

The issue to settle: Who gets the white chess pieces required to make the first game move?

Method to settle the issue: Play 5 rounds of rock-paper-scissors game.

Contestants: Player vs. Computer

How to pseudocode a rock-paper-scissors game using the procedural paradigm

Step 1: Create a place to record each round's result.

Step 2: Start the contest.

Step 3: The player and the computer select an item.

Step 4: Compare the contestants' selected items to determine the round's winner.

Step 5: Assign a point to each round's winner.

Step 6: Declare the current round's result. Afterward, repeat steps 3 to 6 four more times.

Step 7: Show the total scoreboard.

Step 8: Declare the overall winner after the 5th round.

Step 9: Congratulate the overall winner and give them the white chess pieces to start the chess game.

Let's now convert the above pseudocode into a JavaScript program.

How to program a rock-paper-scissors game in JavaScript using the procedural paradigm

// Step 1: Create memories for the results:
let currentRoundResult = "";
let playerScore = 0;
let computerScore = 0;

// Step 2: Start the contest:
startContest();

function startContest() {
selectAnItem();
showTotalScoreBoard();
declareOverallWinner();
}

// Step 3: Player and computer select an item:
function selectAnItem() {
for (let round = 1; round <= 5; round++) {
const playerChoice = playerPicks();
const computerChoice = computerPicks();
currentRoundResult = decideCurrentRoundResult(playerChoice, computerChoice);
updateScore();
declareCurrentRoundResult(playerChoice, computerChoice);
}
}

function playerPicks() {
return prompt("Rock, Paper, or Scissors? What's your choice?").toLowerCase();
}

function computerPicks() {
const gameItems = ["rock", "paper", "scissors"];
const itemChoice = Math.floor(Math.random() * gameItems.length);
return gameItems[itemChoice];
}

// Step 4: Compare the items the contestants chose and decide each round's winner:
function decideCurrentRoundResult(playerChoice, computerChoice) {
if (playerChoice === "rock" && computerChoice === "scissors") {
return "You Win!";
} else if (playerChoice === "scissors" && computerChoice === "rock") {
return "You Lose! Rock crushes Scissors";
} else if (playerChoice === "rock" && computerChoice === "paper") {
return "You Lose! Paper covers Rock";
} else if (playerChoice === "paper" && computerChoice === "rock") {
return "You Win!";
} else if (playerChoice === "paper" && computerChoice === "scissors") {
return "You Lose! Scissors cuts Paper";
} else if (playerChoice === "scissors" && computerChoice === "paper") {
return "You Win!";
} else if (playerChoice === "rock" && computerChoice === "rock") {
return "It's a tie!";
} else if (playerChoice === "scissors" && computerChoice === "scissors") {
return "It's a tie!";
} else if (playerChoice === "paper" && computerChoice === "paper") {
return "It's a tie!";
} else {
return (
"Oops! That's a wrong choice!" +
"\nSo, You Lose!" +
"\nRock, Paper, or Scissors are the acceptable options."
);
}
}

// Step 5: Assign a point to each round's winner:
function updateScore() {
if (currentRoundResult === "You Win!") {
return (playerScore += 1);
} else if (currentRoundResult === "It's a tie!") {
return;
} else {
return (computerScore += 1);
}
}

// Step 6: Declare the current round's result:
function declareCurrentRoundResult(playerChoice, computerChoice) {
console.log(`My selection is: ${playerChoice}`);
console.log(`Computer's selection is: ${computerChoice}`);
console.log(currentRoundResult);
console.log("***========================***");
}

// Step 7: Show the total scoreboard:
function showTotalScoreBoard() {
console.log(
`
||*****Total Score*****||
=========================
Player: ${playerScore} vs. Computer: ${computerScore}
`
);
}

// Step 8: Declare and congratulate the overall winner:
function declareOverallWinner() {
console.log("***========================***");
if (playerScore > computerScore) {
console.log("Congratulations!!! You won the game!");
} else if (playerScore === computerScore) {
console.log("That's a Draw!");
} else {
console.log("Game Lost. Try again.");
}
}

Try Editing It

The procedural program above defined the steps the computer should follow to implement the rock-paper-scissors game.

Let's now discuss the object-oriented approach to implementing the same game.

CodeSweetly ads

Design in Figma, launch in Webflow

Bring your static designs to life with IX2, wire up content using our powerful CMS, and one-click publish onto the fastest hosting infrastructure.
Find out more

What is the object-oriented technique to decide who starts the chess game?

The issue to settle: Who gets the white chess pieces required to make the first game move?

Method to settle the issue: Play 5 rounds of rock-paper-scissors game.

Contestants: Player vs. Computer

How to illustrate a rock-paper-scissors game using the object-oriented paradigm

Illustration of the object-oriented way to develop a
program

An illustration of the object-oriented implementation plan of a rock-paper-scissors game

Let's now convert the above diagram into a JavaScript program.

How to program a rock-paper-scissors game in JavaScript using the object-oriented paradigm

class RockPaperScissorsGameData {
// Create memories for the results:
#currentRoundResult = "";
#playerScore = 0;
#computerScore = 0;

// Player and computer select an item:
selectAnItem() {
for (let round = 1; round <= 5; round++) {
const playerChoice = this.#playerPicks();
const computerChoice = this.#computerPicks();
this.#currentRoundResult = this.#decideCurrentRoundResult(
playerChoice,
computerChoice
);
this.#updateScore();
this.#declareCurrentRoundResult(playerChoice, computerChoice);
}
}
getScore(contestant) {
return contestant === "player" ? this.#playerScore : this.#computerScore;
}
#playerPicks() {
return prompt(
"Rock, Paper, or Scissors? What's your choice?"
).toLowerCase();
}
#computerPicks() {
const gameItems = ["rock", "paper", "scissors"];
const itemChoice = Math.floor(Math.random() * gameItems.length);
return gameItems[itemChoice];
}

// Compare the items the contestants chose and decide each round's winner:
#decideCurrentRoundResult(playerChoice, computerChoice) {
if (playerChoice === "rock" && computerChoice === "scissors") {
return "You Win!";
} else if (playerChoice === "scissors" && computerChoice === "rock") {
return "You Lose! Rock crushes Scissors";
} else if (playerChoice === "rock" && computerChoice === "paper") {
return "You Lose! Paper covers Rock";
} else if (playerChoice === "paper" && computerChoice === "rock") {
return "You Win!";
} else if (playerChoice === "paper" && computerChoice === "scissors") {
return "You Lose! Scissors cuts Paper";
} else if (playerChoice === "scissors" && computerChoice === "paper") {
return "You Win!";
} else if (playerChoice === "rock" && computerChoice === "rock") {
return "It's a tie!";
} else if (playerChoice === "scissors" && computerChoice === "scissors") {
return "It's a tie!";
} else if (playerChoice === "paper" && computerChoice === "paper") {
return "It's a tie!";
} else {
return (
"Oops! That's a wrong choice!" +
"\nSo, You Lose!" +
"\nRock, Paper, or Scissors are the acceptable options."
);
}
}

// Assign a point to each round's winner:
#updateScore() {
if (this.#currentRoundResult === "You Win!") {
return (this.#playerScore = this.#playerScore + 1);
} else if (this.#currentRoundResult === "It's a tie!") {
return;
} else {
return (this.#computerScore = this.#computerScore + 1);
}
}

// Declare the current round's result:
#declareCurrentRoundResult(playerChoice, computerChoice) {
console.log(`My selection is: ${playerChoice}`);
console.log(`Computer's selection is: ${computerChoice}`);
console.log(this.#currentRoundResult);
console.log("***========================***");
}
}

const gameRegister = {
// Show the total scoreboard:
totalScoreBoard: function () {
console.log(
`
||*****Total Score*****||
=========================
Player: ${this.getScore("player")} vs. Computer: ${this.getScore(
"computer"
)}
`
);
},
// Declare and congratulate the overall winner:
overallWinnerDeclarion: function () {
console.log("***========================***");
if (this.getScore("player") > this.getScore("computer")) {
console.log("Congratulations!!! You won the game!");
} else if (this.getScore("player") === this.getScore("computer")) {
console.log("That's a Draw!");
} else {
console.log("Game Lost. Try again.");
}
},
};

const gameData = new RockPaperScissorsGameData();

// Start the contest:
gameData.selectAnItem();
gameRegister.totalScoreBoard.call(gameData);
gameRegister.overallWinnerDeclarion.call(gameData);

Try Editing It

The object-oriented program above created objects and instructed the computer to use the objects' data and methods to implement the rock-paper-scissors game.

Let's now discuss the functional approach to implementing the same game.

What is the functional tactic to decide who starts the chess game?

The issue to settle: Who gets the white chess pieces required to make the first game move?

Method to settle the issue: Play 5 rounds of rock-paper-scissors game.

Contestants: Player vs. Computer

How to illustrate a rock-paper-scissors game using the functional paradigm

Illustration of the functional paradigm way to develop a program

A To-do diagram illustrating the functional paradigm implementation plan of a rock-paper-scissors game

Let's now convert the above diagram into a JavaScript program.

How to program a rock-paper-scissors game in JavaScript using the functional paradigm

function playerPicks() {
return prompt("Rock, Paper, or Scissors? What's your choice?").toLowerCase();
}

function computerPicks() {
const gameItems = ["rock", "paper", "scissors"];
const itemChoice = Math.floor(Math.random() * gameItems.length);
return gameItems[itemChoice];
}

function updateScore(currentGameScores, currentRoundResult) {
// Create memories for the results:
let playerScore = 0;
let computerScore = 0;

// Assign a point to each round's winner:
if (currentRoundResult === "You Win!") {
playerScore = currentGameScores.playerScore + 1;
computerScore = currentGameScores.computerScore;
} else if (currentRoundResult === "It's a tie!") {
playerScore = currentGameScores.playerScore;
computerScore = currentGameScores.computerScore;
} else {
playerScore = currentGameScores.playerScore;
computerScore = currentGameScores.computerScore + 1;
}
return { playerScore, computerScore };
}

// Compare the items the contestants chose and decide each round's winner:
function decideCurrentRoundResult(playerChoice, computerChoice) {
if (playerChoice === "rock" && computerChoice === "scissors") {
return "You Win!";
} else if (playerChoice === "scissors" && computerChoice === "rock") {
return "You Lose! Rock crushes Scissors";
} else if (playerChoice === "rock" && computerChoice === "paper") {
return "You Lose! Paper covers Rock";
} else if (playerChoice === "paper" && computerChoice === "rock") {
return "You Win!";
} else if (playerChoice === "paper" && computerChoice === "scissors") {
return "You Lose! Scissors cuts Paper";
} else if (playerChoice === "scissors" && computerChoice === "paper") {
return "You Win!";
} else if (playerChoice === "rock" && computerChoice === "rock") {
return "It's a tie!";
} else if (playerChoice === "scissors" && computerChoice === "scissors") {
return "It's a tie!";
} else if (playerChoice === "paper" && computerChoice === "paper") {
return "It's a tie!";
} else {
return (
"Oops! That's a wrong choice!" +
"\nSo, You Lose!" +
"\nRock, Paper, or Scissors are the acceptable options."
);
}
}

// Declare the current round's result:
function declareCurrentRoundResult(
playerChoice,
computerChoice,
currentRoundResult
) {
console.log(`My selection is: ${playerChoice}`);
console.log(`Computer's selection is: ${computerChoice}`);
console.log(currentRoundResult);
console.log("***========================***");
}

// Player and computer select an item:
function selectAnItem(
playerPicks,
computerPicks,
updateScore,
decideCurrentRoundResult,
declareCurrentRoundResult
) {
let currentGameScores = { playerScore: 0, computerScore: 0 };
for (let round = 1; round <= 5; round++) {
const playerChoice = playerPicks();
const computerChoice = computerPicks();
const currentRoundResult = decideCurrentRoundResult(
playerChoice,
computerChoice
);
currentGameScores = updateScore(currentGameScores, currentRoundResult);
declareCurrentRoundResult(playerChoice, computerChoice, currentRoundResult);
}
return currentGameScores;
}

// Show the total scoreboard:
function showTotalScoreBoard(playerScore, computerScore) {
console.log(
`
||*****Total Score*****||
=========================
Player: ${playerScore} vs. Computer: ${computerScore}
`
);
}

// Declare and congratulate the overall winner:
function declareOverallWinner(playerScore, computerScore) {
console.log("***========================***");
if (playerScore > computerScore) {
console.log("Congratulations!!! You won the game!");
} else if (playerScore === computerScore) {
console.log("That's a Draw!");
} else {
console.log("Game Lost. Try again.");
}
}

// Start the contest:
function startContest(
selectAnItem,
playerPicks,
computerPicks,
updateScore,
declareCurrentRoundResult,
decideCurrentRoundResult,
showTotalScoreBoard,
declareOverallWinner
) {
const totalScores = selectAnItem(
playerPicks,
computerPicks,
updateScore,
decideCurrentRoundResult,
declareCurrentRoundResult
);
const playerScore = totalScores.playerScore;
const computerScore = totalScores.computerScore;
showTotalScoreBoard(playerScore, computerScore);
declareOverallWinner(playerScore, computerScore);
}

startContest(
selectAnItem,
playerPicks,
computerPicks,
updateScore,
declareCurrentRoundResult,
decideCurrentRoundResult,
showTotalScoreBoard,
declareOverallWinner
);

Try Editing It

The functional program above declared the tasks the computer needs to do to implement the rock-paper-scissors game.

Now that you know how to use the three programming paradigms, let's compare them.

Procedural vs. Object-Oriented vs. Functional Programming Paradigm

Here are the primary differences between the procedural, object-oriented, and functional programming paradigms.

ProceduralObject-OrientedFunctional
Communication StyleImperative: Tells you what to do and the exact way to do it.Imperative: Tells you what to do and the exact way to do it.Declarative: Tells you what to do but not how to do it.
FocusCreate a step-by-step guide to accomplish specified tasks.

Construct and use objects' data and methods to accomplish specified tasks.

Create tasks (functions) to do.
Tools

You can use any tool to create the guide the computer should follow to accomplish your tasks.

Constructors and objects are the primary tools for creating the data the computer needs to accomplish your tasks.

Pure function is the primary tool for defining tasks.

Side effectsYou can read and modify external states.You can read and modify external states.You should avoid creating side effects.
Code reusabilityIts functions are typically not reusable.Its constructors are easy to reuse.Its pure functions are highly reusable.
External code usage

You can define how to use external code within functions and objects.

You can define how to use external code within functions and objects.

Developers typically pass in external code as arguments to parameters.

Which Is the Best Programming Paradigm—Procedural, Object-Oriented, or Functional?

The best programming paradigm is the one that suits you and your project best.

Which of the three do you feel comfortable with? Which one helps you think faster and better? Which style best suits your app's needs?

Frequent use of the three paradigms will help you decide your best one. Nevertheless, here are some things to consider:

  • Some programming languages strictly favor only one paradigm. For instance, Haskell is an exclusively functional language, while Smalltalk is an object-oriented language.
  • JavaScript and other languages (such as PHP, C++, and Python) are versatile languages that support multiple paradigms.
  • One paradigm might work better than others for certain types of projects.
  • Functional programs are highly modular and reusable.
  • Object-oriented programs work like real-world items—which help to easily split codes into logical pieces.
  • Procedural programs are broadly structural and practical for simple projects. However, they can be time-consuming when used for complex projects.

Overview

A programming paradigm is the pattern you prefer to use to develop your application.

Some developers prefer modeling objects to develop their apps, some love step-by-step procedures, yet others are passionate fans of pure functions.

Nevertheless, remember that solving problems is the goal of programming. Paradigms are the means to the goal! The best paradigm is the one that helps you achieve your goals more effectively.

Buy me a coffee