What is Declarative Programming?

Tram Ho

Introduce

It can be said that for each programmer, the concept has been deeply embedded in the subconscious since the beginning, it is Functional Programming, but in the actual process, this programming concept has revealed many problems when the real problem is The big reality and Declarative Programming (DP) were born, which lies at an “extreme” to Imperative Programming (IP), which is often associated with Object-Oriented Programming (OOP), or object-oriented programming. Imperative Programming is too familiar to programmers in general, it can be said that it has dominated the programming world for over 50 years. So what is Declarative Programming ?

Let’s find out together!

What is Declarative Programming?

The above two models are quite broad, sounding quite vague, but in summary, they can be understood as follows:

  • Imperative Programming: tell “machine” how to solve it and what result you want.
  • Declarative Programming: tell “machine” what you want to happen and the computer calculates how to make it.

You can imagine that with Imperative Programming, you are interested in how to solve the problem and the Declarative Programming cares about the input and output of the problem.

Still sound confusing?

Let’s follow the code below written under the following Imperative Programming:

The above code looks very complicated at first glance, but only downloads, parses and processes some data. So let’s try and see if with Declarative programming, how things will change.

It can be seen that the steps are clearer, a newcomer can easily see the function and how the steps are performed.

Continue with another example written under the following Imperative Programming:

And when rewritten with Declarative Programming:

Obviously the code has become much more coherent and clear.

The benefits of the Declarative Programming

1. Limit the variation

Restrict the change of objects and data in the program. You almost do not have to worry about the data being called, has not been changed in any function, which thread (thread) affects it …?

2. Minimize side-effects

The concept of side-effect currently has no specific definition, but imagine when you debug an error generated in another dev function written. After finding out the cause of some variable X, you search, you see this variable X appears in 10 different functions, and obviously you will have to read and understand all these functions carefully and find out the last cause. The same is where it was changed, for what reason?

1425999735-1425293914-windows8-bsod-780x488.jpg

Especially with multi-thread programming problem or more, parallel computational programming using Multi-core (CPU), you will see the change of data between threads / CPU core. will be very complicated and cause a lot of problems when the problem occurs.

3. The code is short, clear, and easy to understand

In the 2 above examples you have seen when using the Declarative, your code will be shorter, clearer and much easier to read. Simply as it focuses on input, what do you want to do with it to generate the output. Imagine that with the Imperative, you have to read a series of nested loops, call statements and then brainwash the strings to know what other developers write this code for.

4. Easy to expand

The Declarative Programming is easier to read, simpler, and clearer so it will definitely be easier to extend and maintain.

Conclude

“Functional Programming” is too familiar, even ingrained into the subconscious of programmers, leading to changing this thinking will be very difficult, instead of the Declarative’s “What” thinking, the “How” thinking has into a trail. Hopefully the article brings a new approach to programming for readers.

Thank you for your time!

Source:

https://www.netguru.co/blog/imperative-vs-declarative https://en.wikipedia.org/wiki/Declarative_programming

Share the news now

Source : Viblo