07.07.2011, 07:26
(
Последний раз редактировалось Kyosaur; 17.10.2011 в 11:36.
)
I felt i should respond since his question wasnt exactly answered, and no one explained the importance of the warnings.
Yes, warnings matter. They may not render your GM unplayable but they generally complain about things that are worth warning you about! Its extremely bad practice to ignore warnings or use pragma's to cover them up.
Proper indentation:: This honestly is one of the best things to practice. You avoid annoying warnings regarding improper indentation after all :P. After every opening curl bracket (The symbol: "{") you should add four spaces to all your code until you hit a closing curl bracket (The symbol: "}"). This makes the code spread out and a lot easier on the eyes. The main reason why indentation is so important though is because it makes scope very clear (Scope is where soemthing iis declared, and where it can be used due to said location)! Here's an example if proper indentation:
local variables shadows another variable: This warning is pretty clear. It means two variables have the same name and are in the same scope. This is important to fix or you could have some unwanted results, as when you mod a variable when there's another variable with the same name, which one is getting changed? Are you sure its the one you want? There is a pretty simple fix for this, simply change the name of one of the variables!
before i get into the fix, here's a small lesson on scope.
Example (for consistency, lets use the same one):
Symbol is never used This one is also very clear. You declared a variable and didnt use it through the entirety of its scope. If your not gonna use a variable, there is no point in declaring it! This clutters up your code as its uneeded, and its bad practice (in some languages, this will just be allocated and ignored). If your not sure if a variable is going to be used, declare it as "stock". This makes it so if you dont use the variable, its not included in your code.
Example:
Yes, warnings matter. They may not render your GM unplayable but they generally complain about things that are worth warning you about! Its extremely bad practice to ignore warnings or use pragma's to cover them up.
Proper indentation:: This honestly is one of the best things to practice. You avoid annoying warnings regarding improper indentation after all :P. After every opening curl bracket (The symbol: "{") you should add four spaces to all your code until you hit a closing curl bracket (The symbol: "}"). This makes the code spread out and a lot easier on the eyes. The main reason why indentation is so important though is because it makes scope very clear (Scope is where soemthing iis declared, and where it can be used due to said location)! Here's an example if proper indentation:
Код:
public OnPlayerCommandText(playerid, cmdtext[]) { //Opening curl bracket, so we indent four spaces (hit the tab key). if(!strcmp(cmdtext, "//example")) // strcmp just for the example { //Another opening curl bracket, so we indent four more spaces. //Some code... } //We're done with the if statement, and add a closing curly bracket, so we go back four spaces. return 0; } //Done with the callback, so we're back were we started (and we repeat :P).
before i get into the fix, here's a small lesson on scope.
- Global scope - This is the point from where you declare your variable that is outside any function/callback, onward (so the rest of the file down).
- Local scope - This is the area between any pairs of curl brackets. So if you put your variable immediately after an opening curly brace it can only be used in the entire area of that code block (until the closing curly brace for that opening curly brace is met).
Example (for consistency, lets use the same one):
Код:
new g_Example = 0; // This is outside of any function or callback, so its global...can be used anywhere in this example public OnPlayerCommandText(playerid, cmdtext[]) { new Example = 0; //Normally if our previous var didnt have the preceeding "g_" this would've given a warning. if(!strcmp(cmdtext, "//example")) { new Example = 0 // This still will give a warning, as there's a var above it (in the same local scope) with the same name ... either use the previous one, or rename this one. } //The Example var (the warning one inside the if statement) is out of scope now. return 0; } //The first Example var (one before the if statement) is no out of scope //g_Example is global so its been in scope the entire time, and still is.
Example:
Код:
new stock g_MyVar = 0;