Declarative Programming vs. Imperative (Procedural) Programming

Can Mingir
Jul 13 · 4 min read

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

Image for post
Image for post

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.

Image for post
Image for post
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:

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:

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:

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.

Image for post
Image for post

Nucleoid is open source (Apache 2.0), a runtime environment that allows declarative programming written in ES6 (JavaScript) syntax. Since statements are declarative, the runtime provides logical integrity and persistency as hiding technical details.

Learn more at nucleoid.org

  1. Lloyd, J.W., Practical Advantages of Declarative Programming

Nucleoid

Declarative Runtime System

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store