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

RE: Declarative programming requires a different mindset

  • From: "Costello, Roger L." <costello@mitre.org>
  • To: "xml-dev@lists.xml.org" <xml-dev@lists.xml.org>
  • Date: Wed, 17 Mar 2010 07:59:18 -0400

RE: Declarative programming requires a different mindset
Hi Folks,

I've had some additional insights into declarative programming that I would like to share.

The three main constructs used in programming are:

  - Loops 

  - Conditional

  - Sequence

I have always thought of these constructs in terms of "Do this, then that." That is, I thought these are imperative constructs only. But now I understand that these are also declarative constructs. 

Here's how to declaratively think about the constructs.


Ways of Thinking about Loops

Example Loop: 

    for $i in //Member return A, B, C

Imperative: loop through each <Member> element and do actions A, B, C.

Declarative: this A, B, C description applies to each <Member> element.


Ways of Thinking about Conditional Statements

Example Conditional: 

    if (empty(//Member[999])) then A else B

Imperative: if there is no 99th <Member> element then do action A, else do action B.

Declarative:  this description A is for the case where the old document does not contain a 999th <Member> element. And this description B is for the case where the old document _does_ contain a 999th <Member> element.


Ways of Thinking about a Sequence of Statements

Example Sequence: 

    delete node $i/Name,
    replace node $i/@id with 
            attribute {'id'} {data($i/Name)}

Imperative: delete the <Name> element, then replace the value of the id attribute with the value of the <Name> element. (Obviously, this is nonsensical in an imperative programming.)

Declarative: in the new document there will be no <Name> element. In the new document the id attribute's value will be the value of the <Name> element in the old document.


To recap: declarative programming focuses on describing what you want in the new document, relative to what was in the old document. How a machine takes that declarative description and accomplishes it, is transparent.

Question: When you create XSLT transforms or XQueries, how do you maintain a declarative mindset, and avoid slipping back into an imperative mindset?

/Roger


[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.