When to use namespaces

Started by
3 comments, last by Telastyn 16 years ago
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?
Advertisement
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.

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

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
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.
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).

This topic is closed to new replies.

Advertisement