5

I read the following paragraphs about architecture from Microsoft .Net: Architecting Applications for the Enterprise, by Dino Esposito and Andrea Saltarello:

Whenever you take a declarative approach to coding rather than an imperative approach, you are delegating some responsibilities to some other layer of code—a layer that you don't control entirely. So you're ultimately hoping that the intermediate tool will understand you correctly and won't encounter any trouble of its own along the way. In software development, delegating to a third party makes development faster, but you should guarantee that users get exactly what you want them to.

This doesn't mean that you shouldn't trust Visual Studio 2008 or similar wizard-based products. The point is something else entirely. In a large system, likely being developed by several teams, classic declarative programming just doesn't work. You need to have in your presentation a layer of code that decides what to display, where to read settings, and how to apply them. Declarative programming is still a great option, but only if you write the engine and the wizards.

Can someone explain to me in simple words what exactly declarative and imperative programming are?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • see this [SO post][1] for a compaison of f# and c# [1]: http://stackoverflow.com/questions/952318/what-are-the-benefits-of-using-c-sharp-vs-f-or-f-vs-c – Ashok Padmanabhan Nov 19 '11 at 21:18
  • @AshokPadmanabhan: Only barely applies, because it's about C# vs. F#, and only partly about imperative programming vs *functional* programming (which is a rather specific subset of declarative programming). –  Nov 19 '11 at 21:20
  • How are wizards and declarative programming even related? – CodesInChaos Nov 19 '11 at 21:43

3 Answers3

7

Declarative - I will describe what I want you to do, you figure it out (SQL, XSLT).

Imperative - I will tell you exactly what to do, one step at a time (C#, Java).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    SQL sounds more functional than declarative to me. I think XSLT is just functional programming with an xml syntax, but I don't know enough of it to be sure. – CodesInChaos Nov 19 '11 at 21:45
  • @CodeInChaos - Both have been described as declarative. I just took a look at the wikipedia entries for both, which agree with this. – Oded Nov 19 '11 at 21:47
7

Examples.

Declarative programming:

<asp:DropDownList runat="server" DataSourceID="ObjectDataSource1" />
<asp:ObjectDataSource runat="server" ID="ObjectDataSource1" ItemUpdating="..." />

Imperative programming:

ObjectDataSource source = new ObjectDataSource();
source.ItemUpdating += ...;

DropDownList list = new DropDownList();
list.ID = "";
list.DataSource = source;
list.DataBind();
Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
2

Declarative programming - What should be done

Imperative programming - How what you want should be done.

Declarative programming requires developers to say what is to be done. Imperative programming requires developers to define step by step how code should be executed.

Example: LINQ in C# is declarative.

Read here for more: http://www.informit.com/articles/article.aspx?p=1330154&seqNum=4

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 1
    LINQ is plain functional programming IMO. Databinding, validation attributes etc. would be what I call declarative. – CodesInChaos Nov 19 '11 at 21:47
  • 1
    @CodeInChaos - Functional Programming and declarative are not mutually exclusive. And LINQ is declarative because you say what you want, and the system knows how to get it, be it from xml, sql, etc. – manojlds Nov 19 '11 at 21:50