Namespace: scope or address?
In this post, I’ll compare the namespace
concept in Java, PHP, and JS and try to answer the question in the title.
What is a namespace?
Wikipedia says:
In computing, a namespace is a set of signs (names) that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.
So, a namespace
is something related to identification and referencing (i.e., addressing).
A namespace name may provide context (scope in computer science) to a name, and the terms are sometimes used interchangeably.
… and is something related to scopes too.
Java
Namespaces in Java have existed from the beginning. A namespace
in
Java is called a package
:
package java.awt.event;
Each Java class is placed in a separate file and includes a package
declaration. Files and packages form a hierarchy:
|- java
|-- applet
|--- AppletContext
|--- ...
|-- awt
|--- event
|---- ActionListener
|...
The uniqueness of identifiers within a namespace is provided by the file system. Each package forms its own context, where classes without explicit access control modifiers are available to each other.
To ensure global uniqueness across all Java code, developers use DNS conventions for naming, e.g., com.company
if
you own company.com
.
PHP
Namespaces in PHP were introduced in version 5.3. They are closely tied to the filesystem, similar to Java:
namespace Zend\Db\Adapter\Driver\Mysqli;
Unlike Java, PHP allows multiple namespaces in a single file (though this is discouraged):
namespace Space1 {
// code
}
namespace Space2 {
// code
}
PHP namespaces can be nested:
namespace Project\Component\Module;
They support absolute and relative addressing, much like a filesystem:
namespace Project\Component\ModuleA {
class Demo {}
}
namespace Project\Component\ModuleB {
class Demo {}
}
namespace {
use Project\Component\ModuleA as ModA;
$modA = new \Project\Component\ModuleA\Demo();
$modB = new \Project\Component\ModuleB\Demo();
}
Both PHP and Java namespaces allow IDEs to locate source code by FQN (Fully Qualified Name).
JavaScript
In JavaScript, the namespace is not a built-in concept. Instead, developers create named scopes manually:
const MyNamespace = (function () {
function myFunc() {
console.log('Main space.');
}
return {fn: myFunc};
})();
(function (mainSpace) {
function myFunc() {
console.log('Nested space.');
}
mainSpace.subSpace = {fn: myFunc};
})(MyNamespace);
MyNamespace.fn();
MyNamespace.subSpace.fn();
Unlike Java or PHP, namespaces in JS focus on scopes rather than addressing. Nested namespaces are runtime-isolated, but
developers cannot uniquely address elements like myFunc
.
This limitation makes JavaScript less suitable for large projects compared to Java or PHP. The introduction of TypeScript has helped mitigate this issue.
Resume
A namespace is both an address and a scope. It resembles a postal address, mapping a unique identifier to a specific location.
In my opinion, namespaces are more about addressing in source code (for developers) than runtime addressing (for computers).
If you enjoyed this article, please give it a clap and follow me for more content!