Global variables are undesired
In the programming world there is consensus about the rule that global variables should be avoided.Global means that a variable can be seen and manipulated from any part of a program.
It is far better to have local variables. These can only be seen and manipulated in a specific small part of a program.
Variables in 8th
As we said in a previous lesson we have a special solution for this in 8th.When we normally define variables (like we did before) these variables will be part of the namespace user which is the namespace where 8th stores variables and words by default. Example:
In line 1 we define the numeric variable named chris.
In line 2 we push the variable itself (not the contents of it) on the stack.
In line 3 you can see that the variable effectively is called user:chris.
Creating variables in your own 8th namespace
In 8th you can create your own namespaces freely. Example:In line 1 we create and make active a new namespace called myspace using the ns: word.
For now this means that variables defined after making this myspace active, will be part of this namespace.
In line 2 we define a new variable named mike.
In line 3 we push the variable mike (the definition, not the contents) on the stack and display the stack
In line 4 we can see that mike now belongs to the namespace myspace, so the qualified name is myspace:mike
Qualifying names
In general it is best practice to qualify variables and words by namespace, like myspace:mike in the previous paragraph.That way it is always clear what you mean.
The same applies to words. So it is better to use n:+ for addition than barely +.
Further use of namespaces
In a next lesson we will see that we can also create pieces of our own code, which will be called words, as is the case with standard 8th word definitions.Those words can also be put in a desired (and/or self created) namespace.
Using standard (default) 8th namespaces
When you create new words or variables that rightly belong to one of the standard namespaces already defined in 8th, you can define your own stuff there.However, in almost any case it is better to create your own separate namespaces. The main reason to do so is that the names you choose for your words or variables might already be taken by standard 8th.
Also you’d have to check with each and every new 8th version if new conflicting names exist.
Unique namespaces best practice
Namespace names can be split in multiple parts using dots between the parts.Here we see that the namespace consists of 2 parts, @john and space1.
This is BTW a very nice naming convention for your own namespaces.
Use your own name preceded by 1 or 2 weird characters (like @) as a prefix and only create namespaces with that unique prefix.
More info about 8th
There is a central website for 8th here: http://8th-dev.com/Corresponding YouTube videos here: https://goo.gl/6Ti2pI



No comments:
Post a Comment