Some programming languages (e.g. Java and C++) have language features called "packages" or "namespaces". How useful is it really to have namespaces? It is possible to mark functions and classes as belonging to some particular library without using such a language feature, like the SDL does (e.g. SDL_BlitSurface()). Are namespaces not helpful enough to be worth having? Are they useful in libraries but not in applications? Are they useful everywhere except for in small projects? Thoughts?


Aren't name prefixes the same thing as namespacing, except that it's done in a way that is less useful and more difficult to read/parse? Doesn't the question answer itself?

  • Just putting a prefix at the beginning of an identifier's name isn't the same thing as using a language feature like namespaces. For instance, in C++, you can say that you are using a particular namespace and then not have to put the prefix at the beginning of identifiers in that namespace. – compman Dec 21 '10 at 17:19
  • 2
    @user9521 - that is my point... – Nicole Dec 21 '10 at 17:33
  • +1 The one big advantage of namespaces is that you can skip/shorten the prefix when it's not needed - in the namespace where the particular thing refered to is defined, by using, by import xxxxxxxxx as yyy, etc. – user7043 Dec 21 '10 at 17:33
  • 1
    Since most programmers are lazy, would you rather declare using SDL; or have to type out SDL_* all over the place? – Berin Loritsch Dec 21 '10 at 17:34
  • 2
    +1, but I think you really meant "less useful, more difficult to read, and not verified by the compiler." – Larry Coleman Dec 21 '10 at 17:38

Most (all?) languages with namespaces tend to be object oriented. Many times a human readable name for a type is appropriate, even though there are multiple incompatible implementations. (this brings up other issues on object oriented reuse, but that's not what this question is about). For instance, in Java you have a Timer that is used for background UI tasks, and a Timer that is used for background application (not tied to AWT/Swing) tasks. The namespace allows you to have these same-named objects living in different sub-APIs.

The reason that namespaces came into existence had to do with the unreasonable task of anticipating what other developers will name their objects. C++ introduced the concept (or at least was the first language I was exposed to with the concept), and it was helpful even though there were no guidelines on best use practices. Java adapted the concept and added some "best practices" that included your company name in the namespace. That way you only had to worry about your own company.

Prefixing can become pretty messy. When do you apply it? When do you not apply it? Do structures/classes/global methods get the prefix? What about methods? What about properties in the struct. I've seen all of these things in code, although thankfully not all at once. Namespaces provides some predictability to all of these questions, and makes it a language feature rather than a personal "best practice".

  • Haskell has namespaces (modules) and isn't object oriented. – Jeremy Heiler Dec 21 '10 at 23:51

I think that namespaces are a great idea. They help prevent naming conflicts by limiting the scope of a name. In Java packages, the suggested package naming convention is based on domain names, which should be unique, which help prevent naming conflicts over custom libraries. Overall, they make naming a bit more unique in the broad sense, while still allowing the programmer a good bit more freedom in naming his pieces without having to follow some obscure naming convention.

  • 1
    However, in the case of Java's specific convention, not everyone has a website. Also, if you ever move your program from one website to another (e.g. Sourceforge to Github), it would make sense but be inconvenient to change packages if other things depend on your code. – compman Dec 21 '10 at 17:21
  • 1
    Java's convention applies to your organization, not the place where it is hosted. You can simply declare yourself an organization and be done with it. There's also the problem of URLs allowing characters that can't be used in package names. But we won't go there. So for you, just use "me.user9521" as your package name and you are set. – Berin Loritsch Dec 21 '10 at 17:31
  • 1
    The convention isn't about web names, but about domain names. You can have a domain without a website. – David Thornley Dec 21 '10 at 22:32

Namespaces/Modules/Packages are useful for avoiding name conflicts. So is name prefixing but namespaces have the added convenience of being able to import symbols into the current namespace so that you don't have to bother with the whole Namespace::*.

Some languages (like Python) extend this ability by allowing you to import only specific symbols into your current module or import symbols as a different name. This is useful if you are only interested in a few classes/functions/constants or if some of the symbols conflict with symbols in your namespace but some don't.

Some languages (like Ruby) allow you to include the methods of a module into your class. This is useful for polymorphism and generics. For example, if you have several classes that have iterators that act in the same way, you can mix methods in to all of these classes from a separate module that provides methods to sort and filter the data in the object. This allows has a relationships as well as is a (inheritance) relationships .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.