Information about Namespace (programming)

Put simply, a "Namespace" is a set of names in which all names are unique.

A namespace is a context in which a group of one or more identifiers 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 programs or documents 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 languages 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 line

using 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 collisions. 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 packages. 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 java.lang contains classes core to the language, and java.lang.reflect 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 scopes can be viewed as implicit namespaces that are inextricably linked with visibility, accessibility, and object lifetime.

XML Namespace

Main article: 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 identifiers are unique and can therefore be used in larger programs without fear of identifier naming collisions.

Unfortunately, this technique has several drawbacks:
  • It doesn't scale well to nested namespaces; identifiers become excessively long.
  • Individuals or organizations may use dramatically inconsistent naming conventions, potentially introducing unwanted obfuscation.
  • Compound or 'query-based' operations on groups of identifiers, based on the namespaces in which they are declared, is rendered unwieldy or unfeasible.
  • All uses of the identifiers must, in effect, be fully namespace-qualified. Languages with direct support for namespaces usually provide ways for the programmer to declare up front that they wish to use some or all identifiers from a specific namespace, which they can then use without qualification for the remainder of the block.

See also

Identifiers (IDs) are lexical tokens that name entities. The concept is analogous to that of a "name". Identifiers are used extensively in virtually all information processing systems.
..... Click the link for more information.
A computer program is one or more instructions that are intended for execution by a computer. Specifically, it is a symbol or combination of symbols forming an algorithm that may or may not terminate, and that algorithm is written in a programming language.
..... Click the link for more information.
document (noun) is a bounded physical representation of body of information designed with the capacity (and usually intent) to communicate. A document may manifest symbolic, diagrammatic or sensory-representational information.
..... Click the link for more information.
Modularity is a concept that has applications in the contexts of computer science, particularly programming, as well as cognitive science in investigating the structure of mind.
..... Click the link for more information.
The term computer language includes a wide variety of languages used to communicate with computers. It is broader than the more commonly-used term programming language. Programming languages are a subset of computer languages.
..... Click the link for more information.
C++
Paradigm: Multi-paradigm
Appeared in: 1983
Designed by: Bjarne Stroustrup
Typing discipline: Static, unsafe, nominative
Major implementations: G++, Microsoft Visual C++, Borland C++ Builder
Dialects: ISO/IEC C++ 1998, ISO/IEC C++ 2003
..... Click the link for more information.
Python

Paradigm: Multi-paradigm
Appeared in: 1991
Designed by: Guido van Rossum
Developer: Python Software Foundation
Latest release: 2.5.1/ April 18 2007
Latest unstable release: 3.
..... Click the link for more information.
tree is a widely-used data structure that emulates a tree structure with a set of linked nodes.

Nodes

A node may contain a value or a condition or represents a separate data structure or a tree of its own.
..... Click the link for more information.
C++
Paradigm: Multi-paradigm
Appeared in: 1983
Designed by: Bjarne Stroustrup
Typing discipline: Static, unsafe, nominative
Major implementations: G++, Microsoft Visual C++, Borland C++ Builder
Dialects: ISO/IEC C++ 1998, ISO/IEC C++ 2003
..... Click the link for more information.
A naming collision is a circumstance where two or more identifiers in a given namespace or a given scope cannot be unambiguously resolved, and such unambiguous resolution is a requirement of the underlying system.
..... Click the link for more information.
In C++, the Standard Library is a collection of classes and functions, which are written in the core language. The Standard Library provides several generic containers, functions to utilise and manipulate these containers, function objects, generic strings and streams (including
..... Click the link for more information.
Java

Paradigm: Object-oriented, structured, imperative
Appeared in: 1995
Designed by: Sun Microsystems
Typing discipline: Static, strong, safe, nominative
Major implementations: Numerous
Influenced by: Objective-C, C++, Smalltalk, Eiffel,[1]
..... Click the link for more information.
A Java package is a mechanism for organizing Java classes into namespaces. Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time.
..... Click the link for more information.
In computer programming, a fully qualified name is an unambiguous name that specifies which object, function, or variable a call refers to absolutely. To fully-qualify most often means to explicitly refer to namespaces that would otherwise be implicit because of the scope of the
..... Click the link for more information.
In computer science, reflection is the process by which a computer program of the appropriate type can be modified in the process of being executed, in a manner that depends on abstract features of its code and its runtime behavior.
..... Click the link for more information.
Ada

Paradigm: multi-paradigm: concurrent, distributed, generic-programming, imperative, object-oriented
Appeared in: 1983, last revised 2005
Designed by: Jean Ichbiah, extended
by S.
..... Click the link for more information.
C#
Paradigm: structured, imperative, object-oriented
Appeared in: 2001 (last revised 2005)
Designed by: Microsoft Corporation
Typing discipline: static, strong, both safe and unsafe, nominative
Major implementations: .NET Framework, Mono, DotGNU
Dialects: 1.
..... Click the link for more information.
.NET Framework is a software component that can be added to or is included with Microsoft Windows operating system. It provides a large body of pre-coded solutions to common program requirements, and manages the execution of programs written specifically for the framework. The .
..... Click the link for more information.
In computer science, a subroutine (function, method, procedure, or subprogram) is a portion of code within a larger program, which performs a specific task and can be relatively independent of the remaining code.
..... Click the link for more information.
This article or section may be confusing or unclear for some readers.
Please [improve the article] or discuss this issue on the talk page. This article has been tagged since December 2006.
..... Click the link for more information.
In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes. The type of scope determines what kind of entities it can contain and how it affects them -- or semantics.
..... Click the link for more information.
In computer science, the object lifetime (or life cycle) of an object in object-oriented programming is the time between an object's creation (also known as instantiation or construction) till the object is no longer used, and is destructed or
..... Click the link for more information.
An XML namespace is a W3C recommendation for providing uniquely named elements and attributes in an XML instance. An XML instance may contain element or attribute names from more than one XML vocabulary.
..... Click the link for more information.
Extensible Markup Language

File extension: .xml
MIME type: application/xml, text/xml (deprecated)
Uniform Type Identifier: public.xml
Developed by: World Wide Web Consortium
Type of format: Markup language
Extended from: SGML
..... Click the link for more information.
search and replace tools with minimal potential for error;
  • to enhance clarity in cases of potential ambiguity;
  • to enhance the aesthetic and professional appearance of work product (for example, by disallowing overly long names, comical or "cute" names, or abbreviations);
    ..... Click the link for more information.
  • C

    The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language.
    ..... Click the link for more information.
    libpng is the official PNG reference library (originally called pnglib). It is a platform-independent library that contains C functions for handling PNG images. It is developed by Guy Eric Schalnat, Andreas Dilger, Glenn Randers-Pehrson and others. libpng is dependent on zlib.
    ..... Click the link for more information.
    libpng is the official PNG reference library (originally called pnglib). It is a platform-independent library that contains C functions for handling PNG images. It is developed by Guy Eric Schalnat, Andreas Dilger, Glenn Randers-Pehrson and others. libpng is dependent on zlib.
    ..... Click the link for more information.
    Identifiers (IDs) are lexical tokens that name entities. The concept is analogous to that of a "name". Identifiers are used extensively in virtually all information processing systems.
    ..... Click the link for more information.
    A naming collision is a circumstance where two or more identifiers in a given namespace or a given scope cannot be unambiguously resolved, and such unambiguous resolution is a requirement of the underlying system.
    ..... Click the link for more information.


    This article is copied from an article on Wikipedia.org - the free encyclopedia created and edited by online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of the wikipedia encyclopedia articles provide accurate and timely information please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.
    Herod_Archelaus


    page counter