I'm slowly learning the scripting, but one thing I'm running up against is the integer type instead of int. No, I'm not asking for a new type, but it got me thinking... Scripts could be better with preprocessor macros. (Showing my bias, I'd prefer C compiler notation)
In laymen's terms, this is code in the script that is run at compile time, not at runtime, and affects how the compiler reads the human-readable code that it turns into computer-readable code. How about an example?
integer foo;
for (foo = 0; foo < 25; foo++)
{DoSomeFunction(foo);}
To the good coder, the question arises: What's 25? Why 25? Is it number of sides? Favorite day of the month? If maintaining this, what should 25 be changed to? What other places have to be changed to the same value? The solution is a variable.
integer NUMOFSTEPS = 25;
...
integer foo;
for (foo = 0; foo < NUMOFSTEPS; foo++)
{DoSomeFunction(foo);}
This has its own problems, as this new variable can be accidentally set (If NUMOFSTEPS=foo, instead of NUMOFSTEPS==foo) and more so, slows the server and script down, requiring additional memory for the variable and additional lookups.
http://secondlife.com/badgeo/wakka.php?wakka=StyleGuide
Has a debate where speed is a tradeoff of maintainability. This is one debate that could be made moot.
#define NUMOFSTEPS 25
...
integer foo;
for (foo = 0; foo < NUMOFSTEPS; foo++)
{DoSomeFunction(foo);}
This has all the advantages of the first version (Speed and space) and the advantages of the second version (Maintainability). But this is a facile example. With just a few commands, quite a few speed/use problems can be resolved.
Functions now can be inlined, speeding up runtime
#define DoSomeFunction(foo) llSay(0,(string) foo)
They now can be pass by reference, making some functions possible.
#define PushStack(foo,bar) bar = bar+foo
Furthermore, functions that are actually #define s make unrolling loops much more maintainable.
And this is a biggie for me: Debug code, also something that is a huge slowdown during runtime, could be left in without slowdowns.
integer DEBUG = 0;
...
if (DEBUG) {llWhisper(0,"I'm debugging!"
;} //Slow: DEBUG must be looked up, then the if structure run.#define DEBUG 0
#if (DEBUG)
llWhisper(0,"I'm debugging!"
;#endif //Fast! Because DEBUG is set 0, this code doesn't exist in the bytecode at all. All the comparing was done once long ago before the script started.
Oh, and of course, lastly, it serves as a replacement for typedef.
#define int integer
And structs, if you ignore the list speed issue.
#define nameEntry list
// string firstName;
// string lastName;
#define SetFirstName(foo,bar) foo=llDeleteSubList(foo,0,1) + [bar]
#define GetFirstName(foo,bar) foo=llList2String(foo,0)
etc...
In conclusion, I would like the following preprocessor macros be added to the scripting compiler:
#define -label-[(-args-)] -value-
#if (-boolean conditional-)
#endif
#undef, #ifdef, #ifndef, and family could be included, but those three are essential.
How about it?
