Recently, while preparing for my finals, I saw a question related to declarative programming. It was a familiar topic. I have heard this somewhere before, but couldn’t remember what exactly declarative programming is. Maybe I missed the lesson sleeping when my teacher was explaining it during the class ¯\_(ツ)_/¯
. So I decided to learn more about declarative programming, and I want to share what I learned.
What is actually declarative programming? Declarative programming is a programming paradigm that’s the opposite of imperative programming. Wait, what? What is imperative programming? In imperative programming, you should define steps that your program follows to achieve the “end” result. In other words, you have to describe how your program should operate. In some cases, it is also called procedural programming. For example, let’s say you want to find an element in an array. What are you going to do? Of course, you’ll write an algorithm that finds an element in an array. You will write an algorithm that sorts the array first and then uses binary search to find the element. As you can guess, you are describing a sequence of tasks, using variables and some memory operations to achieve the goal.
OK, what is declarative programming then? In declarative programming you don’t have to describe how to achieve the “end” result, but you just declare what your “end” result should be. It’s like building UML diagrams for your database while hiding actual implementation under the hood. SQL itself also follows a declarative programming paradigm though, because SQL is a domain-specific language, which serves as an interface for your relational database and it hides the low-level implementation of your database. Infrastructure configuration languages like HCL, Terraform Language, and markup languages, such as HTML, XML, XAML are examples of declarative programming. You don’t write algorithms or workflows in HTML, but you declare what your page should display.
In programming languages like Python, you can combine both declarative and imperative programming styles. You can write the OOP section following an imperative approach, and you can also write small functions and declare what your program should perform by merging those functions(a.k.a. functional programming).
Top comments (0)