Quote:
Originally Posted by cbk1994
For the thread to be stuck pretty much says it is the "correct" way to do things. A few points:
- Variable declarations serve absolutely no purpose to the engine, and can't possibly have any other effect except slowing it down, even if ever so slightly. Saying that all variables should be declared before being initialized is incorrect and personal preference. It is probably better to just use comments for listing variables.
- The indentation of your switch statement is wacky. This is just my personal preference, but yours is being promoted as some kind of standard.
PHP Code:
switch (variable) {
case "value":
// whatever
break;
case "value2":
// whatever2
break;
}
switch statements don't really have a universal format, though.
- Falling through in switch statements isn't a problem unless you work on a server with really poor scripters.
- 'default' needs to end with a break or return as well. Keep in mind it doesn't have to be at the end of the list.
- Apparently classes can't even start with an uppercase letter, and even if they could/can, that would be different than what 99% of servers are doing now.
The thread is well-intentioned, but I don't like how it tries to set the "right" way to script based on someone's ideas. I wouldn't have a problem if the thread was reposted/edited with the controversial/incorrect stuff removed. There are also some things that need further explanation, such as the return statement. The way it's worded now it could be seen that something like:
PHP Code:
return (((a ^ 2) + (b ^ 2)) ^ .5);
is wrong, when it's clearly not.
|
Hi Chris,
For the thread to be stuck just means that people should look at it. Some guidance is better than none. (We have all seen some of the scripts out there.)
Regarding variable declarations, this is my rational behind the rule:
It makes it very clear how GS2 handles variables, and scope. As there is no block scope in GS2, this code code could be misleading:
PHP Code:
for (temp.i = 0; temp.i < 2; temp.i++) {
echo(temp.i);
}
With the above code, one (perhaps who has used C, C++, C#, Java, or the likes) may assume that temp.i would not be accessible outside of the for loop. However, this code makes it clear:
PHP Code:
temp.i;
for (temp.i = 0; temp.i < 2; temp.i++) {
echo(temp.i);
}
Notating the script with comments would not have this same effect.
Also, naturally, when trying to make a standard for defining your variables, why not use the one that already exists within the syntax of the language? Speed is a non-reason against this point.
Regarding the switch indentation, the style 1) prevents over-indentation, and 2) matches the indentation style of all the other statements (that is, indent all compound statements within the outer statement; the cases being part of the switch statement).
Regarding falling through, sorry, but are you calling me and many of famous professional programmers & scripters "poor"? (This includes the language designers of C#, Go, Pascal, Ruby, Ada, Eiffel, and more.) Just because a construct exists in a language doesn't warrant over-usage of it. The switch statement can be really,
really misleading to even mature scripters, and totally foreign to newbie scripters.
For a break;/return; in default:, that sounds extremely weird, as default is normally only evaluated when all the other cases are exhausted (apparently I don't know switches in GS2 that well either), but I'll confirm when I get home tonight.
I would have edited the class rule by now but can't.
The wording on the return statement section is a fair point and I would edit it if I could.
Thanks for commenting Chris, and I hope you agree that a thread including this information is better than no thread at all.