Declarative Programming
Declarative programming is a paradigm that focuses on what the program should accomplish rather than how to accomplish it.
Core Concept
Key Characteristics
- Express logic without describing control flow
- Focus on the end result
- Let the system figure out execution
- Higher level of abstraction
Examples
SQL (Declarative)
-- Declarative: What we want
SELECT name, age
FROM users
WHERE age > 18
ORDER BY name;
-- System figures out HOW to retrieve data
HTML/CSS (Declarative)
<!-- What we want to display -->
<div class="card">
<h1>Title</h1>
<p>Content</p>
</div>
<style>
.card {
padding: 20px;
background: white;
border-radius: 8px;
}
</style>
React (Declarative UI)
function UserList({ users }) {
return (
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}
Advantages
✅ More Readable: Intent is clear
✅ Less Error-Prone: No manual control flow
✅ Optimizable: System can optimize execution
✅ Higher Abstraction: Focus on business logic
When to Use
- Database queries (SQL)
- Configuration files (YAML, JSON)
- UI layouts (HTML/CSS)
- Build specifications (Make, Gradle)
- Rule engines
📋