Programming languages can be classified into two main paradigms: imperative and declarative. While imperative programming has been dominant since those days of programming in machine language, recent advancements in technology have led to a new generation of programming languages that prioritize declarative approaches. In this discussion, we will explore the similarities and differences between these two paradigms. However, before we delve into the details, let's first take a closer look at each of them.
Imperative programming
Imperative programming is the oldest programming paradigm, and it involves issuing precise commands to a computer in order to accomplish a given task. The term "imperative" comes from the Greek word "imperare," which means "to command." Programs based on this paradigm define a sequence of instructions that a computer follows in order to complete a task.
While this approach offers a great deal of flexibility to the programmer, it can also result in longer and more complex code. As a result, it is sometimes easier to achieve the same task using declarative approach.
Imperative programming can be further divided into three subcategories: structured programming, procedural programming, and modular programming. Each of these approaches attempts to address some of the limitations of basic imperative programming
Let's look at a simple example of an imperative program,
Consider if we want to show a message to a user depending on their age.Here is a simple python program
age = int(input("enter age:"))
if age<18:
print("You are underage")
elif age>60:
print('You are above the age limit')
else:
print('You are eligible')
This is just a simple case in real life application, programs and instructions are more complex. The key thing here is how we are instructing the computer on what to do at any given part of the program. The instructions can be viewed as.
- get age from the user in integer format
- If the value of age entered by the user is less than 18 print "You are underage" to the console
- else if the entered value is above 60, print the message "You are beyond the age limit"
- Else print "You are eligible". ( In this case the age entered should be between 18 and 60 inclusive.)
types of imperative programming languages
- FORTRAN
- ALGOL
- COBOL
- C
- JAVA
- Python
- C++
Declarative programming
Declarative programming differs from imperative programming in that it focuses on telling the computer what solution is needed, rather than specifying how to get there. While this approach has significant advantages, such as the ability to achieve complex tasks with fewer lines of code and a syntax that is closer to natural language, it also has limitations. As of today, computers are not yet capable of fully supporting general-purpose declarative languages because the approach shifts much of the problem-solving burden to the computer. Most declarative languages are, therefore, domain-specific and cannot be easily applied outside of that domain, for example one cannot use SQL outside databases.
Despite these limitations, declarative programming has become increasingly popular in fields such as configuration management, databases, and templating. It is also gaining wider application in intelligent systems, such as in the field of artificial intelligence.
Compare the example below on how to create a database table using sql with the previous python program.
CREATE TABLE person(varchar name , int age);
Note how in the above program we didn't tell the computer the steps that it should take in order to create the table. We just describe that we want to create a table called person with columns name and age , and the table is created on the fly.
The interesting thing to note between the two programs is how complex creating a database table is compared to the simple trivial python program above, yet we just use a single line of code in creating the table.
Declarative languages includes
- SQL - Acronym for Structured Query Language is the most popular database language and have been adopted by many modern databases as their default query language
- Analytica
- Datalog
- Gremlin
- Modelica
- Prolog
differences between imperative and declarative programming
Imperative | Declarative |
It describes how to reach the solution | Describes what solution should be reached. |
It's subordinate paradigms includes structured programming, procedural programming and modular programming | Its subordinate paradigms includes logic programming and functional programming |
It is applied in a wide range of programming fields such as application development and web development | It is applied more in domain specific fields such as databases and templating. |
Gives step by step instructions on how to reach the required solution | Describes the required solution without describing how the required results will be obtained |
Describes the control flow of computation | Describes the logic flow of computation |
Offers more flexibility | It is less flexible |
It is the oldest and native programming paradigm | It is relatively new compared to imperative programming. |