Questions tagged [global-variables]

Global variables are variables that are accessible from all elements of a software component.

Global variables are variables that are accessible from all elements of a software component.

Global variables are used to share information between elements of a component and to store a part of the component's state. They therfore:

  • create a hidden coupling between all the elements that use them;
  • are a source of accidental bugs and inconsistencies, when changes and side-effects are not sufficiently controlled.
8810 questions
83
votes
5 answers

Why are global and static variables initialized to their default values?

In C/C++, why are globals and static variables initialized to default values? Why not leave it with just garbage values? Are there any special reasons for this?
Xinus
  • 29,617
  • 32
  • 119
  • 165
81
votes
7 answers

What is the best way to declare global variables in Vue.js?

I need access to my hostname variable in every component. Is it a good idea to put it inside data? Am I right in understanding that if I do so, I will able to call it everywhere with this.hostname?
Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145
81
votes
17 answers

global variable for all controller and views

In Laravel I have a table settings and i have fetched complete data from the table in the BaseController, as following public function __construct() { // Fetch the Site Settings object $site_settings = Setting::all(); …
Deepak Goyal
  • 1,186
  • 2
  • 13
  • 26
80
votes
6 answers

ASP.NET MVC Global Variables

How do you declare global variables in ASP.NET MVC?
Beginner
  • 28,539
  • 63
  • 155
  • 235
80
votes
10 answers

Accessing variables from other functions without using global variables

I've heard from a variety of places that global variables are inherently nasty and evil, but when doing some non-object oriented Javascript, I can't see how to avoid them. Say I have a function which generates a number using a complex algorithm…
John Richardson
80
votes
6 answers

How to store a global value (not necessarily a global variable) in jQuery?

Currently I am working on a legacy web page that uses a ton of JavaScript, jQuery, Microsoft client JavaScript, and other libraries. The bottom line - I cannot rewrite the entire page from scratch as the business cannot justify it. So... it is what…
Kris Krause
  • 7,304
  • 2
  • 23
  • 26
79
votes
11 answers

What is the correct way to check if a global variable exists?

JSLint is not passing this as a valid code: /* global someVar: false */ if (typeof someVar === "undefined") { var someVar = "hi!"; } What is the correct way?
Ebrahim Byagowi
  • 10,338
  • 4
  • 70
  • 81
76
votes
9 answers

I've Heard Global Variables Are Bad, What Alternative Solution Should I Use?

I've read all over the place that global variables are bad and alternatives should be used. In Javascript specifically, what solution should I choose. I'm thinking of a function, that when fed two arguments (function globalVariables(Variable,Value))…
Jonathon Oates
  • 2,912
  • 3
  • 37
  • 60
75
votes
2 answers

C/C++ global vs static global

Possible Duplicate: Static vs global I'm confused about the differences between global and static global variables. If static means that this variable is global only for the same file then why in two different files same name cause a name…
MoonBun
  • 4,322
  • 3
  • 37
  • 69
75
votes
5 answers

Python: logging module - globally

I was wondering how to implement a global logger that could be used everywhere with your own settings: I currently have a custom logger class: class customLogger(logging.Logger): ... The class is in a separate file with some formatters and other…
cwoebker
  • 3,158
  • 5
  • 27
  • 43
75
votes
3 answers

If a "Utilities" class is evil, where do I put my generic code?

I generally live by the rule that Global variables / functions are evil and that every piece of code should live in the class to which it pertains. This is a very easy rule to follow, and I believe that I haven't ever run into an issue with this…
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
72
votes
4 answers

Global constants in Groovy

It is often desired to declare constants at the top of a script that can be referenced anywhere else in the script. In Groovy, it seems that if you declare a constant using final then it isnot accessible in child scopes. What is the solution for…
justGroovy
  • 721
  • 1
  • 5
  • 3
72
votes
3 answers

MySQL wait_timeout Variable - GLOBAL vs SESSION

SHOW VARIABLES LIKE "%wait%" Result: 28800 SET @@GLOBAL.wait_timeout=300 SHOW GLOBAL VARIABLES LIKE "%wait%" Result: 300 SHOW SESSION VARIABLES LIKE "%wait%" Result:28800 I am confused by the results. Why does the last query give Result:28800…
Arunjith
  • 955
  • 3
  • 8
  • 12
72
votes
6 answers

Global variables in packages in R

I'm developing a package in R. I have a bunch of functions, some of them need some global variables. How do I manage global variables in packages? I've read something about environment, but I do not understand how it will work, of if this even is…
bskard
  • 1,040
  • 1
  • 9
  • 13
71
votes
10 answers

Reason for globals() in Python?

What is the reason of having globals() function in Python? It only returns dictionary of global variables, which are already global, so they can be used anywhere... I'm asking only out of curiosity, trying to learn python. def F(): global x …
user1632861