[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: RE: Declarative programming requires a different mindset

  • From: Greg Hunt <greg@firmansyah.com>
  • To: "xml-dev@lists.xml.org" <xml-dev@lists.xml.org>
  • Date: Sun, 18 Apr 2010 02:47:32 +1000

Re:  RE: Declarative programming requires a different mindset
This list seems to have gone the way I remember the patterns mailing lists going.  Apart from announcements, the signal to noise ratio of this list is declining sharply and on-topic, non-announcement messages are now days apart.  

Perhaps this particular thread ought to be elsewhere?  It stopped being primarily about XML (if it ever was) some time ago.  



On Sun, Apr 18, 2010 at 2:01 AM, Costello, Roger L. <costello@mitre.org> wrote:
Hi Folks,

Today I would like to explore the "declarativeness scale," how to nudge programs toward the declarative side of the scale, and how to get into the declarative mindset.

We have defined a declarative program as one that, "describes the mapping between the input and the output."

   Assertion: rule programming is especially well suited
   to declarative programming.

Here is a design pattern for rule-based declarative programming:

Rule:
   Identify an input circumstance/condition.
   Describe the mapping from input to output.

XSLT is well suited for implementing this design pattern:

Each XSLT template rule identifies an input circumstance/condition using match="...".  The content of <xsl:template> describes the mapping between the input and the output.

Thus, one strategy for getting into the declarative mindset is to treat all problems as one of expressing rules that describe the mapping between the input and output.

   Assertion: there is a declarativeness scale. On one end is
   declarative and on the other end is imperative.

Here is a graphic of the declarativeness scale:

http://www.xfront.com/XML-Declarative-Programming/declarativeness-scale.gif

   Assertion: there are degrees of declarativeness.

Let's consider an example.

Suppose the input is a list of books:

<BookList>
     <Book>
           <Title>Don't Make Me Think</Title>
           <Author>Steve Krug</Author>
     </Book>
     <Book>
           <Title>Economics in One Lesson</Title>
           <Author>Henry Hazlitt</Author>
     </Book>
     <Book>
           <Title>Guns, Germs, and Steel</Title>
           <Author>Jared Diamond</Author>
     </Book>
</BookList>

The output is to be an HTML table, with a row for each book.


APPROACH #1: create a single XSLT template rule:

<xsl:template match="BookList">
     <html>
           <head>
                 <title>Book List</title>
           </head>
           <body>
                 <table border="1">
                     <tr>
                           <th>Title</th>
                           <th>Author</th>
                     </tr>
                     <xsl:for-each select="Book">
                          <tr>
                                <td><xsl:value-of select="Title" /></td>
                                <td><xsl:value-of select="Author" /></td>
                          </tr>
                     </xsl:for-each>
                 </table>
           </body>
     </html>
</xsl:template>

There is one rule which does everything. It is a course-grained rule. My intuition is that, "the more fine-grained a rule is, the more declarative it is." I don't have any evidence to support this intuition. Perhaps you do?


APPROACH #2: create an XSLT template rule for each input:

<xsl:template match="BookList">
     <html>
           <head>
                 <title>Book List</title>
           </head>
           <body>
                 <table border="1">
                     <tr>
                           <th>Title</th>
                           <th>Author</th>
                     </tr>
                     <xsl:apply-templates />
                 </table>
           </body>
     </html>
</xsl:template>

<xsl:template match="Book">
     <tr>
           <xsl:apply-templates />
     </tr>
</xsl:template>

<xsl:template match="Title">
     <td>
           <xsl:value-of select="." />
     </td>
</xsl:template>

<xsl:template match="Author">
     <td>
           <xsl:value-of select="." />
     </td>
</xsl:template>

Each rule describes the mapping between the input and the output. For example, the last rule says, "An Author element in the input is mapped to an HTML <td> element and the content of the <td> element is the value of Author."

Wow! That's really declarative!

Here's where I place Approach #1 and Approach #2 on my declarativeness scale:

http://www.xfront.com/XML-Declarative-Programming/many-vs-one-rule-on-the-declarativeness-scale.gif


QUESTIONS

1. What strategies do you use to push your programs toward the declarative side?

2. Do you have any research results that correlate the size of a rule to its declarativeness?

/Roger

_______________________________________________________________________

XML-DEV is a publicly archived, unmoderated list hosted by OASIS
to support XML implementation and development. To minimize
spam in the archives, you must subscribe before posting.

[Un]Subscribe/change address: http://www.oasis-open.org/mlmanage/
Or unsubscribe: xml-dev-unsubscribe@lists.xml.org
subscribe: xml-dev-subscribe@lists.xml.org
List archive: http://lists.xml.org/archives/xml-dev/
List Guidelines: http://www.oasis-open.org/maillists/guidelines.php




[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.