Live data from Hacker News

Global variables are not the problem

codestyleandtaste.com

51–60 of 165 posts

Re: Global variables are not the problem

#51

Earlier quoted context omitted.

OK, I will. But I also got a notification about a big comment you posted but it is now deleted. Did you want to rewrite it or did you give up on it?

I could not initially reply to you. Your comment rubbed me the wrong way, because I had no intention of trying to degrade anyone, and frankly, I was offended. But I thought better of my hasty and emotional response. I would rather take a deep breath, re-focus, re-engage, and be educated in a thoughtful dialog than get into a mud slinging contest. I am always willing to be enlightened.

Commendable, still let's not forget that the part I quoted was the beginning of a mud-slinging contest that you seemed willing to start. ;)

So indeed, let's re-focus and re-engage.

To that end, I will post you a longer reply Later™.

Re: Global variables are not the problem

#52

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…

The counter should be declared as static inside the function, thus limiting is scope and avoiding pollution of the global namespace.

Re: Global variables are not the problem

#53

Earlier quoted context omitted.

I could not initially reply to you. Your comment rubbed me the wrong way, because I had no intention of trying to degrade anyone, and frankly, I was offended. But I thought better of my hasty and emotional response. I would rather take a deep breath, re-focus, re-engage, and be educated in a thoughtful dialog than get into a mud slinging contest. I am always willing to be enlightened.

A tip, in your profile you can set a delay which is a number of minutes before your comments will become visible to other people. Mine is set to 2 right now. This gives you time to edit your comment (helpful for some longer ones) but also to write some garbage response and then think better and delete it before anyone's the wiser. It's also helpful to give you time to re-read a mostly good, but maybe not polite, resp…

Much appreciated :)

Re: Global variables are not the problem

#54

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…

The counter should be declared as static inside the function, thus limiting is scope and avoiding pollution of the global namespace.

In this case, yes. Its scope should be the lowest necessary scope. Does JS provide static variables in functions? If not, then that forces you to lift it to some other scope like file or module scope or the surrounding function or class if that's viable.

Re: Global variables are not the problem

#55

This assertion made in the article invalidates the premise of same: With a little encapsulation, you can make globals error-proof ...

What do you suppose is the recommended way to use the encapsulation? ;) This is partly why there's a "defining global variables" section, I know people will will consider some usage as not using a global

Re: Global variables are not the problem

#57
Globals are essential to track state/values of variables in intermediate step, for debug, I create unique object on globals and store intermediate variables in it, so i can inspect values when something goes wrong, being only one unique object to maintain it will not override any existing vars thus not affecting existing programs
Post reply on HN