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
69
votes
8 answers

C++ singleton vs. global static object

A friend of mine today asked me why should he prefer use of singleton over global static object? The way I started it to explain was that the singleton can have state vs. static global object won't...but then I wasn't sure..because this in C++.. (I…
Tom
68
votes
2 answers

What is the difference between "option" and "set CACHE BOOL" for a CMake variable?

Is there any difference between the following two? set(FOO true CACHE BOOL "description") option(FOO "description" ON) Documentation: set - option Background: Even if I have been using CMake for a while, I only noticed the option command today and…
Antonio
  • 19,451
  • 13
  • 99
  • 197
68
votes
4 answers

How do you use global variables or constant values in Ruby?

I have a program that looks like: $offset = Point.new(100, 200); def draw(point) pointNew = $offset + point; drawAbsolute(point) end draw(Point.new(3, 4)); the use of $offset seems a bit weird. In C, if I define something outside of any…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
67
votes
5 answers

Can't access global variable inside function

This (simplified version of my code) doesn't work: '); function foo(){ $child = $sxml->addChild('child'); } foo(); ?> Why? I want to access $sxml because I want to log errors…
Camilo Martin
  • 37,236
  • 20
  • 111
  • 154
66
votes
8 answers

How to define global variable in Google Apps Script

I see most examples from Google is they use only functions in a single giant script. e.g. https://developers.google.com/apps-script/quickstart/macros But in our style, we usually write all functions under a single namespace, such as MyCompany =…
Ryan
  • 10,041
  • 27
  • 91
  • 156
64
votes
2 answers

List all PHP variables

Is it possible to dump all global variables in a PHP script? Say this is my code:
Salman A
  • 262,204
  • 82
  • 430
  • 521
63
votes
2 answers

Does C++ call destructors for global and class static variables?

From my example program, it looks like it does call the destructors in both the cases. At what point does it call the destructors for global and class-static variables since they should be allocated in the data section of the program stack?
user236215
  • 7,278
  • 23
  • 59
  • 87
63
votes
5 answers

What does the "$" character mean in Ruby?

Been playing with Ruby on Rails for awhile and decided to take a look through the actual source. Grabbed the repo from GitHub and started looking around. Came across some code that I am not sure what it does or what it references. I saw this code…
62
votes
5 answers

C++ Global variable declaration

What I want to do is just to define a variable in a header file and use it on two different cpp files without redefinition that variable each time I include that header Here is how I tried : Variables.h #ifndef VARIABLES_H // header guards #define…
Shahriyar
  • 1,483
  • 4
  • 24
  • 37
62
votes
16 answers

When is it ok to use a global variable in C?

Apparently there's a lot of variety in opinions out there, ranging from, "Never! Always encapsulate (even if it's with a mere macro!)" to "It's no big deal – use them when it's more convenient than not." So. Specific, concrete reasons (preferably…
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
58
votes
4 answers

Correct Use Of Global Variables In Python 3

Which is the correct use of global variables in Python 3?: 1) Stating global VAR_NAME once in the core script (not within a function) and then simply referring to the variable as VAR_NAME everywhere else 2) Stating global VAR_NAME once within every…
Eden Crow
  • 14,684
  • 11
  • 26
  • 24
58
votes
6 answers

Global variables for node.js standard modules?

I know that global variables are bad. But if I am using node's module "util" in 40 files in my framework, isn't it better to just declare it as a global variable like: util = require('util'); in the index.js file instead of writing that line in 40…
ajsie
  • 77,632
  • 106
  • 276
  • 381
56
votes
7 answers

Should I use window.variable or var?

We have a lot of setup JS code that defines panels, buttons, etc that will be used in many other JS files. Typically, we do something like: grid.js var myGrid = ..... combos.js var myCombo = ..... Then, in our application code,…
cbmeeks
  • 11,248
  • 22
  • 85
  • 136
55
votes
3 answers

Modifying global variables in Python unittest framework

I am working on a series of unit tests in Python, some of which depend on the value of a configuration variable. These variables are stored in a global Python config file and are used in other modules. I would like to write unit tests for different…
badzil
  • 3,440
  • 4
  • 19
  • 27
55
votes
2 answers

Is it possible to define global variables in a function in Python

How do I declare a global variable in a function in Python? That is, so that it doesn't have to be declared before but can be used outside of the function.
WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138