Questions tagged [globals]
The globals tag has no usage guidance.
82
questions
0
votes
3
answers
322
views
Does turn "global state" into "passing parameter" increase the whole system coupling or not?
As far as I know, coupling is about counting number of other classes in a class, so for the following program that emulates a UI program that shows "WelcomePage" at start and also pops a ...
2
votes
4
answers
726
views
Why blaming "global state", instead of my user requirements, makes program state unpredictable?
According to Why is Global State so Evil?, I know I should not allow the existence of global state, one of the reasons is so called "it makes program state unpredictable".
However, I'm not ...
0
votes
0
answers
40
views
Storing SVG settings in a global variable to retain its contents during resize - acceptable?
In the solution I've been asked to maintain (Meteor based app), there are several responsive SVG charts. When the page is loaded, settings for each chart are stored using the following:
<input ...
22
votes
5
answers
4k
views
Should I use a global logging variable?
Over and over again we're told, "globals are bad" and with good reason. However, I'm working with a logger that needs to be accessible everywhere in the program. Why shouldn't I create a ...
1
vote
4
answers
490
views
Passing arrays as global variables instead of pointers in C
I'm required to write the Low-Level Requirements of a Software Component which shall perform signal processing over arrays of 200k elements of integers/floats which lives in the main memory of the ...
0
votes
3
answers
330
views
Why is Dependency Injection called "alternative of global state"?I think global state still exists
According to Why is Global State so Evil?, I believe we should avoid global state, so suppose I have an App that count user clicks in all pages like it:
public class GlobalState{
public int ...
12
votes
4
answers
3k
views
How to initialize the same global resources from multiple modules independently?
I encountered the following situation: I have two modules, TokenService and Wifi which are initialized from main(). The modules themselves don't know of the existence of each other and function ...
3
votes
2
answers
331
views
How to implement a counter without global state?
I have read many blogs and stack exchange posts about global state (usually) being bad practice. I'm now trying to avoid this where possible in my code, but I'm running into a case where I don't know ...
1
vote
2
answers
212
views
Using class attributes as globals in Python - is there a catch?
I have found myself in the habit of using code like this.
class glb:
"just for holding globals"
args = None # from argparse
conf = None # from configparser
def main():
......
0
votes
1
answer
250
views
How to handle root paths in a custom package?
I am writing a custom python package, which produces some files in a certain directory. This directory I call root_path and should be set by the user. So basically, it should be a conf variable but ...
0
votes
1
answer
207
views
Plugin system design core component access: Common practice?
I wonder how to design a good plugin architecture in C++/Qt. The main concern I have is about dependency injection vs globals when it comes to the core components. Plugins should have access to ...
2
votes
1
answer
836
views
What are the best practices to follow when using global variables when you must use them?
What are the best practices when using global variables?
Normally, the common answer to this is to avoid using global variables and use local variables, properties and arguments to pass data around. ...
4
votes
1
answer
909
views
How state stores are different from "global variables"?
Context: I am developing an amateur web application (with Vue.js), made up of components, and when reading about state management, the recommended solution is to use a store that gets updated by the ...
0
votes
1
answer
2k
views
When should I use a global variable module in Python?
In Python projects, if your project spans multiple Python modules and you need to pass some data between the modules, as far as I know, there's 2 main ways:
Pass the data as function arguments
Create ...
-1
votes
4
answers
814
views
Is using member variables as bad as using globals?
Is this an acceptable use of member variables:
One method sets the variables so that another method can use them. Would it be better to return the value in a list or tuple from func1? If I don't do ...
4
votes
2
answers
1k
views
Storing global variables in a database
I have several global variables that I need to store in a database. These variables rarely change, but when they do, all installed instances of the application must updated to those changes, which is ...
5
votes
4
answers
347
views
Use of globals in stack-based virtual machine implementation
I'm implementing a stack-based virtual machine in C.
The following variables are used by pretty much every function:
memory array
various pointers to memory offsets
program counter
stack
stack ...
0
votes
2
answers
413
views
How should a "global" resource be handled in Java?
It's a pretty simple question really, how should one properly declare and use a global resource without violating Java's OOP principles?
When I'm writing in C, I would declare a struct in global space....
2
votes
2
answers
175
views
Better way than repeatedly passing the same parameters?
I have some methods in my code that essentially hide/show some Views, like showTitleHideBody(), showBodyHideTitle(), etc. They just change their Views' (tvTitle, tvBody) visibility.
Initially those ...
4
votes
3
answers
1k
views
Why is global state not considered as an example of dependency injection?
According to Why is Global State so Evil?, as I understand, global state is bad and I should use dependency injection instead. That means, for example, a mobile app that use bundles of app level data ...
-4
votes
1
answer
977
views
How to refactor Global variables?
I found this previous question that addresses the issue. The question is: How do I deal with global variables in existing legacy code (or, what's better, global hell or pattern hell)?
My question ...
0
votes
4
answers
666
views
How to avoid global mutable variables within a class?
I know the fact that global mutable variables are bad as they can be accidentally modified and make testing difficult. However, there are situations when a class needs to cache some information, for e....
4
votes
0
answers
316
views
Global variables and common block management in Fortran
Background
The high level overview of my situation is described here. I am breaking it apart into smaller, specific questions, such as this one, regarding extensive use of global variables in a ...
2
votes
2
answers
225
views
Is registry pattern a valid use of global state?
For example, I want to convert json from server to generic object: Shape, as follow:
{
"type":"Circle",
"radius":"3",
},
{
"type":"Rectangle",
"width":"5",
"height":"6",
},
using ...
6
votes
5
answers
1k
views
Are "open-closed principle" and "less coupling" rationales to use global state?
Consider I'm writing a mobile app with user-login feature, using a framework which can be simplified like that:
class UserData{
static token="";
static name="";
static balance=0;
}
class ...
2
votes
3
answers
315
views
Is this design with globals bad?
I designed a Snakes and Ladders game. I divided into a reasonable amount of classes and I believe I did a good job regarding the OOP design.
I have one question about a decision I made for the design,...
0
votes
2
answers
515
views
Is the global state believed to be evil because of its nature or mostly due to its usual, no-rules usage?
I don't dislike global state, but that could be due to the lack of experience. I was thinking about what the usual implementation of global state is:
A big variable where data flows in a non-...
1
vote
1
answer
158
views
Are "I'm writing android applications" and "follow open-closed principles" reasons to allow global state?
For example, suppose I have an android application, in simple, there are 3 activities :
MainActivity -> Main2Activity -> Main3Activity
,which user can visit them in sequence (eg: by button ...
5
votes
2
answers
2k
views
OpenGL and global state
It is generally understood in software engineering that global state is bad. However, OpenGL has been designed very much embracing the concept of global state. many of the things you modify will ...
5
votes
1
answer
375
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 ...
4
votes
2
answers
1k
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 ...
3
votes
1
answer
879
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
1
answer
360
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 ...
4
votes
1
answer
550
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
1
answer
358
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
2
answers
104
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
3
answers
2k
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
1
answer
123
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 ...
1
vote
3
answers
574
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 ...
2
votes
2
answers
148
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 ...
6
votes
3
answers
2k
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 ...
53
votes
15
answers
35k
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 ...
15
votes
2
answers
18k
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
0
answers
1k
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.
...
2
votes
1
answer
521
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 ...
2
votes
1
answer
2k
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'...
3
votes
0
answers
374
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 ...
238
votes
10
answers
35k
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
1
answer
2k
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 ...
264
votes
22
answers
18k
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 ...