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
44
votes
6 answers

Disable global variable lookup in Python

In short, the question: Is there a way to prevent Python from looking up variables outside the current scope? Details: Python looks for variable definitions in outer scopes if they are not defined in the current scope. Thus, code like this is liable…
MB-F
  • 22,770
  • 4
  • 61
  • 116
44
votes
9 answers

Why are global variables considered bad practice?

I keep seeing warnings not to use global variables in JavaScript, but it seems that the only reason people say that is because the clogs up the global namespace. I can imagine this being easily fixed by putting all of the variables into one big…
user1318416
  • 729
  • 1
  • 6
  • 11
43
votes
5 answers

Using global variables in Jenkins Pipeline

I have tried all sort of ways but nothing seems to be working. Here is my jenkinsfile. def ZIP_NODE def CODE_VERSION pipeline{ /*A declarative pipeline*/ agent { /*Agent section*/ // where would you like to run the code …
43
votes
2 answers

Are global variables in C++ stored on the stack, heap or neither of them?

Initially I was pretty sure that the correct answer had to be "None of them", since global variables are stored in the data memory, but then I've found this book from Robert Lafore, called "Object Oriented Programming in C++" and it clearly states…
Edoardo Meneghini
  • 558
  • 1
  • 7
  • 12
43
votes
2 answers

Are there global variables in R Shiny?

How do you declare global variables in with R Shiny so that you do not need to run the same pieces of code multiple times? As a very simple example I have 2 plots that use the same exact data but I only want to calculate the data ONCE. Here is the…
user3022875
  • 8,598
  • 26
  • 103
  • 167
43
votes
5 answers

PHP session side-effect warning with global variables as a source of data

I'm trying to host a PHP web site that was given to me. I see this warning: Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not…
Zack Peterson
  • 56,055
  • 78
  • 209
  • 280
43
votes
2 answers

static and extern global variables in C and C++

I made 2 projects, the first one in C and the second one in C++, both work with same behavior. C project: header.h int varGlobal=7; main.c #include #include #include "header.h" void function(int i) { static int a=0; …
Cristi
  • 1,195
  • 6
  • 17
  • 24
42
votes
4 answers

When are static and global variables initialized?

In C++ I know static and global objects are constructed before the main function. But as you know, in C, there is no such kind initialization procedure before main. For example, in my code: int global_int1 = 5; int global_int2; static int…
Zachary
  • 1,633
  • 2
  • 22
  • 34
42
votes
3 answers

How to programmatically set a global (module) variable?

I would like to define globals in a "programmatic" way. Something similar to what I want to do would be: definitions = {'a': 1, 'b': 2, 'c': 123.4} for definition in definitions.items(): exec("%s = %r" % definition) # a = 1,…
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
42
votes
8 answers

Global javascript variable inside document.ready

Which is the right way of declaring a global javascript variable? The way I'm trying it, doesn't work $(document).ready(function() { var intro; if ($('.intro_check').is(':checked')) { intro = true; $('.intro').wrap('
Alex
  • 7,538
  • 23
  • 84
  • 152
41
votes
4 answers

Create global map variables

I need a little help regarding creating a global map variable in Go. What I have done is as follows: package ... import( ... ) ... type ir_table struct{ symbol string value string } var ir_MAP map[int]ir_table Since I am not…
progfan
  • 2,454
  • 3
  • 22
  • 28
41
votes
1 answer

Globals variables and Python multiprocessing

Possible Duplicate: Python multiprocessing global variable updates not returned to parent I am using a computer with many cores and for performance benefits I should really use more than one. However, I'm confused why these bits of code don't do…
user1475412
  • 1,659
  • 2
  • 22
  • 30
40
votes
8 answers

Why does WordPress still use addslashes(), register_globals() and magic_quotes?

In order to gain more experience in Wordpress I delved into its code base to study its inner working and its workflow, and I was quite astonished when I saw that: They implement register_globals (an excerpt from wp-includes/class-wp.php): // The…
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
40
votes
3 answers

How to cleanly deal with global variables?

I have a number of aspx pages (50+). I need to declare a number(5-7) of global variables in each of these pages. Variables in one page independent of the other pages even though some might be same. Currently I am declaring at the page top and…
kheya
  • 7,546
  • 20
  • 77
  • 109
40
votes
6 answers

Variable not accessible when initialized outside function

When I use code like this, it works fine: function removeWarning() { var systemStatus = document.getElementById("system-status"); systemStatus.innerHTML = ""; } function indicateInvalidUsername() { var systemStatus =…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699