Josh
Josh

Reputation: 119

How are Variables in Namespaces Better than Global Variables

We all know that global variables are something to avoid, but is putting them in namespaces a solution or just another form of the same problem? Is it still bad to have something like an array that anyone can access and change in a namespace?
Are there any alternatives when you have a variable:

Namespaces make it easier to understand if more variables like this one, all related to the same idea, become necessary. They also make it easier to keep track of. Are these the only thing that make them better than just having a global? I'm not trying to downplay their importance, just curious.
Thinking about this more, in C# global static class members are basically the same scenario as this.

EDIT: Ended up making a new class for it after all (and passing it around by ref like normal). There is a possibility it could grow, so I feel like the class is fine. Plus after writing out the namespace it just felt like a horrible solution for this situation.

Upvotes: 1

Views: 1719

Answers (3)

Steve Jessop
Steve Jessop

Reputation: 279245

There are (at least) three separate difficulties with globals. They:

1) prevent anyone else using the same name (or if they do, they're shadowing your name, which may be confusing). Putting things in namespaces helps with this.

2) make it difficult to simultaneously reason about all the code that directly modifies the global. Namespaces don't help with this, other than anonymous namespaces since they can be used to limit access to the global to one TU. Using private static class members, perhaps with some use of friend, can help. Non-mutable globals also help.

3) make it difficult to simultaneously reason about all the code that indirectly modifies the global and hence hard to reason about what the current state of the global should be at any specified point in the program. Non-mutable globals help. Globals whose state you don't care about help (for example it might not affect the correctness of your code whether a given result is already cached or not, provided that cache staleness is handled correctly). Namespaces do not help. "Hiding" the global state in private data members while still leaving public accessors does not help.

So, do namespaces help with the difficulties of globals? A little bit, but not much.

If you are aware of other difficulties with globals then you can assess for yourself whether namespaces help. If you aren't aware of the difficulties with globals, then you are not doing yourself any favours by avoiding them as a matter of religious faith just because someone told you that everyone knows they're bad. One way to get aware to ask someone who's willing to teach you something instead of just trying to scare you into being a good programmer. Another way is to use them and see what trouble you get into, if any.

Upvotes: 1

cooooookie monster
cooooookie monster

Reputation: 334

I believe it is really just part of the same problem. why not ask yourself this before considering to put a variable in a namespace,

Question) can this variable/array be contained within a class for its lifetime and any other class that needs it contain a pointer/reference to this variable.

normally by allowing classes access to the variable via a pointer you can treat it much the same way as a global variable, just make sure to pass it into the constructor, or have a setter for it

Generally your classes want to encapsulate an objects state, with functions to manipulate it. a namespace on the other hand can be thought of more as a grouping of related functions used to solve a task i.e. the std namespace with something like...

 std::cout << "msg here" << std::endl;

there are functions contained within this namespace in this example to print a buffer to the screen and flush the output

Upvotes: 0

mugiseyebrows
mugiseyebrows

Reputation: 4698

When we design program in OOP style, we define it as interaction of entities. Entities hide their state in implementation details, and provide interface (to make interactions possible). It is proven effictive to define all entities as classes. If we have globals, we compromise beauty and break incapsilation principle, having entity state bare and outside of any class. Namespace can't help us in this case.

Upvotes: 0

Related Questions