When are global variables actually considered good/recommended practice?

懵懂的女人 提交于 2019-12-01 18:43:27

Global variables aren't generally bad because of their performance, they're bad because in significantly sized programs, they make it hard to encapsulate everything - there's information "leakage" which can often make it very difficult to figure out what's going on.

Basically the scope of your variables should be only what's required for your code to both work and be relatively easy to understand, and no more. Having global variables in a program which prints out the twelve-times tables is manageable, having them in a multi-million line accounting program is not so good.

I think this is another subject similar to goto - it's a "religious thing".

There is a lot of ways to "work around" globals, but if you are still accessing the same bit of memory in various places in the code you may have a problem.

Global variables are useful for some things, but should definitely be used "with care" (more so than goto, because the scope of misuse is greater).

There are two things that make global variables a problem: 1. It's hard to understand what is being done to the variable. 2. In a multithreaded environment, if a global is written from one thread and read by any other thread, you need synchronisation of some sort.

But there are times when globals are very useful. Having a config variable that holds all your configuration values that came from the config file of the application, for example. The alternative is to store it in some object that gets passed from one function to another, and it's just extra work that doesn't give any benefit. In particular if the config variables are read-only.

As a whole, however, I would suggest avoiding globals.

Global variables imply global state. This makes it impossible to store overlapping state that is local to a given part or function in your program.

For example, let stay we store the credentials of a given user in global variables which are used throughout our program. It will now be a lot more difficult to upgrade our program to allow multiple users at the same time. Had we just passed a user's state as a parameter, to our functions, we would have had a lot less problems upgrading to multiple users.

my question is what is the reason global variables are still needed,

Sometimes you need to access the same data from a lot of different functions. This is when you need globals.

For instance, I am working on a piece of code right now, that looks like this:

static runtime_thread *t0;

void 
queue_thread (runtime_thread *newt)
{
   t0 = newt;
   do_something_else ();
}

void 
kill_and_replace_thread (runtime_thread *newt)
{
   t0->status = dead;
   t0 = newt;
   t0->status = runnable;
   do_something_else ();
}

Note: Take the above as some sort of mixed C and pseudocode, to give you an idea of where a global is actually useful.

Global variables are considered when you want to use them in every function including main. Also remember that if you initialize a variable globally, its initial value will be same in every function, however you can reinitialize it inside a function to use a different value for that variable in that function. In this way you don't have to declare the same variable again and again in each function. But yes they can cause trouble at times.

  • List item

Global names are available everywhere. You may unknowingly end up using a global when you think you are using a local

  • And if you make a mistake while declaring a global variable, then you'll have to apply the changes to the whole program like if you accidentally declared it to be int instead of float
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!