33
CLEANLINESS
The most important practice in effective programming is to generate readable code. Readable code is
easier to debug, understand, and maintain. A short snippet that is poorly formatted may be decipherable
later, but a longer program will not be. If you pay attention to your coding style early on, it will quickly
become second nature.
Here is a list of specific suggestions:
r Bracketing: You must always enclose blocks in curly braces. It is a good idea to always use braces,
even for single statements. Opening braces for functions and states should start on the line after
the declaration. Otherwise, place the brace on the same line as the control block.
r Indentation: On the line immediately following an open brace, you should add one indentation,
usually four spaces. Remove an indentation on the line after a close brace.
The two most important guidelines are indentation and bracketing. This code, for example, is not
properly indented:
default{state_entry(){ llSay(0, "Hello, Avatar!"); } touch_
start(integer total_number) { llSay(0, "Touched."); }}
If you look closely, you'll see that it's the same as Listing 1.1—which was much more readable!
r Line wrap:PVTIPVMENBOVBMMZTFQBSBUFMJOFTMPOHFSUIBODIBSBDUFST
r Coding styles: In general, don't mix coding styles. When editing code you didn't create, keep the
FYJTUJOHTUZMFPSDIBOHFUIFTUZMFUISPVHIPVU
r Function length: Try to keep functions short and focused. A good rule is that a function longer than a
screen height should be refactored into multiple functions.
r Comments: Use comments to explain what you are doing; however, avoid excessive inline
comments: well-written code should explain itself. Do, however, describe what each function does
in a comment near the top of the definition.
r Variable naming:
r 7BSJBCMFOBNFTNVTUBMXBZTTUBSUXJUIBMFUUFSPSVOEFSTDPSF
r %POhUBCCSFWJBUF4JODFDBTFNBUUFSTx is not the same as X), longer names will cause
signicantly less confusion.
r (MPCBMWBSJBCMFTTIPVMECFHJOXJUIBMPXFSDBTFg; for example, gSelected.
r 7BSJBCMFTVTFEBTDPOTUBOUTTIPVMECFJOBMMDBQTBOEXPSETTIPVMECFTFQBSBUFECZVOEFSTDPSFT
like this: OWNER_KEY
.
r :PVTIPVMEVTFDBNFMDBTFMPXFSDBTFàSTUMFUUFSBOEUIFODBQJUBMJ[FFBDIXPSEGPSWBSJBCMF
names, such as this: myCountingVariable.