DEV Community

Cover image for Imperative and declarative programming
Ary Barros
Ary Barros

Posted on

Imperative and declarative programming

Nowadays, programming has become the main routine for people inserted in the technology market. Whether in front-end, back-end programming, data science, microcontrollers, among others.

Many of us view programming as a kind of order, where you tell the computer what you want, using codes, and it will return it to you in the right way.
From this thinking, programming languages ​​as we know them today with structures of repetition and condition arose. Based on that we know today the Imperative Programming.

What is imperative programming

Most programming languages ​​are based on procedures, and try to address real situations and workings. Since programming is a way and not an end, the programmer's natural process is to focus on how to solve a certain problem, without often verifying and consolidating its solution.

Man pointing to computer

Imperative programming arose from the fact that, through codes, the programmer writes situations that indicate something to the computer through the imperative conjugation of verbs, always following a structured and sequential method of things.

  • If this happens > Do it
  • If A is equal to B > Produce this block
  • As long as there is C > Make D appear

And it is from these situations that many codes of different languages ​​can demonstrate this situation. Let's see some below:

If Else em Lua

    if op == "+" then
      r = a + b
    elseif op == "-" then
      r = a - b
    elseif op == "*" then
      r = a*b
    elseif op == "/" then
      r = a/b
    else
      error("invalid operation")
    end
Enter fullscreen mode Exit fullscreen mode

For em Python

for item in [3,4,5,6,7]:
    print(item)
Enter fullscreen mode Exit fullscreen mode

While em Java

public class while {
    public static void main(String args[]) {
        int cont = 0;
        while (cont < 50) {
            System.out.println("Repeat: " + cont);
            cont++;
        }
    }
} 
Enter fullscreen mode Exit fullscreen mode

Benefits

Programming imperatively is the closest model to what we could see in the real world between the human-computer iteration. Its understanding is easy at initial levels and is efficient in most cases, becoming the general model of several languages.

Disadvantages

Despite all this, imperative programming in large projects has difficult legibility and maintainability, always focusing on how the task should be done and not what should be done, generating treatments of confusing data and programs more susceptible to errors.

And where does declarative programming come in?

Declarative programming is a concept that underpinned many existing languages, becoming popular with Javascript, and some already consolidated as SQL.

Declarative programming focuses on what needs to be solved, and thus seeks clean codes, free from complexity and structural approaches, where the focus is on logic, maintenance and reduction of side effects. This favors reusable, readable and succinct code.

Alt Text

How about an example?

Javascript can use both approaches. Below is a code that adds "I Love" to an array of languages.


let languages = ["python", "java", "go", "ruby"];
// Imperative

for(let i = 0; i < languages.length; i++){
   languages[i] = "I love "+ languages[i];
}
// Return: ["I love python", "I love java", "I love go", "I love ruby"]

// Declarative

languages.map(language => "I love" + language);

// Return: ["I love python", "I love java", "I love go", "I love ruby"]

Enter fullscreen mode Exit fullscreen mode

Note that in the declarative code, there was no indication to the computer of how it should do the process, but by reading the code itself, we realized that it will map the array, and return what we wanted. The code became cleaner, less verbose and easily replicable.

However, not everything is flowers, using the declarative code requires further study, in addition to a difficult adaptation, which is the result of ancient habits in imperative languages.

Benefits

  • Reduce side effects
  • Readability
  • Bug reduction

Disadvantages

  • Difficult to adapt
  • High complexity of use

Final verdict

Nowadays, functional, and consequently, declarative programming has become
the current code standard. This growth makes it easier for new languages ​​to adapt to this and generate more readable programs with higher performance.

Programming is a way, not an end to solving a problem.

Thanks for reading!

Top comments (4)

Collapse
 
sonofsafwat profile image
Abdelrhman Safwat

What you’re explaining in this article could be considered one of the best introductions to functional/declarative programming. For the past year I’ve been attempting to unlearn and relearn everything and it’s an ongoing process. But the outcome is very rewarding. Thanks for this amazing intro

Collapse
 
limitedeternity profile image
Marise Hayashi

That finger touching the screen on a pic... makes me feel some kind of anger.

Collapse
 
joakim profile image
Joakim

In the back there's another finger slowly approaching a screen. Must be imperative programmers.

Collapse
 
fcojavierlpz profile image
Francisco J. López

I always had trouble understanding it, but your explanation was very simple and quite clear, thanks.