13 February 2015

Declarative vs Imperative

There are tons of resources on the internet about imperative and declarative programming paradigms. I am writing this article because I think there are a few key points that are commonly missed that I want to cover.

Declarative and Imperative programming paradigms are nothing but buzzwords for describing coding on different levels of abstraction. The concept is much simpler than people make it out to be. The common description of declarative programming that echoes throughout the internet is that declarative programming is concerned with

"what to do, not how to do it."
And likewise imperative programming is generally described as being concerned with
"How to do it, not what to do
Declarative programming is to program on a higher level of abstraction than imperative programming. Neither is better or worse, but both have their places. An example of where declarative programming is necessary would be in web development when you are working with frameworks. An where of where Imperative programming is necessary would be in the engineering of algorithms and other low level necessities.

The definitions for declarative and imperative are pretty clear cut and ubiquitous throughout the internet. Where things often get muddled is when you start throwing in the notions of functional and procedural programming. The concept of functional and procedural programming paradigms are really just extensions of the concept of declarative and imperative programming paradigms. In fact, functional programming is a subset of declarative programming, and procedural programming is a subset of imperative programming. This makes more sense when you really consider what the difference between a function and a procedure is.

The Difference Between a Function and a Procedure

Both functions and procedures are subroutines that are used for re-executing a predefined block of code. The difference between them is that functions return a value, and procedures do not. More specifically, functions are designed to have minimal side effects, and always produce the same output when given the same input. Furthermore, functions are usually concerned with higher level ideas and concepts. A great example of this would be the underscore library. Procedures on the other hand do not have any return value, their primary purpose is to accomplish a given task and cause a desired side effect. A great example of procedures would be the well known for loop: for (var i = 0;i < arr.length;i++) {...}. The for loop is a subroutine that's main purpose is to cause side effects, and it does not return a value in of itself.

How to Remember the Difference Between Imperative and Declarative

I remember the difference between the two through the use of an analogy. To be declarative is to declare something, and usually when I imagine someone declaring something I picture them as being in a position of power. Imagine the president during the state of the union declaring his intentions for what he wants have to happen. On the other hand, a good analogy for imperative would be akin to that of a manager of a McDonald's franchise. He is very imperative and as a result makes everything really important. He therefore tells everyone how to do everything down to the most simplist of actions.

Wrap up (tldr)

  • Declarative programming refers to code that is concerned with higher levels of abstraction.
  • Imperative programming refers to code that is concerned with lower levels of abstraction.
  • Procedural programming is a subset of imperative programming which utilizes subroutines.
  • Functional programming is a subset of declarative programming which utilizes subroutines.