In this post, we will be looking at the major differences between imperative and declarative programming paradigms.
Imperative Programming paradigm: The languages that fall into this category have 2 main properties:
- Control flow is explicit meaning how the operation took place, step by step or more naively “First do this then do that”.Thus, the order of steps is crucial.
- Allow side effects.
To understand side effects, first, we should know what “state” is. In layman terms, the state refers to the value of the variable at any given point in the program like x=2.
Side effect is any state change outside the called function scope. In simple terms, if a function changes the state that is outside function scope or local environment then the function/unit of code is said to have a side effect.
Examples of function side effect include modifying any external variable or object property, performing I/O, calling functions having side effects etc.
1 2 3 4 5 6 |
Total = 0 Fare=[10,20,30,40] for x in Fare: Total += x mean_fare=Total/len(Fare) print(mean_fare) |
Let’s see how this code follows the properties of imperative paradigm. First, the order of steps is crucial because if we implement “mean_fare” statement before the loop, it will give a different result. Second, the unit of code i.e. loop, has side effects as it changes the state of the “Total” variable which is outside its scope.
I hope this might have given you the insight of the imperative programming paradigm.
Now, let’s look at some contradictions that I found while searching for this stuff
Some people might argue that the above code belongs to structured programming which is a kind of imperative programming. The main difference between these two is that in the imperative, control flow is defined by the collection of “goto” statements while in the structured, control flow is defined by nested loops, conditionals, and subroutines with no “goto” statements. okay that’s good I said, but then they categorized “Python” into imperative paradigm knowing that it doesn’t have “goto” statement.
So, what I found is that there are terms like structured and non-structured imperative programming. Above code falls into the structured imperative paradigm while if we use the goto statement to define control flow then it falls under the non-structured imperative paradigm.
Declarative Programming paradigm: This is defined as any style of programming that is not imperative. So, the properties will just be the opposite of imperative.
- Sequence of the steps is not crucial.
- Don’t allow Side effects.
In general, the imperative focuses on “how to do” while declarative focuses on “what to do”meaning logic of software without actually describing its flow. Let’s look at the code below to get more insights into this:
1 2 |
def Mean_list(lst): return sum(lst)/len(lst) |
Using reduce() and lambda functions
1 2 3 |
from functools import reduce def Mean_list(lst): return reduce(lambda a, b: a + b, lst) / len(lst) |
Note: Since python in no sense is declarative, the above examples are more towards functional side than declarative. But since functional also falls under the declarative umbrella, above examples can serve as a good platform for understanding declarative concepts.
Explanation: In the above examples, we are describing “WHAT” we want to do rather than HOW (we don’t know HOW lambda and reduce are implemented, we also don’t care). We’re not mutating any state. All of the mutations are abstracted inside of “lambda” and “reduce“. It’s also more readable (once you get used to “lambda” and “reduce“, of course).
Now, you might have got some feeling about the “How to do” and “What to do” approach. Later, this will become more clear when we will go through Functional and other paradigms. Till then, Enjoy.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.