There are occasions, where I have to write an XSLT function and simply use it. For e.g. (this is just an illustration. we could have more function parameters, and a different return type),
<xsl:function name="fn:somefunction" as="xs:boolean">
<xsl:param name="pName" as="xs:string" />
<!-- use $pName and the variable, $someList (defined below) -->
<xsl:sequence select="something.." />
The evaluation of this function also depends on some data/information other than the parameters being passed. This external information on which the function depends, could be a global variable. Say for e.g.,
<xsl:variable name="someList" as="element()+">
In my case, this is a fairly static data (the variable, $someList), and is needed for the evaluation of above function.
I could see two option, on how the function may use an external data ($someList in this case):
1) Have an external data as a global variable (as illustrated above)
2) Supply this data as additional function parameter
I was in a sort of dilemma recently, where I had to decide whether I should go for option 1) or 2).
In my case, I opted for option 1) i.e., the global variable.
I can think of few pros and cons of both of the above options:
1. Having a global variable: This is good, if the external information is fairly static and perhaps has big chunk of data. Having global variable could be also useful, if the data is shared between multiple functions.
2. Having a parameter for the data: This option looks good from the point of view of the principle of composability. Functional programming advocates like this idea. I think, in classical computer science theory, a function (a callable module) is an abstraction which takes some input and produces some output. I think, the notion of functions accessing data which exists outside it's body is a mechanism devised by specific programming languages, and not as such defined by computer science theory. So from the point of view of this idea, having parameter for data is a good option. In fact I would also support this option, as far as possible.
In my case, I was working with XSLT. But I guess, these concepts would apply to many of other programming languages as well.
This topic could turn into a discussion, about how we must write good computer programs.
Any ideas are welcome please.