DEV Community

Matheus Gomes πŸ‘¨β€πŸ’»
Matheus Gomes πŸ‘¨β€πŸ’»

Posted on • Edited on

Programming Paradigm

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

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

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:

SELECT * FROM Users WHERE Country = 'Canada';
Enter fullscreen mode Exit fullscreen mode

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

Imperative Programming in Javascript

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

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

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)

Top comments (9)

Collapse
 
dexygen profile image
George Jempty

Not to be presumptuous but maybe the following post of mine from 12 months ago will help demonstrate how to institute a system that follows the declarative paradigm (rather than for instance just using what SQL hands you): dev.to/dexygen/accidentally-declar...

Collapse
 
matheusgomes062 profile image
Matheus Gomes πŸ‘¨β€πŸ’»

Fantastic way of mapping input fields. Indeed a much better way, although you started with something similar, using sql to explain. Thanks for the post!

Collapse
 
dexygen profile image
George Jempty

Thanks, of course now with more modern frameworks (that was a jQuery application) things could probably be done reactively.

Thread Thread
 
matheusgomes062 profile image
Matheus Gomes πŸ‘¨β€πŸ’»

That what i was thinking, i did something similar with vuejs. It's a lot easier.

Thread Thread
 
dexygen profile image
George Jempty

I look forward to your upcoming articles. Beware, when posting about functional programming, sometimes chaining methods such as map and filter can hurt performance, as they essentially iterate over the same data set over and over. It's very rare that I've met such a situation that I couldn't address with reduce, and I also did a jsperf comparing my code to the code relying on chaining, and mine was nearly twice as fast.

Thread Thread
 
matheusgomes062 profile image
Matheus Gomes πŸ‘¨β€πŸ’»

I will make sure to study a lot so i don't make any mistakes!

Thread Thread
 
dexygen profile image
George Jempty

Don't worry, everybody makes mistakes. Actually setting out to teach is a very good way to learn. By the way, are you "following" me? I followed you, if you follow back we can have private discussions, in fact I want to tell you who my 2 favorite programmers from Brazil are

Collapse
 
maryvorontsov profile image
Mary Vorontsov

Hey! Would you be interested in a paid writing opportunity in your spare time? Please, ping me back if interested.

Collapse
 
matheusgomes062 profile image
Matheus Gomes πŸ‘¨β€πŸ’»

Sure, you can send me a message or an email. Let's talk.