6

I've made this Community Wiki because some may think it's open to debate and others might think it's a question of using words to mean what they really mean (in other words, it's a matter of opinion whether it's a matter of opinion).

There's a general question on SO about declarative programming, which has some great answers.

But I'm a bit thrown by this blog post from a Microsoft evangelist.

One advantage of declarative programming is that you can indicate what you would like to do, but not how to do it.

So far, so good - that exactly agrees with the accepted answer on the SO question, in fact.

But then check out the part about "Service implementation",

You can just look at a few dozen lines of xaml code and be able to determine how the WCF Service is configured and how the corresponding workflow is defined.

Having looked at some examples, let me briefly answer that by saying "No, I can't". But rather than flippantly dismissing this stuff, let's look at the docs.

It's taken a while but finally reality has caught up with satire... but that's not the point - of course they aren't seriously suggesting doing this to expose something trivial like addition. Nor am I complaining about the ridiculous verbosity, and the strange idea that anyone would ever write something like that by hand - it looks more like the output of a compiler than a human-readable language.

The puzzle for me is that this is claimed to be "declarative". And yet the core of it is an assignment statement.

There's more here:

Declarative services are defined declaratively in XAML and provide another layer of abstraction. Basically, you create a model of the service by defining what you want the service to do rather than how to do it. The entire service can be defined declaratively, including the implementation of the operations.

So if we say declarative or declaratively three times, that makes it declarative. Gotcha. And if we say the magic phrase "what you want to do rather than how to do it" then we can neglect to notice that in the very next sentence, we are going to specify "implementation of the operations" after all, and so we are going to say precisely how to do it.

The example in that page is:

<wma:Sequence>
    <wma:WriteLine Text ='[String.Concat(String.Concat(String.Concat(String.Concat("Add(", CType(op1, Object)), ","), CType(op2, Object)), ") called")]' />
    <wma:Assign x:TypeArguments="xs:Int32" To="[result1]" Value="[op1 + op2]" />
</wma:Sequence>

Which is to say, the whole thing (including a ton of junk I've cut out from the WF example) is precisely equivalent to:

void Add(int op1, int op2, out int result1)
{
    Console.WriteLine("Add(" + op1 + ", " + op2 + ") called");
    result1 = op1 + op2;
}

So - a block of statements, to be executed in the order they appear, and having side effects. There are of course workflow activity elements for looping (and you can write your own activities if WF doesn't already have your favourite imperative statement). Apparently, "rewriting your code in an unreadable format" is the same thing as "adding a layer of abstraction".

To reiterate, it's not the insane unreadable verbosity I'm complaining about - it's the fact that it is clearly Turing-complete imperative programming happening in the service implementation, so what's the point? Before you know it, we'll be stepping through our workflows in the debugger, trying to figure out which assignment statement mutated which value, or why the loop keeps going round forever.

(The irony is that in the C# version, it's a little more declarative, because we haven't specified how the pieces of string should be concatenated, thus allowing the compiler to generate fewer calls to the Concat method.)

So does writing something in XML make it declarative (as well as less readable)?

Community
  • 1
  • 1
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284

2 Answers2

3

Yet another acknowledgment that using XML in capacities other than exchange format sucks big time.

Service "definitions" in WCF have been declarative from day one. However, separating interface definitions from service definitions (by this I mean ServiceContractAttribute et al) is, IMO, good thing. However, using XML as a programming language really sucks.

I personally feel quite explainable attacks of pure terror when looking at these XML docs.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
0

There's a part of Windows Workflow Foundation that isn't much discussed (outside of Microsoft), that may clarify this for you.

If you create a Workflow project, and look in the toolbox, you'll see a large number of "boxes and lines" that you can drag onto the design surface. You may get the impression that you are meant to create your workflows from this set of tools. That is not the case.

You are expected to author custom activities specific to your problem domain. These are meant to be connected by the various out of the box activities. These are likely to be fairly high-level activities - things like "evaluate insurance policy" or "record patient stay".

A declarative workflow (or service) will be declaring how to put together the problem-specific activities you have available to you.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • "declaring how to put together" - but only in the sense that a method body in C# "declares" how to implement the method. And note the appearance of the word "how", which is what declarative programming is supposed to avoid. What is the line between declarative and imperative, given that the workflow activities carried out by a service are typically imperative steps (however chunky) and so have to operate in an imperative sequence, and glued by imperative out-of-the-box WF activities? – Daniel Earwicker Jul 27 '09 at 12:22