Declarative programming is a method to abstract away the control flow for logic required for software to perform an action, and instead involves stating what the task or desired outcome is.
Declarative programming is a high-level programming concept, which is the opposite of imperative programming. It is typically found in databases and configuration management software, paired with a domain-specific language (DSL). Declarative models rely on preconfigured capabilities in the language to accomplish a task without explicit case-by-case instructions on what steps to take.
Declarative programming vs. imperative programming
Declarative programming relies on underlying components of a given language to carry out the necessary steps to reach the stated outcome. In declarative programming, typical programming constructs such as loops and if/then conditions do not exist, because they are instructional.
Declarative programming focuses on the end result, while imperative programming focuses on how to get there. For example, when you jump in a taxi, you declare to the driver where you want to go. You don't tell him how to get there by providing turn-by-turn directions. The turn-by-turn directions are akin to imperative programming.
Declarative programming builds on the capabilities developed by imperative programming, but enables the developer to focus on problem resolution rather than intricacies of code setup.
Many programming languages allow for imperative and declarative programs to be combined. For example, the Java programming languages provides the ability for developers to place annotations on code, which adds declarative capabilities to traditionally developed code. In the code snippet below, the @Enumerated annotation indicates that the property named clientGesture is an enumerated type that should be persisted to the underlying database as a text string:
private Gesture clientGesture;
Other modern programming languages have similar declarative programming facilities.
How declarative programming works
Declarative programming relies on constraints and logic to define the setup and outcome.
Constraints define the properties that are true in a given programming scenario, such as equalities and inequalities. They are how variables relate to each other. Constraints are either embedded in the language or set up in software libraries.
Similarly, logic programming expresses facts and rules about the domain in which the developer is working. Control and logic are separated for this form of programming to work. Constraint logic programming combines both the above practices.
Declarative programming is usually practiced with a DSL, because the control flow must exist separate from the logic, embedded in the language itself.
Declarative programming uses
The relational database is a popular declarative programming concept. A programmer writes statements in a DSL called Structured Query Language (SQL) to control the database. A SQL query that pulls a set of records from a database does not use loops or conditional logic. Instead, it includes: SELECT * FROM <TableName>, which tells the database to return the requested records.
Configuration management tools such as Chef, Puppet and Microsoft PowerShell Desired State Configuration (DSC) all use the declarative programming approach. Each tool is built on an iterative language, including Ruby, Python and PowerShell. The user defines what they need the tool to do via the DSL, and the tool accomplishes the task without requiring information about how to do it.
Declarative programming example
In configuration management, if an administrator needs to copy a file to a server, in pseudocode, an iterative approach follows this construction, with if/then statements:
A declarative programming alternative, in pseudocode, follows a different setup:
There is no conditional logic in the snippet of declarative code. Instead, it displays what action the user wants to happen, and what servers are involved in the action.