| Namespace (computer Science) |
Article Index for Namespace |
Information AboutNamespace (computer Science) |
|
A namespace is a context in which a group of one or more Identifier s might exist. An identifier defined in a namespace is associated with that namespace. The same identifier can be independently defined in multiple namespaces, that is, the meaning associated with an identifier defined in one namespace is independent of the same identifier declared in any other namespace. Languages that support namespaces specify the rules that determine which namespace an occurrence of an identifier (ie, not its definition) belongs to. For example, Bill works for company X and his employee ID is 123. John works for company Y and his employee ID is also 123. The reason Bill and John can be identified by the same ID number is because they work for different companies. The different companies in this case would symbolize different namespaces. There would be serious confusion if the two men worked for the same company, and still had the same employee ID. For instance – when it comes time to pay them, who gets the pay check? In large Computer Program s or Document s it is not uncommon to have hundreds or thousands of identifiers. Namespaces (or a similar technique, see Emulating Namespaces ) provide a mechanism for hiding local identifiers. They provide a means of grouping logically related identifiers into corresponding namespaces, thereby making the system more Modular . Many modern Computer Language s provide support for namespaces. In some programming languages (eg. C++ , Python ), the identifiers naming namespaces are themselves associated with an enclosing namespace. Thus, in these languages namespaces can nest, forming a namespace Tree . At the root of this tree is the unnamed global namespace. USE IN COMMON LANGUAGES In C++ , a namespace is defined with a namespace block. namespace foo { int bar; } Within this block, identifiers can be used exactly as they are declared. Outside of this block, the namespace specifier must be prefixed. For example, outside of namespace foo, bar must be written foo::bar. C++ includes another construct that makes this verbosity unnecessary. By adding the lineusing namespace foo; to a piece of code, the prefix foo:: is no longer needed.Code that is not explicitly declared within a namespace is considered to be in the global namespace. Namespace resolution in C++ is hierarchical. This means that within the hypothetical namespace food::soup, the identifier chicken refers to food::soup::chicken if it exists. If it doesn't exist, it then refers to food::chicken if it exists. If neither exist, chicken refers to an identifier in the global namespace.Namespaces in C++ are most often used to avoid Naming Collision s. Although namespaces are used extensively in recent C++ code, most older code does not use this facility. For example, the entire C++ Standard Library is defined within namespace std, but before standardization many components were originally in the global namespace.In the Java Programming Language , the idea of a namespace is embodied in Java Package s. All code belongs to a package, although that package need not be explicitly named. Code from other packages is accessed by prefixing the package name before the appropriate identifier, for example class String in package Java.lang can be referred to as java.lang.String (this is known as the Fully Qualified Class Name ). Like C++, Java offers a construct that makes it unnecessary to type the package name (import). However, certain features (such as Reflection ) require the programmer to use the fully qualified name.Unlike C++, namespaces in Java are not hierarchical as far as the syntax of the language is concerned. However, packages are named in a hierarchical manner. For example, all packages beginning with java are a part of the Java Platform —the package contains classes core to the language, and contains core classes specifically relating to reflection.In Java (as well as Ada , C# , and others), namespaces/packages express semantic categories of code. For example, in C#, namespace System contains code provided by the system (the .NET framework). How specific these categories are and how deep the hierarchies go differ from language to language.Function and Class Scope s can be viewed as implicit namespaces that are inextricably linked with visibility, accessibility, and Object Lifetime . XML NAMESPACE See Also: XML Namespace Although it is not a programming language, XML makes extensive use of namespaces. EMULATING NAMESPACES In programming languages that do not provide language support for namespaces, namespaces can be emulated to some extent by using an Identifier Naming Convention . For example, C libraries such as Libpng often use a fixed prefix for all functions and variables that are part of their exposed interface. Libpng exposes identifiers such as: png_create_write_struct png_get_signature png_read_row png_set_invalid This gives reasonable assurance that the Identifier s are unique and can therefore be used in larger programs without fear of identifier Naming Collision s. Unfortunately, this technique has several drawbacks:
SEE ALSO
|
|
|