Live data from Hacker News

Global variables are not the problem

codestyleandtaste.com

31–40 of 165 posts

Re: Global variables are not the problem

#31
post #2

Please no. Singletons if you must. At least you can wrap a mutex around access if you're trying to make it thread safe.

You may hate my article next week, it's meant to replace this article. If you want you can email me for early access and tell me how I can improve the article. Lets say you can guess my email if you're emailing the right domain

Re: Global variables are not the problem

#32

The bug in the program reveals a poor understanding of object lifecycles by whoever wrote it. The `obj` argument to `simple` is not globally unique and so it makes a poor location to store global state information (a count of how often `simple` is called, in this example). Never tie global state information to ephemeral objects whose lifetime may be smaller than what you want to track. In this case, they want to know…

Could you tell me where this was posted? I thought no one would see this after I got no comments the first day No one I showed this to complained about the first example but online many people did. I wrote a completely different article which I think is much better that uses examples I would have used in the follow up. I'll post that article next week

[deleted]

Re: Global variables are not the problem

#33

> The problem is data access. Nothing more, nothing less. I agree with this, but the problem with global variables is precisely that they make bad data access patterns look easy and natural. Speaking from experience, it’s a lot easier to enforce a “no global variables” rule than explain to a new graduate why you won’t allow them to assign a variable in module X even though it’s OK in module Y.

You might like the article I wrote for next week. Could you tell me where this post is linked from? I didn't think anyone would see this when no one commented the first day

Re: Global variables are not the problem

#34

The bug in the program reveals a poor understanding of object lifecycles by whoever wrote it. The `obj` argument to `simple` is not globally unique and so it makes a poor location to store global state information (a count of how often `simple` is called, in this example). Never tie global state information to ephemeral objects whose lifetime may be smaller than what you want to track. In this case, they want to know…

> it is the wrong place to put the count information.

I'd argue this is the case regardless of lifetime. It's trying to squash two unrelated things into one object and should have been two different arguments.

Way more obvious if "obj" is replaced with some example object instead of an empty one:

  let person = { name: "Foo Bar", age: 30, counter: counter };

Re: Global variables are not the problem

#35

The bug in the program reveals a poor understanding of object lifecycles by whoever wrote it. The `obj` argument to `simple` is not globally unique and so it makes a poor location to store global state information (a count of how often `simple` is called, in this example). Never tie global state information to ephemeral objects whose lifetime may be smaller than what you want to track. In this case, they want to know…

I like the diagnosis.

My JS is terrible, but it seems like once you make the counter a global variable it is just better to change it to have an atomic dedicated count function. So instead of incrementing the counter in simple, a globalCount() function gets called that isolates the state. Something like

  {
    let i = 0;
    var counter = function (){
        console.log(++i);
    }
  }
Then call counter() to count & log and document that something horrible and stateful is happening. I wouldn't call that a global variable although the article author disagrees.

Re: Global variables are not the problem

#36
Global variables (in languages where they otherwise make sense and don't have footguns at initialization and whatnot) have two main problems:

1. They work against local reasoning as you analyze the code

2. The semantic lifetime for a bundle of data is rarely actually the lifetime of the program

The second of those is easy to guard against. Just give the bundle of data a name associated with its desired lifetime. If you really only need one of those lifetimes then globally allocate one of them (in most languages this is as cheap as independently handling a bunch of globals, baked into the binary in a low-level language). If necessary, give it a `reset()` method.

The first is a more interesting problem. Even if you bundle data into some sort of `HTTPRequest` lifetime or whatever, the fact that it's bundled still works against local reasoning as you try to use your various counters and loggers and what have you. It's the same battle between implicit and explicit parameters we've argued about for decades. I don't have any concrete advice, but anecdotally I see more bugs from biggish collections of heterogeneous data types than I do from passing everything around manually (just the subsets people actually need).

Re: Global variables are not the problem

#37

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…

Hard to take your comment seriously when you go out of your way to degrade a discussion opponent, FYI.

[deleted]

Re: Global variables are not the problem

#38

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…

Hard to take your comment seriously when you go out of your way to degrade a discussion opponent, FYI.

My comment was not intended to be personally degrading to OP. Apologies if it was taken that way.

Re: Global variables are not the problem

#39

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.

https://en.wikipedia.org/wiki/Dependency_injection

This is very similar to dependency injection. Separating state and construction from function or method implementation makes things a lot easier to test. In my opinion it's also easier to comprehend what the code actually does.

Re: Global variables are not the problem

#40

Earlier quoted context omitted.

Hard to take your comment seriously when you go out of your way to degrade a discussion opponent, FYI.

My comment was not intended to be personally degrading to OP. Apologies if it was taken that way.

I did not say you are targeting OP. I meant that you are degrading your parent commenter.

This:

"You gain nothing (other than some smug ivory tower sense of superiority) by making your functions pure and passing around a global state object to every single method invocation."

...is neither productive nor actually true. But I'll save the latter part for your other reply.

Post reply on HN