views:

2137

answers:

13

I keep hearing this term tossed around in several different contexts. What is it?

+4  A: 

Crudely put, it means specifying logic in terms of rules and facts, rather than as instructions.

skaffman
+1  A: 

This would be a good starting point.

SCdF
+20  A: 

Declarative programming is when you write your code in such a way that it describes what you want to do, and not how you want to do it. It is left up to the compiler to figure out the how. Examples of declarative programming languages are SQL and Prolog.

1800 INFORMATION
You still to figure out "how" to tell the computer "what" you want :)
hasen j
+3  A: 

It's a method of programming based around describing what something should do or be instead of describing how it should work.

In other words, you don't write algorithms made of expressions, you just layout how you want things to be. Two good examples are HTML and WPF.

This Wikipedia article is a good overview: http://en.wikipedia.org/wiki/Declarative_programming

Kevin Berridge
Minor quibble. WPF is a library, not really a language or a paradigm. I think you really meant to say XAML is an example of declarative language.
Nick
And how would you describe programming using a library/framework?
Gutzofter
+1  A: 

Declarative programming is the picture, where imperative programming is instructions for painting that picture.

You're writing in a declarative style if you're "Telling it what it is", rather than describing the steps the computer should take to get to where you want it.

When you use XML to mark-up data, you're using declarative programming because you're saying "This is a person, that is a birthday, and over there is a street address".

Some examples of where declarative and imperative programming get combined for greater effect:

  • Windows Presentation Foundation uses declarative XML syntax to describe what a user interface looks like, and what the relationships (bindings) are between controls and underlying data structures.

  • Structured configuration files use declarative syntax (as simple as "key=value" pairs) to identify what a string or value of data means.

  • HTML marks up text with tags that describe what role each piece of text has in relation to the whole document.

C. Lawrence Wenham
Although XML is declarative, I wouldn't go so far as to say it's declarative *programming* simply because there are no active semantics associated with the markup. Saying that something is an address does not help with figuring out what you want to do with it.
HenryR
There has to be an underlying context (domain?) in which the declarative program is used. So using XML combined with ANT can be construed as a declarative program.
Gutzofter
A: 

A couple other examples of declarative programming:

  • ASP.Net markup for databinding. It just says "fill this grid with this source", for example, and leaves it to the system for how that happens.
  • Linq expressions

Declarative programming is nice because it can help simplify your mental model* of code, and because it might eventually be more scalable.

For example, let's say you have a function that does something to each element in an array or list. Traditional code would look like this:

foreach (object item in MyList)
{
   DoSomething(item);
}

No big deal there. But what if you use the more-declarative syntax and instead define DoSomething() as an Action? Then you can say it this way:

MyList.ForEach(DoSometing);

This is, of course, more concise. But I'm sure you have more concerns than just saving two lines of code here and there. Performance, for example. The old way, processing had to be done in sequence. What if the .ForEach() method had a way for you to signal that it could handle the processing in parallel, automatically? Now all of a sudden you've made your code multi-threaded in a very safe way and only changed one line of code. And, in fact, there's a an extension for .Net that lets you do just that.

  • If you follow that link, it takes you to a blog post by a friend of mine. The whole post is a little long, but you can scroll down to the heading titled "The Problem" and pick it up there no problem.
Joel Coehoorn
+2  A: 

Describing to a computer what you want, not how to do something.

denonde
+1  A: 

As far as I can tell, it started being used to describe programming systems like Prolog, because prolog is (supposedly) about declaring things in an abstract way.

It increasingly means very little, as it has the definition given by the users above. It should be clear that there is a gulf between the declarative programming of Haskell, as against the declarative programming of HTML.

Marcin
A: 

imagine an excel page. With columns populated with formulas to calculate you tax return.

All the logic is done declared in the cells, the order of the calculation is by determine by formula itself rather than procedurally.

That is sort of what declarative programming is all about. You declare the problem space and the solution rather than the flow of the program.

Prolog is the only declarative language I've use. It requires a different kind of thinking but it's good to learn if just to expose you to something other than the typical procedural programming language.

paan
+7  A: 
Jörg W Mittag
A: 

I'd explain it as DP is a way to express

  • A goal expression, the conditions for - what we are searching for. Is there one, maybe or many?
  • Some known facts
  • Rules that extend the know facts

...and where there is a deduct engine usually working with a unification algorithm to find the goals.

epatel
A: 

OK, because somebody didn't like my previous answer, let me try to restate it. The property of code known by the adjective "declarative" needs a more measurable definition than examples or loose descriptions (i.e. "SQL", or "not imperative").

In my opinion, there is a type of code that is consistent with currently accepted ideas, such as Don't Repeat Yourself (DRY). This is measurable and does not depend on the form or syntax or semantics of the code. It says, in the ideal extreme, any single change in requirements should be implementable with a single insertion, deletion, or replacement of source code text. To the extent that code approaches this ideal, it minimizes development effort and bugs, in a measurable way. Just examine the differences recorded by the SCCS system, and count them. I hope it is agreed that this "edit count" is a good thing to minimize.

I think the property that "declarativity" is after is to minimize the edit count, and I think this necessarily makes the code more of a problem description than a problem solution. That's why I define "declarative" as the property of minimizing that count. I prefer that over "not imperative" or "functional" or "logical" because those are squishy and don't get at the important property.

I think we should not insist that declarative code be especially readable by the uninitiated. If it is, that's fine, but we should not be surprised if an up-front learning curve is the price of a long-term benefit. In this it is like any language or technique.

Mike Dunlavey
+1  A: 

It may sound odd, but I'd add Excel (or any spreadsheet really) to the list of declarative systems. A good example of this is given here.

Lunatik