Difference between revisions of "LSL Variables"

From Second Life Wiki
Jump to navigation Jump to search
(need to check formatting. I will update as appropriate.)
 
(grammar, more accurate, x y z convention)
Line 1: Line 1:
A variable is a programming term which refers to a place to store information.
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 integer used in the outer most loop as an incrementor.
* '''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 an integer used in an inter loop as an incrementor.


===Magic Numbers===
===Magic Numbers===
The phrase "magic number" refers to a number in code, who's meaning is not intuitive from the context of the code and \ or a number repeated throughout the 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.


Example:
For example:


if ( vctSpd.z < 10 ) {
if ( vctSpd.z < 10 ) {
}
}


In this example 10 is a "magic number". In a scenario where this test is repeated 20 times in the script, a change would require 20 updates and increase the chance of error.
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 avoide 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.
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 1 change and reduces the opportunity for error.
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 20: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.