Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [globals]

The tag has no usage guidance.

6
votes
1answer
221 views

Is private global mutable state ever appropriate, namely when used to prevent API misuse?

I am writing a type checker for an ML dialect that involves generating "fresh" (new and unique) "type variables" (values representing unknowns). My strategy, and the strategy that seems to be used in ...
3
votes
2answers
109 views

How can I avoid using global variables in simple Javascript SPA?

To learn Javascript I am building a simple Single-Page Application. It is a basic slideshow app that needs only to process some basic settings and execute some behaviors. The basic structure of my ...
2
votes
1answer
183 views

I'm moving away from using global variables after 7 years, I need help figuring out the basics

Until recently I was a huge fan of global variables because they are simple and allow building solutions to problems quickly. At one point I had to make a slight change to my application: it needed ...
0
votes
1answer
109 views

How to avoid Global State with a world map?

This is a conceptual problem I'm having general difficulty with, and I'd like to look at an example that can hopefully help clarify this: using a map to represent an alterable game world. For ...
3
votes
1answer
250 views

Are externs (global variables) ever justifiable?

Under which circumstances would you deem it justifiable to use externs (i.e. global variables)? For example, in a system with 1 given state at a time, would it be appropriate to store this state in ...
0
votes
1answer
151 views

How to best pass along global configurations such as login data?

I have a code base consisting of multiple scripts and many functions. Everything is controlled from one master script (master.txt, let's say). I have a lot of "global" configuration files, for ...
0
votes
2answers
92 views

Efficiently computing and storing global state

I am working on a graph problem which is special in the sense that the nodes are computed only when required. This is because the graph is so huge that the entire graph won't fit in memory. So, in my ...
2
votes
3answers
261 views

How do Stack Machine store global vars?

How exactly do stack machines (both real and virtual stack machines) store global variables? I know that C(++) just compile it to the .data segment of a program's memory segmentation. Then there's ...
0
votes
1answer
88 views

Is there a better way than StringHelper?

I want to have functions that convert numeric values into strings in English words, to pluralize nouns, to inflect, break by words and so on. I will use these throughout a big project. Normally I ...
2
votes
3answers
274 views

Is “pass objects instead of required variables only” contradict with “avoid global variables”?

As I know, on one hand, the main stream opinion about global variable is, it is eval because it may make the program unpredictable : some unknown class or functions can change the value of the global ...
1
vote
2answers
129 views

Method manipulates global variables, mark const?

I wrote a class ShaderWrapper in C++, that wraps around OpenGL shaders. In the constructor I generate the actually OpenGL shader object, but this method only returns me a handle/id of type int. The ...
3
votes
3answers
625 views

Are we overlooking bad effects of global state in this design?

I’ve read the answers to Why is Global State so Evil?, and I think the negative consequences do not apply in this situation. However, that’s what everyone says just before they get hit by a falling ...
44
votes
15answers
18k views

Clean Code: Functions with few parameters [closed]

I read the first chapters of Clean Code by Robert C. Martin, and it seems to me it's pretty good, but I have a doubt, in one part it is mentioned that it is good (cognitively) that the functions ...
5
votes
2answers
3k views

Parametrize methods vs global variables

I have a very simple question that has been haunting me for a while when my code starts growing. Should parameters be replaced by global variables when they go through long routes of nested function ...
0
votes
0answers
567 views

Best practices for configuration files in global (or globally required) variables

A common pattern I use on my nontrivial programs is to have a class that parses out a config file (usually YAML), and makes the items in the values in the config available as properties on the class. ...
3
votes
1answer
287 views

Is global state evil if it doesn't change behavior?

I have a library in C where the API uses "objects" to interact with the application. These "objects" are opaque pointers that are created, destroyed, and modified via API calls. These "objects" do ...
1
vote
0answers
426 views

How do you handle time unit constants (second, minute, etc)?

I've been writing a lot of time related code recently, and in various files, I always end up re-defining things like var SECOND = 1000; var MINUTE = 60 * SECOND; This gets frustrating quickly. So I'...
2
votes
0answers
201 views

Avoiding global variables by using persistent variables in MATLAB

Disclaimer: Although I mention my use in matlab I think this is is more of a programming pattern question so I put it here. If I am incorrect I will be happy to go over to stackoverflow. I have a ...
203
votes
10answers
22k views

Is there a name for the (anti- ) pattern of passing parameters that will only be used several levels deep in the call chain?

I was trying to find alternatives to the use of global variable in some legacy code. But this question is not about the technical alternatives, I'm mainly concerned about the terminology. The ...
-2
votes
1answer
846 views

Global variable or parameter?

Is it correct that homeitem should be declared inside the OnLoad method and overgiven to all the render methods instead of just declaring it as a global variable and accessing it by all the render ...
245
votes
22answers
14k views

How are globals any different from a database?

I just ran across this old question asking what's so evil about global state, and the top-voted, accepted answer asserts that you can't trust any code that works with global variables, because some ...
3
votes
3answers
1k views

Are global variables okay to use in asynchronous programs?

This may be a stupid question, sorry. I've read about the drawbacks of global variables a lot on this site. I'm finally trying to increase my code quality for a large project that'll be reviewed by ...
0
votes
2answers
360 views

Is a global context stack a code smell?

I would like to hear arguments for or against a plan I am considering. I have a large network of nodes each of which has vector-valued properties. Accessing these properties (read or write) requires ...
4
votes
4answers
940 views

Are static global variables as bad as global variables? How to avoid using them?

In general, I know that global variables are bad and should be avoided. Are static global variables equally bad? In all my projects, I have heavily relied on static global variables. From design ...
0
votes
2answers
688 views

Without using Singletons, what is a good way to manage a global resource? [duplicate]

Unlike many libraries SDL and OpenGL are designed to provide global resources; you can access them at any time from any class. There are justifications for this: They are written in C, meant to be ...
1
vote
1answer
4k views

Memory usage of global versus local variables

I am a beginner in C coding. I'm writing a C code where a variable is required to be used by different functions. I want to ask whether to use a global variable and manipulate it through functions or ...
-4
votes
2answers
99 views

Rationale for modern languages implement changeable global variables [closed]

Common sense seems to be that usage of global variables is bad, in particular if they are used as real variables (i.e. changing state; e.g. Why is Global State so Evil?). How does it come then, that ...
4
votes
2answers
94 views

AutoIncrement Integer Column,Guid or PGuid, which to choose?

Guid has been a Global Unique Identification. Why does "PGuid" (Global Unique Identification) appear? 1) Is Guid NOT PERMENT for unique? (I sometimes wonder whether Guid is REALLY PERMENT generator ...
6
votes
3answers
3k views

C++ Extensible namespaces - how to force declarations back into global namespace

It is good programming style to include all necessary dependencies in a header that references them. Often this includes declarations that are placed in the STD & global namespaces (like cstdio). ...
3
votes
3answers
11k views

Are file-scope `static` variables in C as bad as `extern` global variables?

In C, you'd often/sometimes (as a matter of style) use a file-scope static variable where you'd use a private class member variable in C++. When scaling to multithreaded programs, simply adding ...
3
votes
1answer
2k views

Module level logger function

I've recently been assigned to a new project because I'm one of the only developers at my company who has used Python extensively in the past. My first task was to clean up the code base and make it ...
2
votes
1answer
232 views

How to avoid tying these two methods together with global mutable state?

I'm in the process of reading Code Complete 2 and learned that using global state is a no-no. What can I do to avoid using global state in this situation? Some background information: I have two ...
8
votes
1answer
296 views

Why create a Global-ish Object.create function?

I'm a fairly experienced programmer in the .NET and Java realms, and I've started reading up on JavaScript. I bought Douglas Crockford's "The Good Parts" book, and I'm immediately put off by a few ...
5
votes
1answer
2k views

Using macros to protect assignment to global variables

Because there is no language feature in C to protect assignment to global variables would you recommend doing something like this? Take this example: We have a module with the header file called ...
5
votes
2answers
5k views

Early destruction of objects in c++

I am not sure if this is the right forum of this question, but I will try here since this question about the c++ language. The problem: I use one global variable in my code which is a graphics object....
0
votes
3answers
450 views

Various methods manipulating same member variable versus each takes inputs and provides output [closed]

As software design paradigm which is better? to let various methods manipulate a member variable, or define each method or function to take some inputs and provide some outputs? For example class ...
1
vote
1answer
705 views

How can I avoid the “global $dbh” anti-pattern?

This is what most of my database-related library code looks like: lib.php <?php $dbh = new PDO(...); function doSomeDatabaseThing() { global $dbh; return $dbh->doStuff(); } function ...
3
votes
3answers
2k views

Separating Code into Smaller Files in C

I am in the process of cleaning up my code and making it easier to maintain. I am doing this by turning my 5000+ line file into separate smaller files. I have successfully created separate source and ...
3
votes
1answer
2k views

How to avoid global state without large function signatures?

So, there are some good answers to this question, namely: Why is Global State so Evil? I have a specific question about the alternatives Mikera proposed as the answer with the second most up-votes. ...
1
vote
1answer
537 views

Doesn't an event bus affect global state?

"Minimize mutable global state" is a more or less accepted program design principle. The reason often cited is that having global state can affect two components that don't are seemingly unrelated. In ...
3
votes
1answer
1k views

Where to keep global variables in Windows Forms App

I have here a Vb.Net employee management application. It has employees, managers, teams, trainings and so on. The way the application is set up now, data is read from the database in the startup form ...
2
votes
3answers
2k views

Setting global parameters: is this a reasonable use of const_cast and volatile?

I have a program that I run repeatedly with various parameter sets. Different parameters are used in different parts of the program (including different source files). The same parameter may also be ...
1
vote
1answer
8k views

Storing session in a static variable?

I'm currently checking a older piece of C# code used to maintain a login session. public static ZRWebserviceSession CurrentSession { get { return _currSession; } set { _currSession = value; } } ...
1
vote
1answer
5k views

Global variables in javascript

I don't consider myself a highly skilled javascript developer, but I thought I got one thing right for sure: don't create global variables unless you really have to. I've been learning angularjs ...
1
vote
2answers
227 views

Dependency injection for request variables in a web application

I'm working a PHP 5.5/MySQL web application. If a class depends partly on some global GET and POST variables for its members, should I just refer to them directly from within the class, or should I ...
5
votes
2answers
1k views

Use a global variable, a singleton, or something else

Preface: I am working in PHP (Abandon hope all ye who enter here). Background: There exists a large set of global functions in PHP, a number of which are miscellaneous system calls, like sleep (and ...
3
votes
2answers
217 views

messaging class and global state

This is a front end project that is using socket.io for all communication with the server. This allows for push which is good, but recently we have been asked to provide a pause button. The pause ...
2
votes
1answer
160 views

Is using a global to maintain the currently logged in user bad practice?

I'm doing some android development in a relatively inexperienced group, and all of the data used by our application is managed in a database. There are several instances where we'll need to pull info ...
1
vote
2answers
352 views

In Ruby, change global in thread safe block

In Ruby, I have a use case for a few common configuration options, e.g. NOOP, TRACE, SILENT. Right now I am using local vars instead of globals and passing them around all over the place and it's a ...
2
votes
2answers
220 views

Replacing static classes with global state to a series of POJOs, beginning to feel like an anti-pattern

I'm trying to refactor some code, and one of the major changes is to remove the (ab)use of static classes to give global state. I've tried to split up some of the 'global state' functionality with ...