# Declarative Statement

A declarative statement has no timing of execution.

The statement list.push('item') executes at a specific point in time. When it executes, it adds an item to the end of our list. If the list is then sorted that item would likely change places in the list. If, however, the list had already been sorted the sort operation would have to affect on the outcome of our push. The push operation can execute multiple times, and each time in will add another item to the list. It could also never execute, having no affect. list.push is an imperative operation—an operation which executes at distinct points in time—and it is can lead to complexities.

Consider a hypothetical system which allows us to define a reactive list. This list would be defined with some conditions, and will be kept up to date by some magic system so that it always satisfies those conditions.

var oddBalls = from ball in balls
where ball.number % 2 == 1
select ball;

In fact, this system does exist. This is an example of using LINQ in C# with Reactive Extensions. LINQ is the language extension which allows us to type these semantics, (from, where, and ball) but Reactive Extensions is the system which keeps list up-to-date. RX, as it's called, will observe balls for changes, and will insert or remove items from oddBalls as necessary so that it is always a filtered variant of the current balls.

Reactive Extensions, unfortunately, is not actually fully declarative. Luckily, there is another, more common example of declarative code—one which is so simple it is often not considered to be code at all.

CSS is a purely declarative language.

.balls:nth-child(odd) {
background: red;
}

This statement is similar to the RX statement, but it satisfies all requirements to be considered declarative.

• A CSS statement is perpetually true. All odd numbered child elements will always be red. If the list of child elements changes, or if the parent loses its balls class, the children will be updated. This is often referred to as being Reactive. The system observes any mutable inputs for changes and updates the effect of the Declarative Statement to match.
• CSS rules are handled without side-effects. Any execution that may take place behind the scenes does not affect any other part of the program.
• CSS rules are Deterministic. Given the same HTML, our rule will always color the same elements red. A statement that "executes" may incorporate time or randomness into its execution, producing different results even under the same initial conditions, but a Declarative Statement never executes. Its outcome only depends on the values specified in its computation.
• CSS rules exist throughout the life of the program. CSS isn't executed from top-to-bottom, collecting rules along the way. All rules exist at all times. Granted, this isn't exactly true; the rules are only applied once the file is downloaded, but CSS is still written as if all rules are always applied.
• A CSS rule cannot be edited by later CSS rules. If a CSS rule could be altered by other rules later in the file, the rule itself would be a mutable, and the later CSS rules would be considered imperative.

As I've said, CSS's declarative nature makes its execution so simple that it's often not considered to even be a programming language. Imagine, however, that CSS was imperative. This rule would need to observe all changes to the document structure—adding and removing elements, changing order, updating classes. The code would then need to update any newly found odd-numbered children of a .balls element with a red background. Much harder than all of that, our imperative code would need to reverse the effect on any balls which stopped being odd. This operation could not simply set the background of the element to the default, but would need to scour the existing HTML elements and CSS rules for whichever rule might now need to be applied. These operations would be incredibly expensive, and very difficult to optimize.

Fortunately, CSS is declarative, and as such we can write our simple little statement, and get on with our simple little lives.

https://foldoc.org/declarative%20language

#complexity #software