Questions tagged [globals]

79 questions
422
votes
17 answers

Why is Global State so Evil?

Before we start this, let me say I'm well aware of the concepts of Abstraction and Dependency Injection. I don't need my eyes opened here. Well, most of us say, (too) many times without really understanding, "Don't use global variables", or…
Madara's Ghost
  • 8,787
  • 9
  • 25
  • 33
264
votes
22 answers

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 other code somewhere else might come along and…
Mason Wheeler
  • 82,151
  • 24
  • 234
  • 309
234
votes
10 answers

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 obvious solution is to pass a parameter into the…
RubenLaguna
  • 1,842
  • 2
  • 12
  • 11
53
votes
15 answers

Clean Code: Functions with few parameters

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 should have as few parameters as possible, it even…
OiciTrap
  • 729
  • 1
  • 7
  • 12
21
votes
5 answers

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 global logging object? The alternative seems to be to pass the…
StaticMethod
  • 345
  • 2
  • 4
14
votes
2 answers

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 calls? I understand that global environment can…
AFP_555
  • 349
  • 1
  • 2
  • 9
12
votes
4 answers

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 completely independent, yet they need access to the…
glades
  • 315
  • 2
  • 6
11
votes
4 answers

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 thread_local in C11 or the long-supported extension…
user186330
8
votes
4 answers

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 perspective, how can I avoid using them. Ex Use case…
Sandeep
  • 179
  • 1
  • 6
8
votes
3 answers

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). However, this creates problems when a second…
bgulko
  • 101
  • 1
  • 6
8
votes
1 answer

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 things. One is modifying the fundamental types…
Casey
  • 189
  • 2
7
votes
2 answers

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. The thing is that when I shut down the program I…
patrik
  • 386
  • 1
  • 3
  • 11
6
votes
5 answers

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 Main{ constructor(){ …
aacceeggiikk
  • 707
  • 1
  • 5
  • 6
6
votes
3 answers

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 piano and my software architecture lessons have…
6
votes
1 answer

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 more Pythonic - while doing so I came across…
Dan Oberlam
  • 1,271
  • 1
  • 9
  • 21
1
2 3 4 5 6