W3C home > Mailing lists > Public > xproc-dev@w3.org > November 2011

Re: Shadowing of variables..

From: Jostein Austvik Jacobsen <josteinaj@gmail.com>
Date: Tue, 29 Nov 2011 11:27:18 +0100
Message-ID: <CAOCxfQfTayrHRkOQnous4iARJAVs2so4Ou+K1=8YDR95AB7KLQ@mail.gmail.com>
To: Chris Maloney <voldrani@gmail.com>, Geert Josten <geert.josten@dayon.nl>, Florent Georges <fgeorges@fgeorges.org>, vojtech.toman@emc.com, xproc-dev@w3.org
2011/11/28 Florent Georges <fgeorges@fgeorges.org>

> My pragmatic way of looking at it is that this prevents the compiler to
> detect what is most likely an error.

I guess that depends on your style of programming. If you prefer to rename
the variable at each step of the calculation, then it's most probably an
error, but then you would also surely get an XD0023: Undeclared variable
exception anyway, so it wouldn't go by unnoticed.

There's also debugging. Say I have this sequence of steps to calculate $var:

<p:variable name="var-tmp1" select="..."/>
<p:variable name="var-tmp2" select="big expression with multiple references
to $var-tmp1"/>
<p:variable name="var" select="big expression with multiple references to

Now, if I suspect there's an error in the calculation of $var-tmp2, or for
any other reason I want to see what results I get if I skip that step, I
would have to go through the select expression for $var and replace
"$var-tmp2" with "$var-tmp1" throughout. And then, when I want to
reintroduce var-tmp2 again in the sequence of calculations, I would have to
remember to update the references in $var from $var-tmp1 to $var-tmp2 - and
the compiler would definitely not catch those errors if I forget it (could
possibly give a unused variable warning).

Also, maybe I want to add a couple of calculations inbetween var-tmp2 and
var (or split a calculation into multiple calculations); I would again have
to go through $var and change all the references.

On the other hand, if I could do this:

<p:variable name="var" select="..."/>
<p:variable name="var" select="big expression with multiple references to
<p:variable name="var" select="big expression with multiple references to

...then it would only be a matter of commenting out the second line - no
references to update.

Also keep in mind that we might start seeing code like this if the variable
needs to be in a different environment:

<p:variable name="var" select="..."/><p:group>
<p:variable name="var" select="..."/><p:group>
<p:variable name="var" select="..."/><p:group>
<p:variable name="var" select="..."/><p:group>
<p:variable name="var" select="..."/>
...do stuff with $var here...

...I might even do it myself to avoid bugs even though it's butt ugly.


2011/11/28 Chris Maloney <voldrani@gmail.com>

> I agree with Florent, and would vote against it.  Especially when
> considering newer programmers, or programmers who come from an imperative
> programming background, immutable variables (variables that don't vary) can
> be confusing.  Allowing them to redefine (shadow) an immutable variable
> just makes it all the more so.  I think enforcing each variable to have a
> unique name within the same scope would be in keeping with the nature of
> this programming paradyme, and would help to reduce bugs, and make bugs
> that do occur easier to find.
> Just my two cents.
> Cheers!
> On Mon, Nov 28, 2011 at 5:58 AM, Geert Josten <geert.josten@dayon.nl>wrote:
>> > > PS: what are the use cases for XSLT? ;-)
>> >
>> >   Honestly, I am not quite sure but I suspect this is the same
>> > reason.  Maybe also to ease generating code in some cases.  But
>> > I didn't say I liked it more in XSLT :-)
>> Not how I meant it. It is just if this is common practice in XSLT people
>> could be used to this, and expect it to behave similarly in XProc. XQuery
>> allows doing similar things with let's as well, so why should XProc be
>> different? ;-)
>> Cheers..
Received on Tuesday, 29 November 2011 10:28:09 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:17:01 UTC