Difference between revisions of "LSL Style Guide"

From Second Life Wiki
Jump to navigation Jump to search
m
m (unified coding style in examples, minor cleanups, fixed indentations)
Line 45: Line 45:
<lsl>
<lsl>
     integer gSelected = 0;
     integer gSelected = 0;
     string  gMyName = "Please set one";
     string  gMyName   = "Please set one";
</lsl>
</lsl>


Line 53: Line 53:
<lsl>
<lsl>
     integer CHAT_CHAN = -517265;
     integer CHAT_CHAN = -517265;
     key OWNER_KEY = llGetOwner();
     key     OWNER_KEY = llGetOwner();
</lsl>
</lsl>


Line 63: Line 63:
     {
     {
         if ( _channel == 1 || _id == llGetOwner() )
         if ( _channel == 1 || _id == llGetOwner() )
        llOwnerSay("Hello Avatar");
            llOwnerSay("Hello Avatar");
     }
     }
</lsl>
</lsl>
Line 70: Line 70:
Many people will start out doing many, many function calls on one line. This makes the code hard to read, and almost impossible to debug. The following is an example of one such program:
Many people will start out doing many, many function calls on one line. This makes the code hard to read, and almost impossible to debug. The following is an example of one such program:
<lsl>
<lsl>
list lst;
list   lst;
integer numDigits = 10;
integer numDigits = 10;
default {
default {
 
    touch_start(integer n) {
  touch_start(integer n) {
        integer i = 0;
      integer i = 0;
        integer index = llListFindList(lst, [llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1))]);
      integer index = llListFindList(lst, [llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1))]);
        if (!~llListFindList(lst, [llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1))]))
      if (!~llListFindList(lst, [llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1))]))
            lst += llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1));
          lst += llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1));
        llOwnerSay(llList2CSV(lst));
      llOwnerSay(llList2CSV(lst));
    }
  }
 
}
}
</lsl>
</lsl>
Line 88: Line 87:


<lsl>
<lsl>
list lst;
list   lst;
integer numDigits = 10;
integer numDigits = 10;


default {
default  
     touch_start(integer n) {
{
     touch_start(integer n)  
    {
         integer i = 0;
         integer i = 0;
         string name = llKey2Name(llDetectedKey(i));
         string name       = llKey2Name(llDetectedKey(i));
         list nameAsList = llParseString2List(name, [" "], []);
         list   nameAsList = llParseString2List(name, [" "], []);
         string firstName = llList2String(nameAsList, 0);
         string firstName = llList2String(nameAsList, 0);
         string startPart = llToLower(llGetSubString(firstName, 0, numDigits - 1));
         string startPart = llToLower(llGetSubString(firstName, 0, numDigits - 1));
         integer index = llListFindList(lst, (list)startPart);
         integer index     = llListFindList(lst, (list)startPart);
 
         if (!~index)
         if (!~index)
             lst += startPart;
             lst += startPart;
Line 109: Line 111:


<lsl>
<lsl>
list lst;
list   lst;
integer numDigits = 10;
integer numDigits = 10;


default {
default  
     touch_start(integer n) {
{
     touch_start(integer n)  
    {
         integer i = 0;
         integer i = 0;
         string startPart = llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1));
         string startPart = llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1));

Revision as of 08:04, 15 April 2009

Effective programming in LSL requires that developers use a disciplined practice for applying formatting and convention to their scripts.

These guidelines, referred to collectively as a Style Guide, are not as rigid as the rules required by the language compiler but nonetheless are critical to creating maintainable code. The most critical aspect of a style is that you apply it consistently to the code you write.

General Guidelines

Most people, when they start programming on their own, will have programs that are UGLY to look at; to put it nicely. They usually look like the following:

<lsl> default {state_entry(){llSay(0,"Hello World.");}}</lsl>

However, that code is impossible to read (or at least to follow) when one is writing a ten thousand word program. Therefore, programmers have two main methods as to bracketing and indenting.


Method One: <lsl>

   default {
       state_entry() {
           llSay(0, "Hello World.");
       }
   }

</lsl>

Method Two: <lsl>

   default
   {
       state_entry()
       {
           llSay(0, "Hello World.");
       }
   }

</lsl>

Method One conserves space, however Method Two is easier to read for the beginner. Once a scripter is the practice of using a particular style, reading code in that style will be easier. Consistent indenting makes reading both styles easier. In Method One indenting is the key indicating factor of levels of scope.

Naming Conventions

There are many naming conventions in Second Life. Only the most used ones will be listed below.


Global Variables (variables used through out the entire program) should begin with a lowercase g. For Example:

<lsl>

   integer gSelected = 0;
   string  gMyName   = "Please set one";

</lsl>


Variable Constants should be in ALL CAPS. For Example:

<lsl>

   integer CHAT_CHAN = -517265;
   key     OWNER_KEY = llGetOwner();

</lsl>


Arguments used within a user defined function or an event should start with an underline (_). For Example:

<lsl>

   listen( integer _channel, string _name, key _id, string _message )
   {
       if ( _channel == 1 || _id == llGetOwner() )
           llOwnerSay("Hello Avatar");
   }

</lsl>

Separating Code

Many people will start out doing many, many function calls on one line. This makes the code hard to read, and almost impossible to debug. The following is an example of one such program: <lsl> list lst; integer numDigits = 10;

default {

   touch_start(integer n) {
       integer i = 0;
       integer index = llListFindList(lst, [llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1))]);
       if (!~llListFindList(lst, [llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1))]))
           lst += llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1));
       llOwnerSay(llList2CSV(lst));
   }

} </lsl>

Now here is the code, with the exact same features, in a simpler way. While hardly anyone could tell you what the above code did, almost everyone can tell you what the below code does.

<lsl> list lst; integer numDigits = 10;

default {

   touch_start(integer n) 
   {
       integer i = 0;
       string  name       = llKey2Name(llDetectedKey(i));
       list    nameAsList = llParseString2List(name, [" "], []);
       string  firstName  = llList2String(nameAsList, 0);
       string  startPart  = llToLower(llGetSubString(firstName, 0, numDigits - 1));
       integer index      = llListFindList(lst, (list)startPart);
       if (!~index)
           lst += startPart;
       llOwnerSay(llList2CSV(lst));
   }

} </lsl>

LSL lacks an optimizing compiler. For this reason it may be necessary to balance the two style to get faster code. Line combination optimization should only be done after the code is working & bug free. Improper optimization can lead to wrong results. Always test optimized code thoroughly.

<lsl> list lst; integer numDigits = 10;

default {

   touch_start(integer n) 
   {
       integer i = 0;
       string startPart = llToLower(llGetSubString(llList2String(llParseString2List(llKey2Name(llDetectedKey(i)), [" "], []), 0), 0, numDigits - 1));
       if (!~llListFindList(lst, (list)startPart))
           lst += startPart;
       llOwnerSay(llList2CSV(lst));
   }

} </lsl>

Script Structure

LSL scripts are comprised of expressions, functions, statements, event handlers and states. The LSL compiler mandates a certain structure to scripts:

  1. User Defined Variables (see LSL_Variables)
  2. User Defined Functions (see User-defined_functions)
  3. default State (see State)
  4. User Defined States

Editor

There are many 3rd party editors with LSL syntax files. See LSL Alternate Editors for more information.