Namespace: scope or address?

Publication date: 2022-11-03

In this post I’ll compare the namespace concept in Java/PHP/JS and I’ll try to answer the question in the title.

First of all, what is namespace in common? Wiki 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, in a common 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

I guess namespaces in Java were from the beginning — they already were when I first time learned to write Java code many years ago. Namespace in Java is called a package:

package java.awt.event; Each Java class is placed in a separate file and each file should contain a package declaration. All files (and packages) form a hierarchy:

|- java

|– applet

|— AppletContext

|— …

|– awt

|— event

|—- ActionListener

|… It is very easy to navigate Java code — sources forjava.applet.AppletContext class will be in ./java/applet/AppletContext.java, sources forjava.awt.event.ActionListener class will be in ./java/awt/event/ActionListener.java.

The uniqueness of identifiers within a namespace is provided by a file system. Each package (namespace) forms its own context where all classes without explicit access control modifiers are available to each other (see package-private).

For global coverage of all Java code with namespaces, the language designers have proposed using DNS to ensure that identifiers are unique throughout the all Java code. You should use package com.company if you are an owner of domain company.com.

So, I consider that namespaces in Java are firstly about identification (addressing) and only secondly about scopes (contexts).

PHP

Namespaces appeared in PHP since version 5.3 and they are closer to the filesystem than namespaces in Java:

namespace Zend; Unlike the Java, it is possible to have more than onenamespace in a file:

namespace Space1;

namespace Space2;

… But “It is strongly discouraged as a coding practice to combine multiple namespaces into the same file” ©

Namespaces in PHP can be nested:

namespace Project; … and can be addressed absolutely or relatively, just like in a filesystem (alias == symbolic link):

<?php namespace Project

namespace Project

namespace {

use Projectas ModA;

$modA = new ();

$modB = new ();

$modB2 = new Demo();

$modA2 = new ModA();

}

PHP namespaces have more flexible options for addressing code elements than Java packages but both languages allow IDE (and the developer) to find source code by FQN.

Of cause, each namespace in PHP sets its own scope, just like a package in Java. I believe it’s a natural ability of the namespace.

JavaScript

Namespace in JS is not a concept of language itself. JS does not contain any statements like namespace or package and the question “How do I declare a namespace in JavaScript?” is a very-very old question that has more than one right answer. The most popular answer I know — “just create named scope”:

const MyNamespace = (function() {…})(); Unlike Java/PHP ‘namespace’ in JS is firstly about scopes and not about identification (addressing) at all. We can create nested ‘namespaces’ and all will be fine in runtime — all nested scopes will be isolated (e.g., we can have function myFunc in every scope):
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();

But different myFuncs still cannot be addressed uniquely. An IDE’s Copy Reference action returns only myFunc, without a namespace.

It is a big problem of this language — we don’t have one commonly used rule to address code elements (constants, objects, functions, classes) globally. To address for developer, not for computer. Try to launch the ‘Find Usage’ action for any of myFuncs in any IDE. This action is almost useless in a big (a really big!) projects. Perhaps, this moment is a one of the reasons for the TypeScript appearance many years ago.

Of cause, modern JS with ES6 modules and with npm is closer to enterprise level language than it was in 2012 (when TypeScript first appeared). But JS is not as convenient for large projects as Java/PHP because of the lack of the right namespaces.

Resume

So, what is the answer to the question in the title? I’m sure, the namespace is an address and is a scope. It’s like a regular post address and a real place.

Image 2

Every post address matches a real place but not every real place has its own address. But more importantly, namespace is about addressing in source code (for developers), not about addressing in runtime (for computers). In my opinion of course :)

If you enjoyed this article, please give it a clap and follow me for more content!

Stay connected:

Thank you for your support!

Additional source-code excerpts

const MyNamespace = (() => {
  function myFunc() { console.log('Main space.'); }
  return { fn: myFunc };
})();
((mainSpace) => {
  function myFunc() { console.log('Nested space.'); }
  mainSpace.subSpace = { fn: myFunc };
})(MyNamespace);