A common ‘mistake’ when working with any programming language is over use of code, or rather not being efficient with code. The most common mistake, and most easily rectified is repetition of similar or exact snippet of code, over and over and over and over and over and over again…. just like the example actionscript code shown below.
Example of Inefficient use of code
_global.variable1 = 0;
_global.variable2 = 0;
_global.variable3 = 0;
_global.variable4 = 0;
_global.variable5 = 0;
_global.variable6 = 0;
_global.variable7 = 0;
_global.variable8 = 0;
_global.variable9 = 0;
_global.variable10 = 0;
_global.variable11 = 0;
_global.variable12 = 0;
_global.variable13 = 0;
In the above example we are simply setting a whole load of global variables in flash. You can imagine that if the number of variables increased, say for example 40 variables, you would need 40 lines of code to achieve the desired result.
To simplify this process i have created a simple snippet of code could be contained within a for loop, creating and setting all of these variables in only 3 lines of code. By using a for loop we can add or decrease the amount of variables increased by changing just one variable.
Efficient use of code
/// set how many variables should be created
number_of_times = 13;
/// loop 13 times creating 13 variables with the value of 0
for (var i = 1; i<(number_of_times+1); i++) {
_global["slide"+i] = 0;
}
The Benefits
This makes it much easier to increase or decrease the amount of variables created, simply change “number_of_times = 13″ to a greater number.
If for example we wanted to rename the varaibale from “variable” to “myVariable” we would ammend one line of code… not 13 as shown in the initial example.
This method can be applied in a variet of different ways helping you become more productive and efficent with your code.
I hope this simple little explanation helps someone out.