Module Boundaries for Namespacing

Namespaces are a feature of many computer programming languages, and they have great benefits for large projects, especially those with multiple programmers working simultaneously. At the simplest level, a namespace is a way to group related classes together, and at the same time separate those classes from other, unrelated classes.

A project that uses namespaces has three big wins over a project that doesn’t:

  • One developer can work on a feature within the confines of one namespace, while another developer works on a different feature within the confines of its own namespace. They don’t risk stepping on each other’s toes.

  • If it makes sense for the overall project, two classes can have the same name, as long as they are in different namespaces. For example, a Clothing::Boot can exist alongside an Automobile::Boot (as in the trunk of the car) without any problem. The namespace provides the context, and there are no naming collisions.

  • Namespaces can provide an abstraction barrier between large, disparate sets of code. If documentation is published describing what’s “public” in the namespace, everything else within it can be changed safely as long as classes in other namespaces restrict themselves to using the published API.

Note

Namespaces do for classes what classes do for data and methods.

In Ruby, namespaces are implemented with modules. Around every class in the module, or around a set of classes, you specify the start and end of the module: ...

Get Enterprise Rails now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.