Declarative Programming vs. Imperative (Procedural) Programming

canmingir

Can Mingir

Posted on July 13, 2020

Declarative Programming vs. Imperative (Procedural) Programming

Yet, another article under this title, but this time it is a point of view from a programming runtime.

By dictionary definition, declarative programming is a style of building the structure and elements of computer programs that expresses the logic of a computation without describing its control flow¹. It is unfortunate that this kind definitions don’t explain how both approach impact developers to their daily routine.

Understanding behaviors of both runtime eases to solve the dilemma and gives concrete practices.

Let’s start with imperative runtime, which executes code lines as directed, and a developer is fully in charge of the flow. Some repetitive works might be automated within methods/tactics like OOP, AOP, etc., but at the end, it is a thing that developer have to manage and configure regardless. For example, you have an object, and every single time when its property is changed, you are to change update time when happened, so that, you have to call updateTime() function. From an engineering stand point, it is not a best practice if hard coded everywhere because this increases number of code lines along with complexity, may end up with serious maintainability issues. However, if you defined aspect, it automatically updates, so that, it shortcuts, but the theory stays the same as it requires developers to code and configure.

Alt Text

*Declarative Runtime vs. Imperative Runtime*

Declarative runtime works with intermediate language that they are mostly considered declarative statements as abstraction of willing (Not literal statements). However, the important point here is to understand of the runtime because its specifications define system behaviors. For example:

> a = 1
> b = a + 1
> a = 2
> b
3
Enter fullscreen mode Exit fullscreen mode

In this case, b = a + 1 is a declarative statement that whenever a changed, the runtime should update b along with, so that, b becomes 3.

If we run the same statements in imperative runtime like JavaScript engine, the result will be different:

> a = 1
> b = a + 1
> a = 2
> b
2
Enter fullscreen mode Exit fullscreen mode

Because a in b = a + 1 only represents memory block location, so that, updating a with 2 doesn’t impact b itself.

We can use more advanced case like including if statement:

> a = 1
> b = a + 1
> if( b > 5 ) {
    c = true
  } else {
    c = false
  }
> c
false
> a = 5
> c
true
Enter fullscreen mode Exit fullscreen mode

Now in this case, when a = 5 called, the runtime automatically aligns rest with calling b = a + 1 and if( b > 5 ) { .. } statements.

Control flow is probably the most important parameter in order to distinguish both because at the end of transaction, logical/data integrity cannot be broken, so that, side effects of execution requires to align with rest of the system. In imperative programming, it is developer responsibility because s/he directly manages the control flow. However, in declarative programming, since the system is accepting declarative statements, the runtime is fully in charge. In the last example above, dependency flow goes like c -> b -> a so, at the time a is changed, the runtime automatically reruns b = a + 1 as well as if( b > 5 ) {..}. However, in imperative programming developer has to run something like a = 5; updateB(a); updateC(b); assuming functions contains the logic.

Conclusion

Both programming styles play significant roles while programming. In high-level programming, especially, in data-driven applications, Declarative runtime may automate repetitive flow along with simplifying code structure, and at the same time, imperative programming gives full control to developer especially it is very important in low-level programming or mission-critical jobs. Clearly, popularity of declarative and functional programming is arising, but as of today, imperative programming including its libraries, tooling, etc. heavily dominates programming languages.


Alt Text

Nucleoid is an open source (Apache 2.0), a runtime environment for declarative programming in ES6/JavaScript syntax and runs as a datastore. Since statements are declarative, the runtime automatically provides logical integrity, multiprocessing, plasticity, persistency etc.

Learn more at nucleoid.org


  1. Lloyd, J.W., Practical Advantages of Declarative Programming
💖 💪 🙅 🚩
canmingir
Can Mingir

Posted on July 13, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related