There isn't much of a difference between Imperative and Declarative programming, except for where we would use them. Today we are going to explain the key difference that separates these two.
Imperative Programming
Imperative programming can be the easiest for beginnings since it relies more on a step-by-step process for executing code. This style of coding allows the programmer to tell the program what to do, how to do it, and when to do it. This process is called the control flow!
# Calculate the Result in the list
result = 0
theList = [2, 4, 6, 8, 10]
# Creating a for loop to add numbers in the list to the Result
for x in theList:
result += x
print(result)
Languages that are examples of Imperative Programming:
- Java
- C
- Pascal
- Python
- Ruby
- Fortran
- PHP
Declarative Programming
Declarative Programming in contrast to Imperative defines the results we want to accomplish without describing its control flow. This process allows the us to place emphasis on the results of the overall goal instead of the execution process. It can lead to more descriptive and specific code. Declarative structure is precise with less code, reducing complexity and making it easier to read/understand.
theList = [2, 4, 6, 8, 10]
# Setting the result to the sum of numbers in theList
result = sum(theList)
print(result)
Languages that are examples of Declarative Programming:
- SQL
- Miranda
- Prolog
- Lisp
- Many markup languages (ex. HTML)
Both of these structures are useful in certain places. The key differences are that Imperative Programming is easier, step-by-step, and allows the programmer to have complete control allowing them to customize the structure to their needs with control flow. Whereas, Declarative Programming makes it easier to limit the complexity of the code making it shorter and more concise, customization is more complicated since it has reduced code, and it also allows us to easily optimize code, with adding extensions and making upgrades.
Languages that have been updated to support both paradigms include:
- Python
- JavaScript
- C++
- C#
Top comments (0)