The Art of Organizing and Writing Code: Paradigms

Alok Sharma
Level Up Coding
Published in
4 min readJun 29, 2020

--

I keep emphasizing my articles around ‘Readability’, ‘Maintainability’, and ‘Scalability’ of the code, as these are the pillars that determine the quality of the codebase that we are developing. A great developer is not one who only knows multiple languages and technologies, but one who is proficient and effective at organizing code with the tech stack that they possess.

Efficiency comes from knowing the different strategies and techniques that make our codebase morestructured’ and ‘organized’, and more importantly learning which technique to introduce depending upon the scope and the size of the project.

These techniques include using ‘Architectures’, ‘Patterns’, and ‘Paradigm’ that help us in splitting and organizing our application into multiple smaller components/fragments, which promotes ‘Reusability’, ‘Readability’,Scalability’, as well as Maintainability’.

In this article, We will be discussing the What, Why, and How of ‘Paradigms’.

What is a Paradigm?

Paradigm is a way of structuring, organizing, and writing our code.

In this modern era, machines and languages have become so powerful that they can easily understand and execute our code, Even omitting our certain mistakes, But as the old adage says —

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” — Martin Fowler

Paradigms allow us to write code that is more structured, organized, and understandable. They make the flow of data and changes to the state across the application more “predictable”.

We know that the purpose of writing any program is to solve a particular problem, and there can be multiple ways in which we can approach that problem. So, Paradigm can be regarded as an ‘approach’ to solving a problem or developing an application.

These ‘paradigms’ or ‘approaches’ are broadly categorized as —

  1. Imperative Paradigm, And
  2. Declarative Paradigm.

Imperative Paradigm

In Imperative programming, we code logic that is a step by step instruction regarding how to solve a problem. Developers have to write a complete ‘algorithm’ that will produce the desired result.

Let’s understand it with an example — The function defined below will simply square each value present within an array using ‘Imperative Paradigm’.

Note — I will be using Javascript for demonstration purposes. You can use whichever language you want. The use of Paradigms is independent of the choice of the language (syntax may differ).

Imperative Programming

In the above example, We have used a ‘for loop’ where we need to specify the initial condition (i = 0), the test condition (i < values.length), how the steps will update (i++), etc to get the desired result. So, In Imperative Programming, We have to focus more on the HOW.

Declarative Paradigm

In Declarative programming, we code logic that will give us the desired result without writing a step by step instruction or an algorithm. For example — The function defined below will solve the above problem using the ‘Declarative Paradigm’ as

Declarative Programming

In the above example, We don't need to specify the initial condition, test condition, update step, etc to get the desired result. The ‘map’ method will figure it out automatically. So, In Declarative Programming, We have to focus more on the What.

Important —

  • In the Imperative Paradigm example, We have done changes directly to the ‘values’ array. But in Declarative Paradigm example, We have used another variable ‘squaredValues’ to store the desired result, because —
  • In Imperative Programming, direct changes to the original state (values) are allowed, But In Declarative Programming, direct changes to the state are not preferred. As
  • Direct changes to the state make our code less ‘predictable’ as the state can be manipulated directly from different parts of the application, while the Declarative paradigm makes our code more ‘predictable’ and ‘manageable’.
  • Imperative Programming improves your problem-solving skills to a greater extent as compared to Declarative Programming as conceptual logics are used to write an algorithm. On the other hand, Declarative programming makes the development process much faster.
  • Imperative code easily becomes extensive and confusing which also increases the risk of errors and makes it difficult to debug code. On the other hand, Declarative code is shorter, easier to understand and debug.

So, both the paradigms have their own pros and cons, but learning the art of choosing the right paradigm for your application will make you grow as a developer, which will come from experimenting and experience.

The most popular examples of Imperative Paradigms are — Procedural and Object-Oriented Programming, and Declarative Paradigms are — Functional and Logical Programming.

As a last note, there are languages that support the usage of only one paradigm like Smalltalk(Object Oriented), Haskell (Functional) and there are also languages that promote the usage of multiple paradigms like Javascript, Python, C++, Java, etc

That’s it for this article. Object-Oriented and Functional Programming will be discussed in the upcoming articles. If you find this article helpful just do let me know. Stay Tuned!!!

--

--