What is Imperative and Declarative Programming | Imperative Declarative Difference | InterviewDOT
Channel: Interview DOT
Date: 09/01/2020

Click here - https://www.youtube.com/channel/UCd0U_xlQxdZynq09knDszXA?sub_confirmation=1 to get notifications.

What is Imperative and Declarative Programming | Imperative Declarative Difference | InterviewDOT


Declarative programming is a programming paradigm … that expresses the logic of a computation without describing its control flow.
Imperative programming is a programming paradigm that uses statements that change a program’s state.

Imperative Programming
So, what is imperative programming? The shortest way to answer this question is to say that by using the imperative programming language we try to say HOW we want to do something. For this purpose, we use statements that can change the current state of the application.

In imperative programming, you use assignment statements to locate some information in memory to use it later. The wide use of looping statements allows executing sequences of statements. If you want to check whether some condition is met before performing some actions, you can use conditional branching statements.

To better understand how this paradigm works, let’s take a look at a small example. Imagine that we want to change the color of a button after a user clicks it. According to the imperative paradigm, we have to know how we want to do it. Thus, we have to check the current state of the component and manipulate it.

Declarative Programming
Here’s the second big question. What is a declarative language?
The term Declarative programming is often used as an opposite to the Imperative programming. Shortly speaking, this paradigm allows you to declare WHAT you want to be done.

This style of developing implies description of the logic of computation but not its control flow. By describing only the result that you want to get from the application (instead of the ways of reaching this result), you can minimize unwanted side effects. Developers describe the results they want to get without explicit description of required steps.

Declarative programming is “the act of programming in languages that conform to the mental model of the developer rather than the operational model of the machine.” 
In computer science, declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow.

Declarative Programming Languages
Declarative Programming Languages focus on on describing what should be computed - and avoid mentioning how that computation should be performed. In practice this means avoiding expressions of control flow: loops and conditional statements are removed and replaced with higher level constructs that describe the logic of what needs to be computed.

The usual example of a declarative programming language is SQL. It lets you define what data you want computed - and translates that efficiently onto the database schema. This lets you avoid having to specify details of how to execute the query, and instead lets the query optimizer figure out the best index and query plan on a case by case basis.

Python isn't a pure Declarative Language - but the same flexibility that contributes to its sluggish speed can be be leveraged to create Domain Specific API's that use the same principles. I thought it would be kind of interesting to look at a couple specific examples of how this plays out.