Declarative vs Imperative Programming

Opeyemi Olorunleke
3 min readAug 20, 2023

--

Artist vs Soldier

Declarative programming is an approach to programming that focuses on stating the desired outcome, rather than providing explicit step-by-step instructions to achieve it. On the other hand, Imperative programming is a programming style in which specific instructions are directly written to address a problem.

In other words, Declarative tools require developers to outline what needs to be done, whereas imperative tools require developers to detail how the tasks should be executed.

For example, To solve a problem of reaching point A from B.

Imperative: you would create step-by-step instructions to navigate from point A to B, including avoiding obstacles, adjusting speed, and braking. There is no underlying system.

Declarative: instruct it to move from A to B. An underlying system will convert your declarative instruction to imperative code because all code must be imperative for a computer to understand.

Declarative programming extends the capabilities established by imperative programming. However, it allows developers to concentrate on solving problems rather than getting entangled in the complexities of code arrangement.

Declarative programming uses

It is typically found where code flexibility is important such as databases, configuration management software and recently User Interface design

Databases

Structured Query Language (SQL) is a declarative language used to manage (create, read, update or delete items) an SQL Database(DB). This SQL command select * from customers where country="France" fetches customers from the country of France (in the DB) through the underlying SQL system. The above code does not contain a “How”, freeing the DB administrator to focus on more essential responsibilities.

User Interface design

The main purpose of a User Interface is to convey the current state of a system. This “state” refers to the condition of the system at a specific moment. Owing to the inherent complexity of applications and various factors like system configuration change, it is challenging to anticipate and reflect all potential states effectively.

UI development takes two stages, one is design and state management.

For example, in imperative UI design, you might write code like:

button = createButton()
button.setText("Click me")
button.setPosition(100, 100)
button.onClick(function() {
// Handle button click event
})

in declarative UI design:

<Button 
text="Click me"
position={[100, 100]}
onClick={handleButtonClick}
/>

In declarative approach, State changes are automatically propagated based on the declared relationships, reducing the need for direct management while State changes are explicitly triggered and managed by the developer, in imperative approach

The conventional/imperative approach of manually handling UI “state” frequently gives rise to errors that gradually evolve into challenges of maintenance, testing, and scalability.

Android Views uses a combination of both approaches, however, fully declarative approach is poised to define the future of UI design because it’s underlying system is utilised for both UI design and state management.

Web UI design (HTML and CSS) has always been declarative, but the brilliance of this approach has recently been embraced in technologies like Android (Jetpack Compose), iOS (SwiftUI) and cross platform development tools (Xamarin’s C# Markup and Flutter’s Declarative UI).

Conclusion

Declarative programming is the best approach for solving problems that require handling lots of flexibility while Imperative is apt for critical tasks like business logic.

--

--

Opeyemi Olorunleke

Passionate about solving problems and building scalable apps. I write about AI/ML, software development and personal growth. FOLLOW to not miss new post