Live data from Hacker News

Global variables are not the problem

codestyleandtaste.com

71–80 of 165 posts

Re: Global variables are not the problem

#71
mutable global variables are intrinsically incompatible with a multithreaded environment. Having mutable shared state is never the right solution, unless you basically can live with data races. And that's before taking maintainability in consideration too

Re: Global variables are not the problem

#72

From the article> "Static Function Variable: In C inside a function, you can declare a static variable. You could consider this as a local variable but I don't since it's on the heap, and can be returned and modified outside of the functions. I absolutely avoid this except as a counter whose address I never return." These variables are not on the heap. They are statically allocated. Their visibility is the only thing…

static variable addresses are an extremely important tool in static analysis, for proving certain program invariants hold. In C, a const static often will be able to tell you more about the structure of your code at runtime than a #define macro ever could! though unless you're programming extremely defensively (eg trying to thwart a nation state), I see no reason why you would use them at runtime

Could you expand on this? I don't understand the point you're making, or how it's useful.

Re: Global variables are not the problem

#73

I find the concept of a context structure passed as the first parameter to all your functions with all your "globals" to be very compelling for this sort of stuff.

Hard disagree. If I have 500 functions, I don't want to extrapolate out the overhead of passing a state object around to all of them. That's a waste of effort, and frankly makes me think you want to code using an FP paradigm even in imperative languages. Module-level and thread-level "globals" are fine. You gain nothing (other than some smug ivory tower sense of superiority) by making your functions pure and passing…

Just need to make sure your module doesn't get too big or unwieldy. I work in a codebase with some "module" C files with a litany of global statics and it's very difficult to understand the possible states it can be in or test it.

I agree that so long as overall complexity is limited these things can be OK. As soon as you're reading and writing a global in multiple locations though I would be extremely, extremely wary.

Re: Global variables are not the problem

#74

Earlier quoted context omitted.

Hard disagree. If I have 500 functions, I don't want to extrapolate out the overhead of passing a state object around to all of them. That's a waste of effort, and frankly makes me think you want to code using an FP paradigm even in imperative languages. Module-level and thread-level "globals" are fine. You gain nothing (other than some smug ivory tower sense of superiority) by making your functions pure and passing…

You get functions that are easily testable in isolation with all state provided in parameters. You also get explicit dependencies and scoping controlled by caller. I don't mind globals but saying you get nothing for avoiding them is :/

If that’s so useful, make your language support the concept of lexical environments instead. Otherwise it’s just manual sunsetting every day of week. Our craft is full of this “let’s pretend we’re in a lisp with good syntax” where half of it is missing, but fine, we’ll simulate it by hand. Dirt and sticks engineering.

(To be clear, I’m just tangentially ranting about the state of things in general, might as well post this under somewhere else.)

Re: Global variables are not the problem

#75

Earlier quoted context omitted.

The burden is on the programmer adding a new thread to know what they can safely access. The conclusion of your argument looks like 2000s Java - throw a mutex on every property because you never know when it will need to be accessed on a thread. Designs that spread complexity rather than encapsulate it are rarely a good idea.

> Designs that spread complexity rather than encapsulate it are rarely a good idea. Exactly, globals spread complexity. You need to look at the implementation of each function to know if you can call it. That’s the landmine.

I agree that dependent sequences of events, coordinated through a global are bad. But there are other usages which are not error prone. For example an allocation, logger, readonly settings, or a cache.

Re: Global variables are not the problem

#76
There are only a few use-cases where global variables make sense:

1. Thread container registry with mutex lock for garbage collection and inter-process communication (children know their thread ID)

2. volatile system register and memory DMA use in low level cpu/mcu (the compiler and or linker could pooch the hardware memory layout)

3. Performance optimized pre-cached shared-memory state-machines with non-blocking magic

4. OS Unikernels

Not sure I have seen many other valid use-cases that splatter language scopes with bad/naive designs. YMMV =3

Re: Global variables are not the problem

#77

I find the concept of a context structure passed as the first parameter to all your functions with all your "globals" to be very compelling for this sort of stuff.

Hard disagree. If I have 500 functions, I don't want to extrapolate out the overhead of passing a state object around to all of them. That's a waste of effort, and frankly makes me think you want to code using an FP paradigm even in imperative languages. Module-level and thread-level "globals" are fine. You gain nothing (other than some smug ivory tower sense of superiority) by making your functions pure and passing…

Yes, you gain testability.

If your global state contains something that runs in prod but should not run in a testing environment (e.g. a database connection), your global variable based code is now untestable.

Dependency Injection is popular for a very good reason.

Re: Global variables are not the problem

#80
Any program that uses a database has a very similar problem to global variables.

As Gilad Bracha has pointed out, types are antimodular, and your database schema can be considered one giant type that pervades your program, just like globals can be.

I don't think we have tools to compositionally solve this, across different programming languages.

Post reply on HN