Featured image of post Programming Paradigms Explored

Programming Paradigms Explored

Object Orientated vs Functional vs Asynchronous va Recursive vs Parallel

The Procedural Predicament

First up, we have procedural programming. Think of it as the “to-do list” approach to coding. You write a series of steps, and the computer follows them obediently, much like a dog fetching a stick. Simple, right?

1
2
3
4
5
6
7
8
9
# Python example of procedural programming
def make_sandwich():
    print("1. Take two slices of bread.")
    print("2. Add peanut butter to one slice.")
    print("3. Add jelly to the other slice.")
    print("4. Put the slices together.")
    print("5. Enjoy your sandwich!")

make_sandwich()

In this example, we’re instructing the computer on each step to make a sandwich. Unfortunately, it won’t actually make you a sandwich, but it’ll print the steps. Close enough.

The Object-Oriented Odyssey

Next, we venture into object-oriented programming (OOP). Imagine your code as a collection of objects, each with its own properties and behaviors, like a zoo where the animals can code. Intriguing, right?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// Java example of object-oriented programming
public class Sandwich {
    private String filling;

    public Sandwich(String filling) {
        this.filling = filling;
    }

    public void eat() {
        System.out.println("Eating a delicious " + filling + " sandwich.");
    }

    public static void main(String[] args) {
        Sandwich myLunch = new Sandwich("ham and cheese");
        myLunch.eat();
    }
}

Here, we’ve created a Sandwich class with a filling of your choice. Instantiate it with your favorite ingredients, and voilà! A virtual sandwich. Still not edible, though.

The Functional Fiasco

Now, let’s talk about functional programming. This paradigm treats computation as the evaluation of mathematical functions without changing state or mutating data. Sounds fancy, doesn’t it?

1
2
3
4
// JavaScript example of functional programming
const makeSandwich = (filling) => `Enjoy your ${filling} sandwich!`;

console.log(makeSandwich('turkey and avocado'));

In this snippet, makeSandwich is a pure function that, given a filling, returns a string. No side effects, no mess, just pure, unadulterated sandwich-making.

The Asynchronous Antics

Ever tried to make a sandwich while waiting for the toaster? That’s asynchronous programming for you—doing other things while waiting for something to happen.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// JavaScript example of asynchronous programming
function toastBread() {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve('Toasted bread');
        }, 3000);
    });
}

async function makeBreakfast() {
    console.log('Putting bread in the toaster...');
    const toast = await toastBread();
    console.log(`Time to eat: ${toast} with butter and jam.`);
}

makeBreakfast();

Here, toastBread simulates the time it takes to toast bread. Meanwhile, you can do other things, like dance around the kitchen. Once the toast is ready, you proceed. Efficiency at its finest.

The Recursive Riddle

Recursion is like looking into a mirror reflecting another mirror—it’s the function that calls itself until it doesn’t. Confused? Let’s clarify.

1
2
3
4
5
6
7
8
9
# Python example of recursion
def count_down(n):
    if n <= 0:
        print("Blast off!")
    else:
        print(n)
        count_down(n - 1)

count_down(5)

This function counts down from a given number and then launches. It’s like having your own personal NASA countdown. Minus the rocket.

The Parallel Pandemonium

Parallel programming is all about doing multiple things at once. Imagine making multiple sandwiches simultaneously. Hungry yet?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// C# example of parallel programming
using System;
using System.Threading.Tasks;

class SandwichMaker
{
    static void MakeSandwich(string filling)
    {
        Console.WriteLine($"Making a {filling} sandwich.");
        Task.Delay(2000).Wait(); // Simulate time to make sandwich
        Console.WriteLine($"{filling} sandwich is ready!");
    }

    static void Main()
    {
        string[] fillings = { "ham", "turkey", "veggie", "tuna" };
        Parallel.ForEach(fillings, filling => MakeSandwich(filling));
    }
}

In this C# example, we’re making multiple sandwiches in parallel. Each sandwich takes time, but by multitasking, they’re all ready faster. Your virtual guests will be impressed.


Key Ideas:

ConceptDescription
Procedural ProgrammingWriting code as a sequence of instructions or steps.
Object-Oriented Programming (OOP)Organizing code into objects with properties and behaviors.
Functional ProgrammingTreating computation as the evaluation of mathematical functions.
Asynchronous ProgrammingPerforming tasks concurrently without waiting for each to complete.
RecursionA function calling itself to solve a problem incrementally.
Parallel ProgrammingExecuting multiple tasks simultaneously to improve efficiency.

Reference Links: