News for hackers. Stuff that matters.
Re:Global variables are bad (2013) by bluetomcat on Thursday July 26, 2018 @03:19AM (#17615662)
I'll play the devil's advocate. Global (shared) variables are fine when they represent non-instantiable program state and have descriptive names, with a clear semantic contract for mutating their state. Essentially, something like `the_world.rotation_speed` is a good example.
Re:Global variables are bad (2013) by mpartel on Thursday July 26, 2018 @03:25AM (#17615690)
What if you, one day, want multiple worlds for some reason?

I'm developing a game in Unity, and the programming culture around it encourages globals and singletons to an unhealthy degree. I pushed back against them heavily in our project. As a result, it was very easy to add a previously unplanned split-screen mode one day, thanks to the codebase (e.g. the UI and input modules) not assuming that there's only ever one local player.

Re:Global variables are bad (2013) by JimDabell on Thursday July 26, 2018 @04:07AM (#17615862)
> What if you, one day, want multiple worlds for some reason?

Write code for the application you want, not the set of all applications you might hypothetically want someday. Incur the cost of new features after you decide you want them, not before.

Re:Global variables are bad (2013) by mpartel on Thursday July 26, 2018 @04:14AM (#17615880)
In general I fully agree, YAGNI and all that, but from experience, mutable globals, unless used very sparingly, rigidify the codebase to a surprising degree and make it more error-prone, often for very little actual gain.

As with any software engineering guideline, there are exceptions. E.g. Java-style loggers are so ubiqitous and "not that mutable", that it's generally good for them to be global.

Edit: also from experience, suddenly wanting two of something where before there was only one, is not that uncommon.

Re:Global variables are bad (2013) by AstralStorm on Thursday July 26, 2018 @04:15AM (#17615884)
Global state is a maintenance cost from day 1, increasing over time.
Re:Global variables are bad (2013) by bluetomcat on Thursday July 26, 2018 @03:49AM (#17615791)
In the basic case when you don't need to clone the world, you avoid the passing down on the call stack of what is truly a global variable, disguised as a local instance. As a casual reader of such code, you might be deluded that `the_world` in `func1` is something different from `the_world` in `func2`, when they are the same.

If you plan to "virtualise" stuff later, instantiation is certainly the way.

Re:Global variables are bad (2013) by mpartel on Thursday July 26, 2018 @04:08AM (#17615864)
I'd rather readers of such code take the (IMO minor) extra effort to consider the possibility of the worlds being different rather than bake the assumption that they're the same into even more code. That's how globals spread around your codebase, little by little, until it's very hard to refactor away from them. Better to just say no from the start.

Also, if you have so many globals that passing them around as parameters / instance variables becomes truly tedious, that can be a valuable signal that the program is poorly structured.

Of course, none of this matters in small codebases, as long as they stay small.

Re:Global variables are bad (2013) by darekkay on Thursday July 26, 2018 @03:54AM (#17615808)
This is how Redux (Flux implementation) in JavaScript works. Basically the whole application state is maintained in a single, global variable. Also, every possible action has a global string ID constant.

Coming from Java and its best practices it seemed really odd at first, but it makes state management and debugging so much easier.

Re:Global variables are bad (2013) by rypskar on Thursday July 26, 2018 @03:56AM (#17615814)
>`the_world.rotation_speed`

Isn't properties for the globe by definition global?

Also, in most cases, would not the rotation speed of a world be a constant not a variable?