Transform this
#11

Quote:
Originally Posted by ******
Посмотреть сообщение
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];
}
Either you add that in every time you check the variable, or you just put it in one place in your function:

Код:
bool:IsPlayerX(i)
{
    if (gPlayerData[i][E_PLAYER_X])
    {
        ++gPlayerData[i][E_PLAYER_X_ACCESSES];
        return true;
    }
    return false;
}
And of course this can be modified or added at a later data again in just one place.

Well written code will almost never use variables directly except really internally in a module.
Good argument, agreed.
Reply


Messages In This Thread
Transform this - by KinderClans - 28.10.2018, 22:24
Re: Transform this - by RogueDrifter - 28.10.2018, 22:30
Re: Transform this - by TheToretto - 28.10.2018, 22:32
Re: Transform this - by RogueDrifter - 28.10.2018, 22:35
Re: Transform this - by BigETI - 29.10.2018, 10:16
Re: Transform this - by GameOvr - 29.10.2018, 10:27
Re: Transform this - by IllidanS4 - 29.10.2018, 11:26
Re: Transform this - by BigETI - 29.10.2018, 11:33
Re: Transform this - by TheToretto - 29.10.2018, 11:38
Re: Transform this - by Y_Less - 29.10.2018, 13:42
Re: Transform this - by RogueDrifter - 29.10.2018, 22:14
Re: Transform this - by TheToretto - 29.10.2018, 22:54
Re: Transform this - by KinderClans - 29.10.2018, 23:03
Re: Transform this - by Y_Less - 30.10.2018, 09:50
Re: Transform this - by Kane_ - 30.10.2018, 10:38

Forum Jump:


Users browsing this thread: 2 Guest(s)