Sign in to follow this  

When to use namespaces

This topic is 3557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I understand the purpose of namespaces, to break up the global namespace, and I can understand how they would be extremely useful when working with a large team of programmers or integrating several code bases. But when you are the sole developer on a project that you are writing from scratch, what is the value in encapsulating all or part of it in a namespace? I keep reading about how important it is to organize into namespaces, but unless I'm writing a library that someone will be importing into a different project, I don't see the point. Can anyone enlighten me?

Share this post


Link to post
Share on other sites
Organisation.

My engine is called 'Symmetry'. All headers are prefixed with 'SYM_' i.e, 'SYM_Terrain.h', as were all of the functions, i.e, 'SYM_InitOpenGL()'.

Instead of prefixing all the functions, I threw them in namespaces, SYM::InitOpenGL(). I can distinguish if the function belongs to the SYMmetry engine, or save msyelf sometying with 'using namespace'.

It's probably the lamest excuse to use them, but it's MY excuse to use them.

Share this post


Link to post
Share on other sites
The major reason to use namespaces is to prevent naming collisions. Say you have a rendering engine that does both 2d and 3d. So we have two point structs, one that is 2d (x,y) and the other 3d (x, y, z). The easiest way to deal with this is within your engine have a 3d namespace and a 2d namespace, that way you can easily know which point you are using.

theTroll

Share this post


Link to post
Share on other sites
Like the others have mentioned it's to avoid name collision. And if you're using any sort of libraries then you'll probably want to encapsulate your modules in namespaces.

An example of something I used today at work (which is kinda ugly, but that's life when you're working with legacy code):

ModuleA::Team& moduleATeam = ModuleA::GetTeam();
MyModule::Team& myTeam = m_myModule.GetTeam();

Without using the MyModule namespace the reference to Team would be ambiguous and the compiler wouldn't be able to resolve the symbol.

Essentially I find that it's good practice to put all your sub modules into their own namespace (with good descriptive names) to reduce the risk of name collision problems. Doing it right off the bat is much easier than retrofitting your code to do it later.

Share this post


Link to post
Share on other sites
Personally, the major benefit of namespace usage is to keep the number of names in scope down. This makes intellisense work speedier and makes it much more likely that I'll hit the unique name (and thus autocomplete) in 2-4 keystrokes. This allows me to use longer, more descriptive variable names without typing issues. More descriptive names are one of the key components of readable, maintainable code.

Plus namespaces help me look for stuff when I forget where I put them (which happens more and more as you get older, more experienced, and work on larger projects).

Share this post


Link to post
Share on other sites

This topic is 3557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this