Show understanding of the characteristics of a number of programming paradigms: Declarative
20.1 Programming Paradigms
Declarative Programming
What is Declarative? 🧩 Declarative programming tells the computer what you want to achieve, not how to do it. Think of it as giving a final picture instead of step‑by‑step instructions.
- Focus on desired outcomes (e.g., “display a list of students”).
- Less control over execution flow – the language/runtime decides.
- Often used for data‑driven tasks: SQL, HTML, CSS, functional languages.
- Can lead to simpler, more readable code when the problem fits the paradigm.
Analogy: Cooking a Meal vs. Following a Recipe
Imagine you want to make a pizza. Imperative: You’ll be told step by step: “Roll the dough, add sauce, sprinkle cheese, bake for 12 minutes.” Declarative: You simply say, “I want a pizza with cheese and tomato sauce.” The kitchen (runtime) figures out the steps. 🍕
Common Declarative Languages & Tools
- SQL – Querying databases:
SELECT name FROM students WHERE grade > 90; - HTML/CSS – Marking up and styling web pages.
- Functional languages (Haskell, Elm) – Emphasise pure functions and immutability.
- Configuration languages (YAML, JSON) – Describe desired system state.
When to Use Declarative?
- When the problem is about data manipulation or specifying structure.
- When you want readability and maintainability over fine‑grained control.
- When the underlying system can optimise execution (e.g., database query planner).
Declarative vs. Imperative – Quick Comparison
| Aspect | Declarative | Imperative |
|---|---|---|
| Focus | What you want | How to do it |
| Control Flow | Hidden, handled by runtime | Explicit loops, conditionals |
| Typical Use | Data queries, UI markup, functional code | Algorithms, system control, game logic |
| Readability | Often higher for simple tasks | Depends on programmer skill |
Exam Tips for Declarative Paradigms
1. Identify the paradigm: Look for keywords like query, select, render, compose. These hint at declarative code. 🚀
2. Explain the difference: Use the table above or a short bullet list to show how declarative focuses on what versus imperative focusing on how. 📊
3. Give an example: Write a simple SQL query or an HTML snippet that achieves a clear goal. Show the declarative statement and explain its intent. 📄
4. Discuss advantages: Mention readability, maintainability, and optimisation by the runtime. Also note the limitation: less control over performance details. ⚖️
Revision
Log in to practice.