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.
7914 questions
87
votes
6 answers

Global Variables in Dart

I try to create a Dart single page application. I have created a first custom element (custom-application) which contains the whole application. It has a container in it which is used to render views. And a side nav which will contain user…
T00rk
  • 1,603
  • 2
  • 11
  • 17
86
votes
11 answers

How to avoid global variables in JavaScript?

We all know that global variables are anything but best practice. But there are several instances when it is difficult to code without them. What techniques do you use to avoid the use of global variables? For example, given the following scenario,…
Josh Stodola
  • 77,975
  • 43
  • 178
  • 222
86
votes
6 answers

Are global variables in PHP considered bad practice? If so, why?

function foo () { global $var; // rest of code } In my small PHP projects I usually go the procedural way. I generally have a variable that contains the system configuration, and when I nead to access this variable in a function, I do…
KRTac
  • 2,401
  • 4
  • 20
  • 18
86
votes
19 answers

How to return an array in bash without using globals?

I have a function that creates an array and I want to return the array to the caller: create_array() { local my_list=("a", "b", "c") echo "${my_list[@]}" } my_algorithm() { local result=$(create_array) } With this, I only get an expanded…
helpermethod
  • 51,037
  • 60
  • 165
  • 263
85
votes
13 answers

PHP pass variable to include

I'm trying to pass a variable into an include file. My host changed PHP version and now whatever solution I try doesn't work. I think I've tried every option I could find. I'm sure it's the simplest thing! The variable needs to be set and evaluated…
user1590646
  • 851
  • 1
  • 6
  • 4
84
votes
6 answers

shared global variables in C

How can I create global variables that are shared in C? If I put it in a header file, then the linker complains that the variables are already defined. Is the only way to declare the variable in one of my C files and to manually put in externs at…
Claudiu
  • 206,738
  • 150
  • 445
  • 651
82
votes
3 answers

How to tell JSLint / JSHint what global variables are already defined

In my project we have some global variables that work as containers: MyProject.MyFreature.someFunction = function() { ... } So then I use that script across the site and JSLint / JSHint complains about that: 'MyProject' is not defined I know that…
Emiliano Zilocchi
  • 1,035
  • 1
  • 7
  • 12
81
votes
10 answers

Default values and initialization in Java

Based on my reference, primitive types have default values and Objects are null. I tested a piece of code. public class Main { public static void main(String[] args) { int a; System.out.println(a); } } The line…
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,134
  • 2
  • 20
  • 26
77
votes
5 answers

Compiler error: "initializer element is not a compile-time constant"

When compiling this code, I get the error "initializer element is not a compile-time constant". Can anyone explain why? #import "PreferencesController.h" @implementation PreferencesController - (id)init { self = [super init]; if (self) { …
76
votes
7 answers

Declaring variables without var keyword

At w3schools there is written: If you declare a variable, without using "var", the variable always becomes GLOBAL. Is it useful to declare global variable inside the function? I can imagine to declare some global variables in some event handler,…
xralf
  • 11,703
  • 34
  • 110
  • 167
75
votes
6 answers

ASP.NET MVC Global Variables

How do you declare global variables in ASP.NET MVC?
Beginner
  • 25,463
  • 62
  • 148
  • 228
74
votes
5 answers

Why does assigning to my global variables not work in Python?

I'm having terrible trouble trying to understand python scoping rules. With the following script: a = 7 def printA(): print "Value of a is %d" % (a) def setA(value): a = value print "Inside setA, a is now %d" %(a) print "Before…
Free Wildebeest
  • 6,562
  • 8
  • 35
  • 41
73
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
73
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
  • 45,925
  • 29
  • 140
  • 183