What do you think about global variables/Objects?

Hi people!

What do you think about a plugin that uses a lot of global variables or use one global variable storing multiple objects?
let shirt = {
color: blue,
size: Big,
etc...
}

I mean, this is a way for us to make plugin parameters, right?
JavaScript:
    Eli.Param.DevSwitches = {
        Playtest: Number(Eli.Parameters['Playtest Switch']),
        BattleSc: Number(Eli.Parameters['Battle Switch']), // falta fazer
        Mobile: Number(Eli.Parameters['Mobile Switch']),
        Desktop: Number(Eli.Parameters['Desktop Switch']),
        Vehicle: Number(Eli.Parameters['isInVehicle Switch']),
        Airship: Number(Eli.Parameters['isInAirship Switch']),
        Boat: Number(Eli.Parameters['isInBoat Switch']),
        Ship: Number(Eli.Parameters['isInShip Switch']),
        DmgFloor: Number(Eli.Parameters['isOnDmgFloor Switch']),
        Dashing: Number(Eli.Parameters['isDashing Switch']),
        Bushed: Number(Eli.Parameters['isOnBush Switch']),
        Ladder: Number(Eli.Parameters['isOnLadder Switch']),
        Timerw: Number(Eli.Parameters['Timer running Switch'])
    };

This above, are global objects wrapped in the namespace of the plugin, right?

So if I make some of these Global Objects like:
JavaScript:
Eli.DevSwitches.obj = {
....
}

This can lead me to a performance issue in my plugins? Using a lot of this to store values, booleans etc...

My question about this is that I constantly have to check and use the value from, for example, Game_Timer.prototype... and Sprite_Timer and Window_Timer...
With that, sometimes I have to use:
Window_Timer.prototype.windowWidth();
Inside the codes of Game_Timer or Sprite_Timer...
And I think it is kinda ugly for me.

I have asked it here other times(about performance), and I think I'm thinking a little exaggerated about performance. That's the reason I don't use many global objects and variables(besides those ones that rm mv code already have).

But in the scope of "Making plugins for Rm Mv", should I worried about this?
I mean, a lot of global variables or objects(wrapped in the nameSpace of the plugin) storing values can cause a performance issue in plugins?
Actually, yes, you should avoid any unnecessary global variables or objects. It does make a difference, but it always depends on the situation.

It's a good practice.

As coders, we share best practices and learn new ones because we accept that no matter what, there will be issues in our code the first few times we try a build.

There may be a time where you have so much that keeping it global would be an issue. But rather than wait for it to become one, you use your good practices, like not making anything global that doesn't have to be, not creating a bunch of closures, not creating an array with new Array, etc.

Right now I have some part in one of my plugins that declared
i
as a global variable instead of a block level one.
Now, my good practices prevent this from becoming an issue until I find it. Because everywhere that I use an i variable, it's block level and only lives as long as it needs to (I'm not even sure if this global i is from one of my plugins)

So to sum it up
limiting global variables won't matter everytime, but it will matter sometimes
and for this reason, it's best to prepare against it with good practices.


As far as getting your params ahead of time, strings are actually not huge as far as memory required, but they're larger than other primitive types.

Params from plugins often are only required in certain settings. So if there are any ones, like for example that only deal with the menu, then you could have those only load when the menu is loading, and flush them out in the meantime.

So plugin parameters that belong to the menu and didnt' need to read elsewhere, for me, would only live as long as the menu was opened, and maybe not even after they were no longer needed.

Original thread: https://forums.rpgmakerweb.com/threads/what-do-you-think-about-global-variables-objects.117783/