# A Declarative Approach to Services

## An example

Instant translate with XForms

## The Cray

In the 1980's when PCs were new, the most powerful computers were Crays, huge machines with extensive cooling.

And people used to say "One day we will all have a Cray on our desk".

## Now

So do we have a Cray on our desk now?

## Now

So do we have a Cray on our desk now?

Sure: in fact current workstations are about 120 Craysworth.

## Now

So do we have a Cray on our desk now?

Sure: in fact current workstations are about 120 Craysworth.

Even my previous mobile phone was 35 Craysworth...

## We live in an exponential world

To demonstrate Moore's Law

Take a piece of paper, divide it in two, and write this year's date in one half:

## Paper

2007

Now divide the other half in two vertically, and write the date 18 months ago in one half:

## Paper

2007
2005

Now divide the remaining space in half, and write the date 18 months earlier (or in other words 3 years ago) in one half:

## Paper

2007
2005
2004

Repeat until your pen is thicker than the space you have to divide in two:

## Paper

2007
2005
2004
2002
2001
1999
1998
1996
95
93
92
90
89
87

This demonstrates that your current computer is more powerful than all other computers you have had put together (and the original Macintosh (1984) had a tiny amount of power.)

## So how are we using all that extra power?

Mostly for pixel pushing.

Most computers spend most of their active life idle.

## Our Idle Computers

Why aren't we using the extra power to make people's (our!) lives better?

## The cost of producing applications

According to the DoD, 90% of the cost of software is debugging.

According to Fred Brookes, in his classic book The Mythical Man Month, the number of bugs increases quadratically according to code size: L1.5.

In other words, a program that is 10 times longer is 32 times harder to write.

Or put another way: a program that is 10 times smaller needs only 3% of the effort.

## Constructing Applications

The problem is, in comparison to web sites, no one writes applications except programmers.

Mostly because they use a declarative programming model.

The nice part about declarative programming is that the computer takes care of all the boring fiddly detail.

## XForms

XForms is a recent technology from W3C.

As its name suggests, it comes from an analysis of HTML Forms, and the needs of the community for Forms support.

However, because of the generality of the resulting design, it turns out to be useful for a lot more than just Forms.

## XForms: the essence

XForms uses essentially a Model-View-Controller approach, so that the memory model is completely separated from the input/output.

The model specifies the values being collected (the instance), and their related logic:

• Types, restrictions
• Initial values, Relations between values

Input/output controls then bind to values in the instance

## Intent-based controls

The input controls, rather than expressing how they should look (radio buttons, menu, etc), express their intent (for instance: "this control selects one value from a list").

You then use styling to say how they should be represented, possibly with different styling for different devices (as a menu on a small screen, as radio buttons on a large screen).

Three possible representations of the control "select one from a list":

Colour: red green blue

## The instance

The place where values are stored is called an 'instance' (there can be several of them). Each is an XML document:

```<instance>
<data xmlns="">
<width>640</width>
<height>480</height>
<depth>8</depth</depth>
<bits/>
</data>
</instance>```

It has a spreadsheet-like computation model:

```<bind nodeset="bits"
calculate="../height * ../width *
../depth"```

## External instances

An instance can be initialised by anything addressable with a URI, which includes local files, but also anything over the net:

`<instance src="http://www.example.com/values"/>`

## Submission

The values in an instance can be submitted over the net:

`<submit action="http://www.example.com" ... />`

but a nice feature is that the result of the submission can:

• Replace the whole document (as in HTML)
• Replace any instance, or a part of an instance
```<submit action="http://www.example.com"
replace="instance"
instance="results"/>```
• Be ignored
```<submit action="http://www.example.com"
replace="none"/>```

## XForms for services

So we have:

• A memory model
• Input/output
• Local and remote computation

Everything we need for service-oriented computing.

## SOAP

Of course SOAP messages are also just XML instances, so XForms has everything you need to create, send, receive and unpack SOAP envelopes.

## An Example: Flickr

Flickr (use Firefox)

Flickr (IE with plugin)

## Case study: Cordys

Cordys are building a system to deal with legacy systems.

Essentially they build a wrapper around each legacy machine that makes the machine talk SOAP.

Then they connect all machines with an XML data bus carrying the SOAP messages.

All messages contain all the transaction details: so the system is stateless (and therefore scalable).

Originally they used Ajax to provide the user interface, but started losing programmers to nervous breakdowns: Javascript has no engineering support.

They now use XForms.

More than 200K of Javascript

## Google maps as Declarative Application

Although the example shown above is not quite complete, it does more than Google maps does and yet it is only 25Kbytes of code (instead of the 200+K of Javascript), and was written by one person.

Remember, empirically, a program that is an order of magnitude smaller needs only 3% of the effort to build.

## Another data point: big machines with complicated interfaces

A company makes BIG machines (walk in): user interface needs 5 years, 30 people

This became: 1 year, 10 people with XForms

10 person years instead of 150 person years.

## Implementations of XForms

Different types of implementation:

Many big players have done implementations, e.g.

• Novell
• Oracle
• IBM
• Sun (on Openoffice/Staroffice)
• Mozilla

## Conclusion

The advantages of the XForms approach include:

• Accessible applications: Because of the model-view-controller approach, it is just as easy to bind an accessible interface to the data as a purely visual one.
• Device independent applications: Likewise, because of the late binding to the data, you can bind device-dependent interfaces just as easily. (This is comparable with the advantages of using content+stylesheets in conventional XML and XHTML)
• Re-use: If someone develops a widget, it is available to all applications, and isn't hard-wired into an app (again, like stylesheets)
• Much less coding: Declarative programming is well known for requiring much less coding, mainly because you don't have to worry about all the fiddly administration involved in traditional procedural programming.