by levodelellis on 2/7/25, 8:28 PM with 65 comments
by sshine on 2/10/25, 8:23 AM
Your program must be really small and scoped for this to make sense.
Also, kudos for providing so many corner cases to avoid that are non-trivial to formulate.
It feels like one of those "it's not impossible to do right" cases.
I'll just cite one evaluation point from the post:
> It's extremely easy to use incorrectly.
by forrestthewoods on 2/10/25, 9:07 AM
I find the append_work especially egregious. Never ever use a bloody global for that! Goodness gracious me. What an absolute and utter waste that provides zero utility. If you have a job system just pass a damn handle to the job queue. Good lord.
If everyone spent a little effort to not use globals the world would be a much better place. The value add of globals when they are mildly useful is so inconsequential such that it's basically never worth the effort.
by metayrnc on 2/10/25, 8:46 AM
by Ragnarork on 2/10/25, 10:41 AM
However this just underlines again why globals are usually good to avoid. It takes rigor not to make mistakes that can completely mess up with program state.
I also strongly disagree with the benefits author advances for having global. Code feels actually less spaghetti when things are properly scoped and not accessible from everywhere, which hides extremely well dependencies and makes it way harder to reason about the code.
One famous exception to that is indeed logging (which in itself is based on global state when printing to stdout or stderr anyway).
And also
> on account of how few places your object can be stored to
you can store things in a single place, just once, without the need for globals.
by pjc50 on 2/10/25, 9:53 AM
Most languages make global and static variables thread-global by default, and making them thread-local is more work. I can see why, but that piece of language design causes a lot of global variable problems.
Also: you can simplify a lot of problems by deciding that something is going to be limited to n=1, whether that's variables or threads, and then a business reason comes along where you really want to have n=2. Suddenly every global is a source of regret.
by antithesis-nl on 2/10/25, 8:01 AM
No, not in the face of any sort of concurrency it doesn't...
by flohofwoe on 2/10/25, 9:22 AM
Immutable global state is fine to be accessible from the whole code base.
Pure functions are best of course, but you can't build real-world things entirely from pure functions.
The problematic approach lives somewhere inbetween, passing context/object references into deep callstacks isn't really a good solution for anything.
by progx on 2/10/25, 9:15 AM
by MattPalmer1086 on 2/10/25, 12:11 PM
We stick to these kinds of rules because most people are not great programmers all the time. It's just mostly better to do the safe and boring thing most of the time.
by banthar on 2/10/25, 10:51 AM
by matheweis on 2/10/25, 10:05 AM
I’m sorry but no. Humans are human and mistakes will be made. I’ve lost count of the number of esoteric bugs I’ve had to track down due to global state being changed and not put back properly.
If you have to qualify a pattern with rules that are easily forgotten or open to corner case bugs, it’s far better to just not use that pattern in the first place.
by Vanit on 2/10/25, 8:27 AM
by zombot on 2/12/25, 7:16 AM
by NooneAtAll3 on 2/10/25, 11:18 AM
let's = let us, it is for suggestions
lets = I let, he-she-it lets, it's the verb form
by IshKebab on 2/10/25, 8:08 AM
This is what you should do if you must use globals, but it doesn't really give any advantages.
by BoingBoomTschak on 2/10/25, 8:12 AM