Programming Paradigms

When I was ten years old, the first thing that would pop in my mind when I thought of programming were of terminals spitting out insurmountable amounts of information. This image of programming most likely spawned from the infamous movie “Hackers”(1995) which portrayed hackers and programmers as geniuses that could digest an impossible amount information in milliseconds.

The infamous movie “Hackers” (1995)

I was sorely mistaken, the truth is software development is a relatively slow process and it can be really quite tedious at times. We as developers have to spend a tremendous amount of time debugging, sometimes even debugging spaghetti code that somebody else wrote(speaking from experience). Debugging becomes especially more difficult when there is a supernumerary number of paradigms being used across a large code base. These inefficiencies could be mitigated if we are more deliberate about the paradigms that we use.

What is a programming paradigm?

A programming paradigm is a style or way in which one programs. Programming languages allow for a set of programming paradigms depending on how the language was designed. A few notable examples of programming paradigms are declarative, imperative, object-oriented, event-driven, and functional programming. Every single paradigm has its own use case.

The Declarative and Imperative Paradigms

The declarative and imperative programming paradigms date back to the earliest days of programming. Here are examples of declarative and imperative paradigms being implemented in JavaScript:

// Declarative paradigm, the filtering is abstracted awayconst allPets = ["Dog", "Cat", "Hamster"];const goodPets = allPets.filter((pet) => pet !== "Hamster");

and

// Imperative paradigmconst allPets = ["Dog", "Cat", "Hamster"];const goodPets = [];for (let pet of allPets) {    if (pet !== "Hamster") {        goodPets.push(pet);    }}

As you can see, the declarative paradigm is more about the what while the imperative paradigm describes the how. Having the ability to describe the how is often important for efficiencies in code. For instance if you wanted to do more than just filter out items while iterating through the array, you could do so with the imperative approach while the declarative approach may be redundant and inefficient, or sometimes impossible if the functionality that you desire isn’t already abstracted.

The Object-Oriented Programming Paradigm

Many programmers today are familiar with OOP. It rose to popularity in the 1990’s and it is still very popular to this day. OOP is paired with the imperative paradigm. OOP is over-used in my opinion, too many times do I see developers use classes for everything. For instance I have seen a Utils class in a Utils file that was comprised of purely static methods. This code added no real value, no extra readability, all it really added were lines of code. When should we use OOP then? GUI’s or anything event-driven, when OOP would increase readability and decrease redundancy. Simply put, use it where it makes sense!

The Functional Programming Paradigm

The functional programming paradigm in purity is often paired with the declarative paradigm. In a pure functional programming paradigm there is no If-Else conditionals, only function calls. Functional programming is more relevant in its impure form and provides many benefits. In OOP there is a shared state which can cause issues since any method within the scope of a class can mutate that state. In functional programming the state is not shared, functions take an input and provide an output. It is much easier to debug and it is more difficult to create bugs. It has its limitations though, sometimes you need to maintain a state and do other fun stuff that OOP provides.

The Event-Driven Paradigm

The event-driven paradigm is often used for GUI’s. It makes sense right? A user will initiate some event and we handle said event. For this you need an Event Loop and for each potential event, you need a event handler and a event callback. You are almost forced to use this paradigm when creating applications with GUI's.

Conclusion

The imperative and declarative paradigms are opposites but both have valid use cases and are often used together. Do not use OOP where it does not make sense. The event-driven paradigm is often used with applications that have GUI’s. Functional programming is cool when you can use it.

Thanks for reading!

--

--

--

Software Engineer @ Rogo

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Just how expensive is the full AWS SDK?

Salesforce Flow: Why Now is the Perfect Time to Learn

Demystifying Docker Containers Internal and Breaking Myths

P5-Rapid Application Development

How to Create Snippets on VSCode

AWS EKS and Google Cloud Functions

Interesting Facts About MassBit You May Not Know

CS371p Week 12

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Noah Gaeta

Noah Gaeta

Software Engineer @ Rogo

More from Medium

As If Sand Were The Stone…

Fundamental programming patterns I — Loops

What Are SOLID Principles?

code dictionary