Namespaces are a concept used in XML.

They are required because the X in XML is for extensible. An application of XML will typically involve defining a set of new tags. The concept of namespaces ties a set of tags to a URI for uniqueness. Even if two separate XML applications use the tag <key> to mean a musical register or a computer button, they will be segregated by being in a different namespace.

An XML document may have a default namespace for all its tags, which may be overwritten with a usage like <computer:key>.

So in XML namespaces are A Good Thing, but for e2 node titles they are very unhelpful. In XML we try to structure information, in E2 we like to break down the structures.

In fact, namespaces are a very general concept that occurs a lot in computer science. Basically a namespace is an abstract "area" in which different objects cannot have the same name. Or rather, if a name occurs twice, it is assumend that it means the same object. Also, within a namespace, a name that belongs to it can be used directly, without further qualification.

Example: modern programming languages give separate parts of code separate namespaces for variables so that you can use variable names without having to worry about someone else who works on another part of the code using the same names. Using variables outside your own modules, classes or whatever is sometimes possible, but you have to specify where the variable can be found in addition to its name.

A namespace is a characteristic of the directory table on a traditional NetWare volume. It stores the names of file entries on the volume. Most popular variations are DOS (the default), LONG (formerly known as OS/2), NFS, and Macintosh Multiple namespaces can exist on the same volume, and there are rules for translating names across them. Also, namespaces contain additional information that some filesystems would have, like Macintosh resource forks or NFS permissions.

On an NSS volume, the directory structure is different, and all namespaces are loaded and enabled by default.

In C++, each structure, class, or union has its own namespace. Within that namespace, member names will not conflict with other classes. (This was not the case with very very early versions of C, which is why a lot of structures in the C standard library have their members prefixed with the struct name.)

Also, the 3rd edition of The C++ Programming Language introduces the separate concept of namespace as a keyword in its own right, which allows the programmer to assign an identifier or group of identifiers to a namespace. This helps reduce global namespace pollution which the linker hates so much, especially when there is a collision. Namespaces use the same qualification syntax as classes , but the two are not interchangeable. Namespaces are simpler, in that they only do encapsulation. Stroustrup says1 they are better than classes when you need namespace encapsulation, but don't need type checking and object overhead provided by a class.

In C++ (post 1997), all of the libraries have their own namespaces. The standard library lives in std:: of course. The standard C libraries are also in the std:: namespace.

The using keyword allows you to alias an identifier into the current scope; otherwise the namespace must be qualified for each use. Single identifiers from a namespace can be imported this way, or the entire namespace can be imported. If imported namespaces have overlapping symbols, they must be resolved at compile time, rather than link time, and thus symbol collision errors are caught sooner. The using keyword can clarify which namespace's function is used, if the results are not as desired. Note well that if you just import all the namespaces, you will be back where you started, with the compiler complaining about ambiguous duplicate functions.

Unlike classes, namespaces are "open". The same namespace can be mentioned in different places, adding symbols to it in each place.

Namespaces don't have to be named, just like enums don't have to be named. The effect of an unnamed namespace is that the symbols in the namespace will not be exported out of this compilation unit. Global static has nearly the same effect in C, but is depreciated in C++ in favor of namespaces. Global statics are mostly to prevent name collisions between modules--and namespaces do this better.

Stroustrup2 also recommends giving namespaces long names that include version numbers and full library names, and then using the namespace aliasing mechanism to alias back to a short name that is more manageable. This way, you can change which library you are using just by changing the namespace alias.

If a function is not found in the current namespace (and hasn't been imported from another namespace), the compiler will search the namespaces of the argument types to that function in hopes of finding a matching prototype. Like many other resolution issues, this could seem magical.

For complex examples, check out a book. These examples should be enough to help figure out syntax, though.

namespace MyStuff {
int f();
}     // look, ma, no semicolon

namespace m=MyStuff;    // namespace alias

void g() { m::f(); }     // must be qualified

namespace MyStuff {    // extend it; must use original name, not alias.
int ff();
}

using m::f;    // import f into current namespace

void h() { f(); }    // qualification no longer needed

int MyStuff::f()    // define the function in the namespace
{
return ff();    // this doesn't need qualification to use m::ff
}

using namespace m;    // drag in the rest of MyStuff too

void k() { ff(); }

Note that MyStuff could have been used in place of m above except (obviously) in the alias statement. The alias name can't be used to add to the namespace, so m could not be used there.

Stroustrup hints that it might be a good idea to put everything except main() into appropriate namespaces. Certainly this would make resolving unintentional symbol collisions a lot easier at link time.

For more examples, see using.

 

Sources:

A namespace is a way to use words and names in a context without getting these words confused with other uses or the general use of the word. Inside a namespace, a name has just one meaning(basically). This is similar to the concept of scoping.

In a programming language like C++, this means the same name for an object, variable, type or function can be used in different namespaces, without confusion or clobbering; for example you can refer to the function "swap" in two different namespaces("std" and "my") as: "std::swap" and "my::swap". This can be in the same line of code and the compiler is still able to understand and tell the two different meanings of "swap" apart.

You also can have namespaces in XML, where they help you to use two XML specifications in one document. Namespaces can be hierarchical i.e. nested, like a directory tree (hit the hotkey Windows-E on a windows system to browse).

In biology, scientists came up with the clever trick of using a different language, latin like a namespace which contains the names of species, and the other ranks of taxonomy. So that to the biologist, the word equus for horse refers to the family of horses, while the word horse has the normal meaning of "horses" or even means a specific horse.

On E2, you should be wary of using namespaces; often it is better just to node the word as it is. However, if you have decided to node it in a namespace, you could namespace it by putting a ":" between the namespace name(s) and the actual name. Be careful to put a space before and after the divider(:), because otherwise the search function will not find the node. For example, you could node the tarot card Fortune under
Tarot : Major Arcana : The Wheel of Fortune .
Still, this example is a case where it might be better to just node the card under The Wheel of Fortune or list and explain it under Major Arcana.

Namespaces and the search box:
How the search algorithm of Everything2 could be improved; Such an improvement would both make namespacing more effective, but would also make it superfluous in many cases.
 
As I understand it, the search algorithm currently finds its matches in just one or two SQL queries. This is fast, but instead I suggest that the search makes a search separately for each word found in the search box; After each search, it adds every match to a hash/associative array, with the match id/title as a key and increases this entry by one. Finally, the output is displayed in the order of displaying the keys with the highest number of matches first.
This way, the search for "Tarot Fortune Major Arcana" will rank the pages tarot cards and Tarot : Major Arcana : The Wheel of Fortune among the first two matches found, providing a ranking of search results.

Log in or register to write something here or to contact authors.