TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

I Like and Use Global Variables

36 pointsby levodelellis3 months ago

16 comments

sshine3 months ago
There is something beautiful about global, mutable state:<p>Your program must be really small and scoped for this to make sense.<p>Also, kudos for providing so many corner cases to avoid that are non-trivial to formulate.<p>It feels like one of those &quot;it&#x27;s not impossible to do right&quot; cases.<p>I&#x27;ll just cite one evaluation point from the post:<p>&gt; <i>It&#x27;s extremely easy to use incorrectly.</i>
评论 #42998329 未加载
评论 #43009843 未加载
评论 #42998433 未加载
forrestthewoods3 months ago
Hard disagree. Except for, begrudgingly, logging those are all bad uses and you shouldn&#x27;t do them.<p>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.<p>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&#x27;s basically never worth the effort.
metayrnc3 months ago
I think the hatred of global variables comes from the fact that they are hard to use correctly. This article also makes the point that to use correctly you need to follow a bunch of rules.
评论 #42998428 未加载
评论 #42998846 未加载
评论 #43009910 未加载
Ragnarork3 months ago
I commend author for not shying away from writing such a take on one of the generally assumed consensus about code.<p>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.<p>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.<p>One famous exception to that is indeed logging (which in itself is based on global state when printing to stdout or stderr anyway).<p>And also<p>&gt; on account of how few places your object can be stored to<p>you can store things in a single place, just once, without the need for globals.
pjc503 months ago
&gt; If you&#x27;re using threads, global and static variables should be thread local. If they are not then the discussion becomes about sharing data across threads and synchronization, which is a different topic<p>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.<p>Also: you can simplify a lot of problems by deciding that something is going to be limited to n=1, whether that&#x27;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.
antithesis-nl3 months ago
&gt; For example, counter() keeps increments consistent<p>No, not in the face of any sort of concurrency it doesn&#x27;t...
评论 #42998322 未加载
评论 #42998098 未加载
评论 #42998521 未加载
评论 #42998410 未加载
评论 #42997984 未加载
flohofwoe3 months ago
IMHO mutable global state is totally fine if it is scoped to the current module or source file. Of course then it&#x27;s not really &#x27;global&#x27; anymore ;)<p>Immutable global state is fine to be accessible from the whole code base.<p>Pure functions are best of course, but you can&#x27;t build real-world things entirely from pure functions.<p>The problematic approach lives somewhere inbetween, passing context&#x2F;object references into deep callstacks isn&#x27;t really a good solution for anything.
评论 #42999582 未加载
progx3 months ago
I Like and Use Global Variables, then I grew up. ;-)
评论 #42998418 未加载
评论 #42998601 未加载
MattPalmer10863 months ago
Well sure, if you&#x27;re a great programmer you can do all kinds of things that are potentially dangerous, like using globals or lots of goto for control flow.<p>We stick to these kinds of rules because most people are not great programmers all the time. It&#x27;s just mostly better to do the safe and boring thing most of the time.
banthar3 months ago
You can make globals thread safe by using thread locals. You can make methods using them reentrant by carefully saving and restoring state. What about exceptions? Any exception from `process()` is going to leave this global state in a total mess.
评论 #43009922 未加载
matheweis3 months ago
&gt; A Few Rules For Using Globals: &gt; If you change observable state, restore it<p>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.<p>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.
评论 #42998792 未加载
评论 #43009954 未加载
评论 #42998910 未加载
评论 #42998823 未加载
Vanit3 months ago
You may as well just use a singleton pattern if you&#x27;re going to do this, and at least that&#x27;s easier to maintain if your use cases change.
评论 #42998462 未加载
评论 #42998407 未加载
评论 #42998117 未加载
评论 #43010012 未加载
zombot3 months ago
There will always be people who have invincibly bad taste.
NooneAtAll33 months ago
author, if you are here<p>let&#x27;s = let us, it is for suggestions<p>lets = I let, he-she-it lets, it&#x27;s the verb form
IshKebab3 months ago
&quot;Cool. Now we need two work lists.&quot;<p>This is what you should do if you <i>must</i> use globals, but it doesn&#x27;t really give any advantages.
评论 #42998352 未加载
BoingBoomTschak3 months ago
The hatred of global variables for the sake of it is something that Common Lisp and its earmuffs un-brainwashed in me, thankfully.