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