Learn something new every day
More Info... by email
Declarative programming is a way of specifying what a program should do, rather than specifying how to do it. Most computer languages are based on the steps needed to solve a problem, but some languages only indicate the essential characteristics of the problem and leave it to the computer to determine the best way to solve the problem. The former languages are said to support imperative programming whereas the latter support declarative programming.
One can consider the following example from SQL to get all the sales regions where profit exceeds a certain number, say, $700 US Dollars (USD), from the sales data.
Select Region, Profit from Sales where Profit > 700
This statement does not indicate what the SQL system has to do in order to get the data. The SQL system can go through each sales record and determine whether the condition is satisfied, or, it can quickly obtain the top few records from presorted data. The statement only indicates the nature of the desired data.
Prolog is a declarative programming language which indicates the logical relations between entities.
ancestor(M, C) :- mother(M, C)
ancestor(X, Z) :- mother(X, Y), ancestor(Y, Z)
The above program indicates truisms. If M is the mother of C then M is an ancestor of C. If X is the mother of some person Y who is an ancestor of Z, then X is an ancestor of Z. Given this and some mother-child facts, the program can answer questions about the whole maternal family tree.
During program execution, the Prolog system builds up a number of true statements, thus creating a knowledgebase. This knowledgebase is searched efficiently on its own. The creator of the above program does not specify how to search.
HTML and CSS are declarative programming languages. For instance, the HTML example <table border="1">, indicates a thin border. A CSS example is color: blue. This specifies the text color. As can be seen in these examples, HTML and CSS specify what should appear on a web page but not how to do so.
The advantage of declarative programming languages is mainly two-fold. The programs are concise; this makes it easy even for non-programmers to obtain solutions. In the SQL example above, an analyst or business support person can get the desired information. Similarly, laypersons can write acceptable web pages with simple HTML and CSS commands.
The second advantage of the declarative programming model is that repetitive imperative code that indicates how to solve things is provided in the computer system behind the scenes. Such code can be made highly efficient and can incorporate the best ideas from computing. It can take advantage of parallelism.
@NathanG - There’s a guy I work with who’s really excited about Ruby programming because of its declarative nature. He says it’s more concise than traditional programming languages. I’ve seen some of the code, and it just looks like a different language.
I’m not sure I understand what the hype is about but I do anticipate these types of languages will become more common in introductory computer courses.
Declarative programming requires a shift in the way that you think. Examples of declarative programming language technologies include Windows Presentation Foundation, which is the new technology from Microsoft that is supposed to be the de facto standard for future Windows development.
Windows Presentation Foundation is completely XML-based, which makes it a little like programming in HTML. That can be a bit disorienting at first. However, the philosophy behind it is to separate function and design. A Windows “form” in the WPF model is an XML document that a designer can modify easily and then send back to the developer to update the code. There is no binary form file as there used to be in the other Rapid Application Development tools.
One of our editors will review your suggestion and make changes if warranted. Note that depending on the number of suggestions we receive, this can take anywhere from a few hours to a few days. Thank you for helping to improve wiseGEEK!