The 2 Programming Paradigms

CodeLydia
4 min readAug 24, 2021

--

The moral dilemma of picking the best-fit .

What’s the point of knowing the 2 paradigms?

Before anything, let’s get the unspectacular definitions out the way:

Paradigm : an outstandingly clear or typical example or archetype

So , a Programming Paradigm is a typical example of programming ,

or a “ WAY ” of programming

Programming paradigms are the fundamental principles used when developing software.

Learn the methods. Choose the application . Improve your code efficiency.

The 2 MAIN paradigms ( yes, there are more. These two are the umbrella term that each include more sub-paradigms. Exciting! Or not… ) :

The main paradigms and their sub-paradigms
  • Declarative Programming : What to do ?
  • Imperative Programming : How to do it ?

Enough wish-wash waffle with the definitions, let’s cut straight to the chase.

Here’s the prompt for a Basic JavaScript Algorithm:

Return Largest Numbers in Arrays

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].

You could easily whip up some recursive high function, and pass the tests on first run:

Observe the quantitative differences between this solution and the following one:

Looks like the function just had a growth spurt in the span of a second!

The in-depth analysis for the 1st solution:

  • Initialise the second parameter of the function (finalArr) = empty array []
  • Return whichever of the following conditions is true:
  • If the length of the parent array is a falsy value
  • Return the empty array , finalArr ( [] )
  • Else, call this same function, passing all the numbers in the array starting from arr[1], and the result of concatenating the value of finalArr with the largest number in each nested array arr[0]

And for the 2nd:

  • Create 1 variable results = empty array []
  • Create an outer loop to iterate through the outer array.
  • Create a 2nd variable to hold the largest number
  • Initialise it with the first number in the first sub-array.
  • (This must be outside the sub-array inner loop so it won’t be reassigned until we find a larger number )
  • Create said inner loop to work with the sub-arrays.
  • Check if the element of the sub array is larger than the currently stored largest number. If so, then update the number in the variable.
  • After the inner loop, save the largest number in the corresponding position inside of the results array.
  • And finally return the array.

As to be expected, both output the same , correct solution.

Surely one of them is the better one…

From a point of view of readability ,

  • Imperative is more human-readable, easier to understand
  • The program flow is more easily tracked

practicality :

  • Imperative is excellent for general-purpose programming
  • There are more books and online courses available on tested algorithms

mutability :

  • Imperative is more easily managed
  • Reusable in other programs

… Nope. There’s no rose without a thorn

  • Imperative programs is slower and uses up a high amount of memory
  • Over-generalization
  • Programs built using this paradigm take longer to be created
  • The method can have side-effects

In the areas where imperative FAILS, declarative EXCELS

how to implement these paradigms as tools?

A sweet ending-summary to close up this article:

No programming paradigm is going to be your bread and butter all the time

You have to CHOOSE . Instead, use a judging system with categories ( Mutability, Importance of Order of Statements in the Function , Flow control, Side-effects, Memory Occupied, Coding Language Type, etc. ) .

RANK the categories in order of importance, and use the top most important category to identify the paradigm you are going to adopt.

Exploit the benefits of each to their 100% ( but know their limits )

Like, Comment and Share

the original code question is from : https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays

Follow me on Twitter https://twitter.com/LydBb8 for coding shortcuts / concepts explained the simple way

--

--

CodeLydia
0 Followers

0-jargon Code concepts and latest tech explained