Imperative and declarative programming

aryclenio

Ary Barros

Posted on July 12, 2020

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!

💖 💪 🙅 🚩
aryclenio
Ary Barros

Posted on July 12, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Imperative and declarative programming
productivity Imperative and declarative programming

July 12, 2020

Programação imperativa e declarativa
productivity Programação imperativa e declarativa

July 12, 2020