29.10.2018, 22:14
Quote:
There are very big advantages to using a function instead of a variable:
1) It makes your code more readable if (IsPlayerX(i)) has way more meaning than if (gPlayerData[i][E_PLAYER_X]). 2) It makes your code more maintainable. If you decide to change how a players X-ness is defined, you only need to change this in one function, instead of all over your code. 3) It isolates data. Pretty much all variables should be declared static, which restricts them to a single file. You then provide access to the data through a fixed API in the form of your functions, not just letting anyone access it. 4) It allows you to control pre- and post-conditions. Maybe you want to do something like: Код:
if (gPlayerData[i][E_PLAYER_X]) { ++gPlayerData[i][E_PLAYER_X_ACCESSES]; } Код:
bool:IsPlayerX(i) { if (gPlayerData[i][E_PLAYER_X]) { ++gPlayerData[i][E_PLAYER_X_ACCESSES]; return true; } return false; } Well written code will almost never use variables directly except really internally in a module. |