newbies: beware script locals

Ben Rubinstein benr_mc at cogapp.com
Wed Feb 26 09:42:48 EST 2003


on 2/21/03 2:09 PM, Ivers, Doug E wrote

> I love script locals, those local declarations at the top of your script
> outside of all handlers:
> 
> local myLocalVariable
>
> [...snip...]
> 
> I use them a lot in my top level script.  They are great because I don't have
> to declare them in each handler and they are "safer" than globals, if you know
> what I mean.
> 
> The caution is that whenever you edit the script, the value of the local goes
> away.

Yep - specifically, when the script is recompiled, the values of the locals
are reset to global.

I love them too, so I put up with this.  I use one of two strategies to deal
with this problem:

    * either make them self healing - eg if the use of the local is to hold
a calculated value which takes a bit of work to calculate, and is then more
or less a constant, I have a handler to "ensure they're set", which all
applicable routines invoke in set up.  This takes care of initialisation
when the stack opens or whatever; and also heals, with a small time penalty,
when I edit the script during development

    * or, having made sure that the names are unambiguous (eg
"myLocalVariable", or using some prefix or suffix convention that suits you)
I simply make them global during development.  So I have declarations at the
top of a script as apparently contradictory as
    global locOneThing, locAnotherThing
It's only one word to change when I think I've finished mucking about.  And
during development it's also sometimes helpful because you can inspect the
value in the message box.
 
  Ben Rubinstein               |  Email: benr_mc at cogapp.com
  Cognitive Applications Ltd   |  Phone: +44 (0)1273-821600
  http://www.cogapp.com        |  Fax  : +44 (0)1273-728866





More information about the use-livecode mailing list