I assume that when I write code, I will make mistakes and lots of them.
I define mistakes as any discrepancy between what I mean versus what I
say.
When you write code you always say what you say. That's inevitable. But
we don't always say what we mean. And it's critically important we find
ways to express our intent.
Whenever I write an XSLT function or template mode, my first order of
business is to write lines of comment that express in regular prose what
I'm expecting in input, and what I'm expecting in my output, and a
description of edge cases, plus examples of input + output.
Next, whenever I run into an XPath construct like the one you mentioned,
I bind it to a variable, whose name expresses my intention. Something
like
<xsl:variable name="post-initial-chapters"
select="//book/chapter[position() ge 1]"/>
In that construct it's much easier to identify my typo, because it's
bound to the variable name. Well, if it is "post-initial" how could it
ever be value 1? No guarantee I'll catch the problem, but almost always
it's easier to find the mistake later.
Finally, as I write something I also develop a regression test library.
You can use XProc, Schematron, and a variety of technologies to do this,
really depends on the applications. But set these test suites up to
express your intentions. When a validation error strikes, those
expressions of intent will help you diagnose.
I was once a music major, with a passionate interest in composition. My
music theory professor told us all (now deeply engraved on my heart),
"There are are only two types of people who write music in ink. Fools
and geniuses."
It behooves us to code in pencil.
How to avoid making mistakes? Is it even possible?
Joel
--
Joel Kalvesmaki
Director, Text Alignment Network
http://textalign.net