5 ms·
I agree that going to the extreme of 100% eliminating global mutable state is going to fail for practical purposes, but we don't need to go that far to benefit.
by kibwen 9y ago
I agree that going to the extreme of 100% eliminating global mutable state is going to fail for practical purposes, but we don't need to go that far to benefit.
I think of the code that I wrote when I first started programming. Nearly every variable was global, because I simply couldn't be bothered to think about where it could be accessed from. But nowadays I can't remember the last time that I used a global variable, and I'm no worse off for it. For beginners it's a tempting tool to turn to, and we need to continue teaching aversion to it. Like `goto`, we can both consider it harmful and also happily allow its use when well-considered.
(Historical fact: very ancient versions of Rust didn't have global state at all, as Graydon was hoping to find a way to avoid ever needing to use it; eventually he was persuaded otherwise. Though global mutable state in Rust still requires the `unsafe` keyword to access due to the risk of concurrent access, so it's rare to see it in the wild (and quite obvious when it is).)
- jimmaswell 9y agoYou really never use singletons or other static global objects? I use them fairly often, mostly in game dev.
- ad133 9y agoGame dev is different than back-end server dev, is different from front-end web or desktop dev. For example, in back-end Java dev, I never use singleton objects in the truest sense of the pattern; singletons cannot be mocked out to test units in isolation. Instead, instantiate it with a dependency injection container and let your framework (Guice, Spring) wire things together. In effect, your global state becomes contained within a single context. Nothing stopping you having two contexts in the same runtime environment though. I find static methods are okay if they are concise and have no state; in particular if I'd never want to test the caller without it making the call to the static method anyway. This is becoming more important with Java 8's lambdas and needing to write helper methods. On the other hand, the cost of doing this might not be worthwhile for a game dev? Do game devs write unit tests? I have no idea.
- virtualized 9y agoGame developers don't write tests. They pick up habits along the way and keep them for a long time. The code might be crap, but they know their crap very well and can debug it. There is less need for tests because most code never rarely refactored or being touched at all. It's already in the familiar style, why change anything?
- tigershark 9y agoWhen I started programming I remember just a mess of line numbers and GOTO statements in which there was no global variable at all... because literally everything was global. I like much more my code now with glorified singletons called IoC containers, extensions methods that are just global static methods and all the rest of the wagons... ...just joking, I'm trying to write more F# now to finally complete my journey procedural->object oriented->functional
- sago 9y agoI agree. But here's the problem I have. If we don't want people to use global variables, the advice should be 'don't use global variables'. There is no need to wrap it up in 'avoid global mutable state', when using global mutable state is unavoidable in many cases and the best engineering solution in many others. At the point you have to nuance it to 'reduce your code's exposure use global mutable state, accessing it through mockable interfaces, and defensively checking its values' to mean 'don't use global variables', it may be simpler to say what we mean. For example: there is a comment lower down in this thread of someone confused by the advice in the context of the game engine. I've worked with hundreds of game engines, every one of them has global mutable state.
- otakucode 9y agoIn many languages, variables are not necessarily state. Global variables are fine if they're global constants which can not (not just 'should not') be modified.