Skip to main content

Timeline for Why is Global State so Evil?

Current License: CC BY-SA 3.0

22 events
when toggle format what by license comment
Feb 18, 2023 at 14:52 comment added DeadMG @user253751 There's no need to take to its extreme. A lack of knowledge about the code is relative. There might be bugs in my code I don't know about. I could have state that has unexpected values. But there's a difference between "I can't prove that's not true" and "I'm inviting it and maximizing the probability of it". Being unable to eliminate the situation completely doesn't mean you should just give up or let the worst come. If all you can do is minimize things you don't know about your program, then that's exactly what you should do.
Feb 16, 2023 at 23:26 comment added Stack Exchange Broke The Law @DeadMG yeah, but taken to its extreme, you don't really know anything about the code, such as the fact it has global state in it
Feb 16, 2023 at 23:23 comment added DeadMG @user253751 Studying the code doesn't tell you what the state is. It tells you what you think the state should be. Those are not the same thing. Additionally, studying the code in that kind of detail is incredibly painfully expensive because you would have to know your whole damn codebase to figure it out.
Feb 15, 2023 at 18:32 comment added Stack Exchange Broke The Law I don't like the "you don't know what the state is" answer because you do know, if you study the code - just like everything else about the code. Points 1, 2, 4 and 5 are better.
Nov 15, 2017 at 18:34 comment added Jo So So you can easily find out the locations where you inject state, but you couldn't find out if these exact locations would use the global? That's ridiculous. If anything, opposite is true, because accesses to global variables happen always by the same name. Which can't be said of injected state that is stored as local member variables.
Nov 11, 2017 at 18:40 comment added DeadMG And frankly, you seem to be under the assumption that I think that injecting mutable state through DI is totally fine. It's not really fine. It's just not as bad as random global variables. You'r'e absolutely right that many of the same issues can occur if used unwisely.
Nov 11, 2017 at 18:37 comment added DeadMG @Pacerier: The IDE and ctr+f will only work if you have all the source for every module that consumes the global, which is quite optimistic. Apart from anything else, first, you'd need to actually know who consumes the global. And I sure hope that there's no code that e.g. dynamically loads symbols or can produce references by macro expansion or any other of a thousand horrible ways to make this difficult to work with.
Nov 11, 2017 at 18:34 comment added DeadMG @Pacerier: There's two key differences. The first is scale. If you inject a dependency into 100 classes, you know you have 100 classes to change. A global can affect the entire application- e..g 100,000 classes. The second is clarity- it's clear when you inject a parameter that you are adding that dependency, but when you access a global, there's no evidence on the caller's side that you have taken this dependency.
Sep 19, 2017 at 18:11 comment added Pacerier @DeadMG, Re "..You must remember to call SET_MAGIC_VARIABLE..", don't be silly, there's no need for that. You call a global function and it takes care of all that.
Sep 19, 2017 at 18:08 comment added Pacerier @DeadMG, Re "..If you ever change anything to do with your global, all your code needs changing..", Dude, it's the exact same thing with dependency injection. You have an obj that is injected in a hundred classes or is a parameter for a hundred functions, and you decide to change that obj, guess what happens?
Sep 19, 2017 at 18:06 comment added Pacerier @DeadMG, Re "..really hard to test..", Not true if you are smart about it. In fact, an IDE could tell you which variables in a class are global and which are not, effectively it's no different from dependency injection.
Sep 19, 2017 at 18:03 comment added Pacerier @DeadMG, Re "..is always completely unreliable..", the same goes for dependency injection. Just because you made it a parameter doesn't mean that the obj is guaranteed to have a fixed state. Somewhere down the function you could have make a call to another function that modifies the state of the injected variable at the top.
Sep 19, 2017 at 18:02 comment added Pacerier @DeadMG, Re "..hidden dependencies..", they aren't hidden if you got an IDE and/or Ctrl-F.
Dec 21, 2016 at 11:15 history wiki removed yannis
May 17, 2012 at 19:21 history made wiki Post Made Community Wiki by Mike Samuel
May 12, 2012 at 2:09 comment added DeadMG @MarkJ: I hope that nobody ever, say, shadowed the name.
May 11, 2012 at 20:04 comment added sleske @MarkJ: You don't grep for a type, you use a static analysis tool/IDE to find it for you. As to "grep": Ever tried grepping for a global named "config"? Good luck! And the question does not say anything about the values being read-only (maybe the conf can be changed at runtime).
May 11, 2012 at 19:24 comment added MarkJ Nitpicking: Why is it easier to grep for a type than a global? And the question is about read-only globals, so point 2 and 3 aren't relevant
May 10, 2012 at 23:52 comment added sleske @Coder: Note that the sane alternative to a global is not "config readers from 1000x places in the code", but one config reader, which creates a config object, which methods can accept as a parameter (-> Dependency injection).
May 10, 2012 at 23:23 comment added Coder Well, you can have a global config class which encapsulates the locking, and IS designed to change state at whatever time possible. I would choose this approach over instantiating config readers from 1000x places in the code. But yes, unpredictability is the absolutely worst thing about them.
May 10, 2012 at 19:47 history edited DeadMG CC BY-SA 3.0
added 53 characters in body
May 10, 2012 at 19:42 history answered DeadMG CC BY-SA 3.0