3 ms·
The stdlib does include some globals/statics, for good reasons (the default logger, the default http mux). I'm always wary about advice that says "you must nev
by marcus_holmes 9y ago
The stdlib does include some globals/statics, for good reasons (the default logger, the default http mux).
I'm always wary about advice that says "you must never use {this tool provided by a language}". Never say never ;)
Globals can be and are useful in some circumstances. Having a blanket ban on globals because "globals are bad mmkay" is not useful.
I draw this line at "do I need to mock this for testing?". If I need to mock it to run tests, then it shouldn't be a global and should be DI'd (so I can DI the mock). If I'm not going to mock it, then it's probably fine to leave it as a global.
The Db and Logger are perfect examples: I frequently mock the Db connection, so I can test sql output. I almost never mock the logger, because I only use it to output meta information. So having a global logger is OK.
YMMV...
- lpghatguy 9y agoShouldn't need a DB connection to create strings. Sometimes want to turn off, scope, or classify logging especially during testing. When these new levels of granularity are desired in your project, switching from globals is no fun.