Edexcel International Advanced Level Computer Science, point 5.4.2
Definition
The declarative programming paradigm describes what result is wanted, or what is true about the problem, instead of listing the steps for getting it. The language or system works out how to produce the result.
Function
It lets the programmer concentrate on the problem and leave the method to the system, which suits tasks such as querying data, describing layouts and solving logic puzzles.
Characteristics
- The programmer writes facts, rules or queries, or a description of the desired result.
- A declarative program is made of rules (logical relationships or conditions that must hold, such as "if a member's subscription has ended, block their login") and constraints (limits that values must meet, such as "only customers aged 18 or over").
- No explicit control flow (no step-by-step loops written by the programmer).
- Little or no changing state: you describe relationships, not sequences of changes.
- The system (an interpreter, query engine or inference engine) chooses how to find the answer, sometimes by searching and backtracking.
- Examples:
- SQL (databases):
SELECT ... FROM ... WHERE .... - Prolog (logic programming).
- HTML and CSS: describe what a page looks like, not how to draw it.
- Regular expressions: describe a pattern to match.
- SQL (databases):
Why needed
A declarative approach is the better choice when:
- the result matters more than the method: you know what you want, and the system is better placed to decide how to get it;
- the problem can be described as rules and constraints rather than as a sequence of steps;
- the code must be easy to read and maintain;
- the task involves large amounts of data, databases or logic-based systems, where the system can search and optimise far better than hand-written loops;
- the requirements change often: changing a rule or condition is quicker than reworking the flow of a program.
Operation: a database query (SQL)
Task: find the names of the students in form 12A with a mark of 70 or more.
Procedural (how): the programmer writes every step.
- Start with an empty list of names.
- Take each student in turn.
- If the student's form is 12A and their mark is at least 70, add their name to the list.
- Repeat until every student has been checked.
Declarative (what): the programmer describes the result.
SELECT name
FROM Student
WHERE form = '12A' AND mark >= 70;
The SQL doesn't say how to search (loop, index, order). The database engine decides, and may use an index to be faster. This is declarative.
Benefits
- Shorter and clearer code for suitable problems (a query is one statement).
- Focus on the problem, not the method.
- Less chance of control-flow errors, since there are no loops or state to get wrong.
- Changes are simpler: altering the result or a rule doesn't mean reworking the whole flow of the program.
- The system can optimise the way it finds the answer (database indexes, query planning).
- Easier to reason about, because no hidden state changes.
Drawbacks
- Less control: the programmer can't choose the method, so it may be slower or use more memory than hand-written code.
- Harder to debug: you can't step through the system's choices.
- Only suits certain problems (data queries, rules, pattern matching). General-purpose tasks are awkward.
- A different way of thinking, so it can be harder to learn.
- Usually needs a special language or engine.
Procedural and declarative compared
| Procedural | Declarative | |
|---|---|---|
| Describes | How | What |
| Control | Programmer | System |
| State | Changes as the program runs | Little or none |
| Examples | Python, C | SQL, Prolog, HTML/CSS |
| Good for | General-purpose programs | Queries, rules, descriptions |
| Efficiency control | High (programmer tunes it) | Low (the system decides) |
Worked example
Task: a library database has a table Book(Title, Author, Year). (a) Write a declarative query for the titles of books by "Austen" published after 1810, in order of year. (b) Explain why this is declarative.
(a)
SELECT Title
FROM Book
WHERE Author = 'Austen' AND Year > 1810
ORDER BY Year;
(b) The query states what is wanted (the titles, the conditions and the order). It doesn't say how to find them: no loop, no comparison code, no sorting algorithm is written. The database engine decides how to carry the query out.
Exam tip: the key contrast is "what" against "how". Give an example (SQL, Prolog or HTML) and say what the programmer doesn't have to write. For drawbacks, say the programmer has less control over how the answer is found, which can make it less efficient or harder to debug.
Check yourself
What is declarative programming?
A paradigm in which the programmer states what result is wanted or what is true, and the system works out how to produce it.
Give two examples of declarative languages or technologies
Any two of: SQL, Prolog, HTML/CSS, regular expressions.
Give one benefit and one drawback of declarative programming
Benefit: short, clear code that focuses on the problem. Drawback: less control over how the answer is found, so it can be less efficient or harder to debug.
Why is an SQL SELECT statement declarative?
It describes the data wanted and the conditions, but doesn't say how to search, loop or sort; the database engine decides.