Re: Global Namespace Variables

From: Bryan Schofield (bschofield_at_users.sf.net)
Date: 04/13/04


Date: Tue, 13 Apr 2004 03:51:25 GMT

miguel sofer wrote:
> Bryan Schofield wrote:
>
>> Terry Hampton wrote:
>>
>>> All,
>>>
>>> A colleague of mine insists on using only fully qualified namespace
>>> variables ( the global namespace - ex: ::foobar ) He
>>> correctly
>>> tells me "so that foobar is visible everywhere and there is no
>>> ambiguity".
>>> Of course that's correct and he writes solid code, but ...... I
>>> don't
>>> agree with the practice. If everything is "global", things get
>>> pretty
>>> crowded I think. Meaning - only one ::foobar could be used in your
>>> entire
>>> program. Many times NSPACEA::foobar, and NSPACEB::foobar are
>>> more logical. He uses fully qualified proc names as well.
>>>
>>> Anyway .... I can't find a better reason to discount his way of
>>> handling
>>> variables other than personal preference. I don't think it's
>>> technically
>>> correct, but don't have a proof.
>>>
>>> What are your thoughts about making most all variables fully
>>> qualified ?
>>>
>>>
>>> Thanks,
>>> Terry
>>
>>
>>
>> I personally prefer qualifying variable instead of using "global" or
>> "variable" in procs. To me, it makes the code clearer, but consider
>> the following:
>>
>> > tclsh
>> % proc a {} {
>> global foo
>> set bar $foo
>> }
>> % proc b {} {
>> set bar $::foo
>> }
>> % set foo "whee"
>> whee
>> % time a 1000000
>> 9 microseconds per iteration
>> % time b 1000000
>> 7 microseconds per iteration
>> %
>>
>> As you can see the "global" command isn't free. It's cheap, but not
>> free. This global and variable commands can really add up if there are
>> few in proc that is heavily used.
>>
>> Having said that, it probably still comes down to preference.
>>
>> -- bryan
>
>
> 'global' will do a lookup-by-name once; after that, the variable is
> accessed by index (~ pointer), much faster. Fully qualified names are
> accessed by name each time.
>
> So, if extracting every microsecond: use fully qualified names for
> variables you access just once, 'global'or 'variable' for those that are
> accessed several times in the proc body.
>
> Miguel

Thanks I did not know that!
-- bryan



Relevant Pages