Difference between revisions of "LSL Variables"
(need to check formatting. I will update as appropriate.) |
(grammar, more accurate, x y z convention) |
||
Line 1: | Line 1: | ||
A variable is | A '''variable''' is a place to store information, like a number or strings. | ||
LSL is a strongly typed language. This means that variables must be declared by type and that variables may only hold values of a corresponding type. | LSL is a strongly and dynamically typed language. This means that variables must be declared by type and that variables may only hold values of a corresponding type. | ||
==Variable Conventions== | ==Variable Conventions== | ||
=== Common Variable Uses=== | === Common Variable Uses=== | ||
i is an | * '''i''' is usually used as an index variable in a [[for]] loop. '''j''' is usually used for a nested for loop (when i is already being used), and then '''k'''. | ||
* '''x''', '''y''', and '''z''' usually refer to the first three dimensions of some vector (when applicable). | |||
j is | |||
===Magic Numbers=== | ===Magic Numbers=== | ||
The phrase "magic number" refers to a number in code, | The phrase "magic number" refers to a number written literally as a constant in code, but the meaning is not intuitive from the context of the code. It may also be such a number used repeatedly throughout the code. | ||
For example: | |||
if ( vctSpd.z < 10 ) { | if ( vctSpd.z < 10 ) { | ||
} | } | ||
The 10 is a "magic number". In a scenario where this test is repeated 20 times in the script, any change to the number would require 20 updates by hand, which increases the chance for errors. | |||
To | To avoid magic numbers, declare a variable of the appropriate type, set the value of the variable in the declaration and use the variable in your code. | ||
integer intSlowSpeed = 10; | integer intSlowSpeed = 10; | ||
Line 26: | Line 25: | ||
} | } | ||
Updating the value of intSlowSpeed here requires only one change and reduces the opportunity for error. The name of the variable also expresses its meaning. |
Revision as of 19:23, 3 February 2007
A variable is a place to store information, like a number or strings.
LSL is a strongly and dynamically typed language. This means that variables must be declared by type and that variables may only hold values of a corresponding type.
Variable Conventions
Common Variable Uses
- i is usually used as an index variable in a for loop. j is usually used for a nested for loop (when i is already being used), and then k.
- x, y, and z usually refer to the first three dimensions of some vector (when applicable).
Magic Numbers
The phrase "magic number" refers to a number written literally as a constant in code, but the meaning is not intuitive from the context of the code. It may also be such a number used repeatedly throughout the code.
For example:
if ( vctSpd.z < 10 ) { }
The 10 is a "magic number". In a scenario where this test is repeated 20 times in the script, any change to the number would require 20 updates by hand, which increases the chance for errors.
To avoid magic numbers, declare a variable of the appropriate type, set the value of the variable in the declaration and use the variable in your code.
integer intSlowSpeed = 10;
if ( vctSpd.z < intSlowSpeed) { }
Updating the value of intSlowSpeed here requires only one change and reduces the opportunity for error. The name of the variable also expresses its meaning.