[Home] [By Thread] [By Date] [Recent Entries]
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] |

Cart



