Quote:
|
Originally Posted by ApothiX
Wrong answer.
|
No, I just assumed you guys would be clever enough to understand it without be giving multiple paragraphs of answers.
The microseconds it takes the compiler to calculate the number are not even worth mentioning. Assuming that Stefan does not do constant folding at compile time, the microseconds it takes the interpreter to calculate the number are still not enough to worry about it.
We want to write down the whole formula in the script because the trouble I will have with recalculating the number each time I am going to change the tile refering to severly outweights the CPU's trouble calculating it for me. This is about what you said, I think.
But we still want to use a variable holding the number for us instead of putting it down where we use it and just commenting it for its meaning.
We could consider the variable lookup that is going to take a lot more performance, over time then the CPU's calculation will, because gscript does not have C++-style const variables and cannot substitute the variable references with the number at compile time. This could be as simple as having a numeric variable index and using it to index some variables array, but as it is possible to form variable names using the
this.("foo" @ "bar) notation at runtime, I assume that we will still have to do a string-search in the
this object at runtime.
But I do not care because it is gscript's job to take care of stuff like that. I gladly make that negligible sacrifice to gain more maintainability.
You generally do not want to have random magic numbers in your code. Often, the meaning will not be immediately obvious to someone trying to do maintain your script, either someone else in your NAT, or yourself a week later. This is why we give the number a name, using a variable. This is why we use names in general, we should not make an exception here. Using a comment would only be an ugly workaround, harder to maintain and no one bothers to write comments in production code anyway when code could be self-documenting.
Also, according to
Don't repeat yourself, I want to be able to change the constant's value in a single place in my system without having to search my whole script or even set of scripts for uses of that particular equation. I am likely to forget to change it in one place, leading to annoying bugs.
There is more discussion regarding
magic numbers on that other wiki.
Quote:
|
Hmm, in an interpreted scripting language? I doubt it :x
|
GScript is compiled to bytecode. Why would there not be constant folding?