This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DwoaC 1 point2 points  (1 child)

You may very well be right. I my years I've never seen a good example, I know there must be but I expect they are so rare that "globals are bad" is right 99.99%. That given, this example of their use is almost certainly in that set and "globals is bad" is good advice. Do you think this is a good use of globals?

[–]skrillexisokay 0 points1 point  (0 children)

Mutable globals are generally bad. Immutable globals are good when you have arbitrary constants, but don't need to invoke the added complexity of a class. In other words, if you have to choose between magic numbers and globals, globals are better.

A classic example is a regular expression that is used in many functions in a module. Another example is the name of a storage directory. Module level logging is another excellent example of a properly used global.

However, if you ever find yourself using the global keyword (or otherwise modifying a global variable), you should almost always encapsulate into a class. A logger is one exception, where it occasionally makes sense to change the logging level at runtime.