24

I didn't take the usual CS route to learning programming and I often hear about namespaces but I don't really understand the concept. The descriptions I've found online are usually in the context of C which I'm not familiar with.

I am been doing Ruby for 2 years and I'm trying to get a deeper understanding of the language and OOP.

1
  • 10
    Don't put yourself down - a CS degree is not a good predictor of a good developer and I suspect the majority of developers globally are not so afflicted! Jun 14, 2009 at 10:21

7 Answers 7

52

I am going to provide a more commonplace description.

Say my wife has a sister named Sue, and so do I. How can I tell them apart in a conversation? By using their last names ("Sue Larson" vs "Sue Jones"). The last name is the namespace.

This is a limited example of course, and in programming, the potential family members may be far more numerous than my example, therefore the potential for collisions is higher. Namespaces are also hierarchical in some languages (e.g. java), which is not paralleled with last names.

But otherwise it is a similar concept.

2
  • 2
    Thank you for your wonderful explanation. So simple when explained like that.
    – Sam
    Apr 1, 2011 at 4:34
  • 7
    Another example: if you have Building::Key and Piano::Key in the same application, the namespaces keep the Key classes separate. Jun 20, 2011 at 17:28
18

Definition of namespace from Wikipedia:

A namespace is an abstract container or environment created to hold a logical grouping of unique identifiers (i.e., names). ...

For example, one place you can find namespaces usable is something like this:

You define a constant or a variable, or even a class which has a generic name. If you don't wish to rename this constant/variable/class, but need to have another one with the same name, you can define that new instance in different namespace.

In ruby, a module is basically the same thing a namespace is in C++.

eg:

module Foo
  BAZ = 1
end

module Bar
  BAZ = 2
end

puts Foo::BAZ #=> 1
puts Bar::BAZ #=> 2

So, there, you have constant BAZ declared in two modules (aka namespaces in ruby)

15

A namespace provides a container to hold things like functions, classes and constants as a way to group them together logically and to help avoid conflicts with functions and classes with the same name that have been written by someone else.

In Ruby this is achieved using modules.

3

just think of it as a logical grouping of objects and functionality

3

Shortest answer: Namespaces create scope.

3
  • What do you mean by "scope" in this context? May 5, 2011 at 23:20
  • Scope is enclosing context where objects are associated. Refer - winterdom.com/dev/cpp/nspaces. Do you agree?
    – Ninad
    May 17, 2011 at 12:43
  • Namespaces don't create scope - they encapsulate within a local scope. In computer programming, the scope of a name binding—an association of a name to an entity, such as a variable—is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity.
    – nCardot
    May 6, 2021 at 3:06
2

Wikipedia has a short but useful article on namespaces in general, as well as a much more detailed one for namespaces in computer science, with language-specific examples &c.

The key point is that, when you cannot "logically group" names into namespaces, i.e. you only have a single undifferentiated "spaces" where all names live, to avoid accidental clashes you end up clumsily re-implementing rudimental namespace functionality by such tricks as name prefixes &c. For example, "to draw" means something very different to an artist or to a gunslinger; if you couldn't have separate namespaces for artist-stuff and gunslinger-stuff, you'd end up with identifiers such as artist_draw and gunslinger_draw, and still risk accidental clashes if some library authors use different conventions, etc, etc.

2

From php.net:

What are namespaces? In the broadest definition namespaces are a way of encapsulating items. This can be seen as an abstract concept in many places. For example, in any operating system directories serve to group related files, and act as a namespace for the files within them. As a concrete example, the file foo.txt can exist in both directory /home/greg and in /home/other, but two copies of foo.txt cannot co-exist in the same directory. In addition, to access the foo.txt file outside of the /home/greg directory, we must prepend the directory name to the file name using the directory separator to get /home/greg/foo.txt. This same principle extends to namespaces in the programming world.

So, as another poster mentioned, namespaces can be used to group items together.

To see why this might be useful, suppose you want to write a plug-in for, say, Wordpress, and you want to create a class named 'MyClass'. The trouble is, though, you have no idea if some other developer has already written another Wordpress plug-in using a class named 'MyClass'. So to avoid naming conflicts, instead you name your class 'MyPluginMyClass'. This is annoying, but it probably avoids naming conflicts.

But then comes the release of PHP 5.3, which finally supports namespaces (let's assume, too, that Wordpress and all of the servers on which it is deployed upgrade to PHP 5.3). Now you can create a namespace, say 'MyPlugin', and encapsulate 'MyClass' within it. Having done this, you can publish your plugin without worrying that your version of 'MyClass' will conflict with someone else's version of 'MyClass'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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