Programming Paradigm
Tags: todayilearned, computerscience, sql, javascript
Published: 2020-01-12 17:12:23

Sup dudes, hope everything is all right 😎

Today I will talk about programming paradigms. I intend to make a series of posts on the topic mostly because of its complexity. In this post I will talk about two of the main paradigms, the declarative and the imperative paradigm.

I will try to explain (~~if i know~~) briefly and concisely so any questions leave your comment bellow and I will try to answer as soon as possible!

OK, let's go!

You must be asking yourself...

## Why is knowing this so important? 🤔

Well, after my co-worker bombarded me with these terms of "Paradigm" and "Abstraction" every day I decided to understand what this meant and why they seemed so important. It turns out I came to the conclusion that knowing what an imperative and declarative paradigm is is important for two reasons:

1. Helps you better understand the difference between object-oriented programming, procedural programming, and functional programming. Understanding these five terms makes it possible to understand where authors draw their code designs from.
2. Knowing this is a great plus in a job interview.

Before continuing, understand that all five of these terms are *considered programming paradigms*, but Imperative and Declarative are considered "parents" in the hierarchy with regard to procedural, Object Oriented Programming (OOP), and functional.

Okay, I get that. But after all...

## What is a Paradigm?

The word paradigm is derived from the Greek *paradeiknyai*, it commonly refers to the method of thinking about a problem or situation.

Thus a programming paradigm is a means of classifying programming languages based on their functionality. Languages can be classified into several paradigms. Thus, a programming paradigm provides and determines the programmer's view of program design and execution.

## Imperative Paradigms 

![](https://media.giphy.com/media/BRH9RY5fSpDyM/giphy.gif)

### More Technical Explanation: 👨‍💻

These are programming paradigms that use imperative statements to alter the state of a program. Just as imperative statements work in natural language, imperative statements in programming consist of commands for the computer to perform. Thus, imperative programming focuses on describing ***HOW***  a program works.

They are the opposite of declarative paradigms, these focus on ***WHAT***  the program should accomplish without specifying ***HOW***  the program should achieve that result.

Programs written in this imperative way often compile into executable binaries these are the most efficiently since all CPU instructions themselves are imperative statements.

Some of the most famous languages that follow the imperative paradigm are C, Pascal, Fortran and Cobol.

### More practical explanation: 😋

If we imagine the problem of building a house, we have to take the imperative paradigm of ***HOW***  to build the house. So to write a program that builds a house I would do something like:

1. Build the foundation
2. Fit the beams
3. Install the utilities
4. Add the walls
5. Finish the finishing touches

In this kind of programming I said exactly (~~not so much, but you understand~~) what steps to take in order to build a house.

## Declaratives

![](https://media.giphy.com/media/Qa5dsjQjlCqOY/giphy.gif)

### More Technical Explanation: 👨‍💻

It is a paradigm that allows the developer to define what the program ***MUST***  do rather than exactly how it should do it. This approach often considers programs as theories of formal logic and computations as deductions of that logic in space. This paradigm has the benefit of simplifying the programming of some parallel processing applications.

One language that is considered reference in the declarative paradigm is SQL.

### More practical explanation: 😋

Declarative Paradigm is about ***WHAT***. Using the house example, we would have a program as follows:

1. I don't care about the way you build it, but I want a nice balcony, a big kitchen, and a bedroom with a bathroom.

In this kind of programming I say what I expect from the program. I know that in this case, if I give the inputs in the form of money I will get the desired results to build a home.

## It's all about Abstraction 🐱‍🐉

All programmers use the word "abstraction" a lot. An abstraction is to take all the nitty gritty details out of a subject to speak it in a high-level concept. Imagine that you are in the house that your program made, you are taking a shower and realize that no hot water comes out of the shower, you don't want to know all the steps that are in place for the hot water to fall into your head. You just want hot water out of the shower! This is an abstraction.

Thus declarative paradigms allow abstraction, whereas imperatives do not.

## Let's take a look at a basic code example!

Declarative Programming in SQL:

```sql
SELECT * FROM Users WHERE Country = 'Canada';
```

Note that we do not care about how it will take the information, but what it will bring to us!

Imperative Programming in Javascript

```javascript
function double (arr) {
  let results = []
  for (let i = 0; i < arr.length; i++){
    results.push(arr[i] * 2)
  }
  return results
}
```

Here we see that we need to specify exactly what should be done in order to get the expected result!


![](https://media.giphy.com/media/2YgCmRh9Arduw8LshF/giphy.gif)

That was the first post of the series, I hope you enjoy and see you next time!

LINKS/References:

https://pt.wikipedia.org/wiki/Paradigma

https://en.wikipedia.org/wiki/Imperative_programming

https://www.computerhope.com/jargon/i/imp-programming.htm

https://www.computerhope.com/jargon/d/declarprog.htm

https://zachgoll.github.io/blog/2019/imperative-vs-declarative-programming/ (Post Strongly Inspired by this)

https://tylermcginnis.com/imperative-vs-declarative-programming/ (Code References)