[REBOL] Re: Q: Functions and the visibility of words
From: sanghabum:aol at: 26-Apr-2001 5:02
[Al--Bri--xtra--co--nz]:
> When a word is definied inside a function it is visible also outside the
> function (after the invication of the function) unless explicitly specified
> local. This seems to be a different approach to most of the other
> programming languages (at least that I know :).
>
> > The disadvantages are obvious, it is easy for the programmer to introduce
> unwanted side-effects, but what are the advantages this scheme introduces?
The disadvantages are so major, that something needs to be done. It don't
matter so much if I'm the only coder, but what happens if I start using code
contributed by others? Unless I can *guarantee* they have not created global
variables, then my use of their code could subvert one of my globals, or vice
versa.
I've been trying to avoid the accidental creation of clashing names in two
ways:
1. My externally callable functions are embedded in objects. So any globals
they create (accidentally or otherwise) have two-part names:
myobject/myvarname. Not perfect, but better than accidentally creating
myvarname.
2. I've gotten into the habit of starting all functions like this:
myfunc: func [.... /local aa bb cc dd]
Thus giving me a "pool" of fairly anonymous local variables that the function
can use. It saves me having to think up names too...though I will explicitly
name a variable if its usage is not obvious.
Carl S. is obviously aware of this problem, and has recently suggested a
solution similar to my first suggestion....
Carl Sassenrath:
>On modules, here's a handy way to accomplish a lot of what is desired.
>This is how we modularize our code within REBOL products:
>mod-mycode: context [
> var1: 1234
> var2: var3: var4: none
> func1: does ....
> func2: does ....
> set 'external-func1 does ...
> set 'external-func2 does ...
>]
>
>So, using a one-of-a-kind object allows you to make your code modular...
?at least to the degree that you don't have to worry about stepping on
>globals, etc.
>
>REBOL modules will be added for 3.0. But first, we must sell some
>products to pay the rent and eat from time to time. ;)
--Colin.